postinsight.py 2.09 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import json
from url import Url


class PostInsight() :

token = ""
type = ""
link = ""
comment = ""
data = {}
insight_map = {}

def __init__(self,token):
self.token = token.getToken()
#print "Insight"

def setContentInsight(self,content_id):

url ='https://graph.facebook.com/v2.5/%s?fields=link,comments.limit(5),type,insights{values}'%content_id
txt = requests.get(url + self.token).text
#print url + self.token
self.data = json.loads(txt)
self.setContentType(self.data)
self.setContentComment(self.data)
self.setLinkUrl(self.data)
#print self.data
try:
for i in self.data['insights']['data']:
name = i['id'].split("/")[2]
values = i['values'][0]
self.insight_map[name]= values
except:
return

def getContentInsightByKey(self, key):
#subkey is double only

sub_key_list = key.split(',')
try:
if len(sub_key_list) == 2:
ret = self.insight_map[sub_key_list[0]]['value'][sub_key_list[1]]
else :
ret = self.insight_map[sub_key_list[0]]['value']
except:
return 0

if ret == {} :
return 0

return ret


def getContentInsightAll(self):
return self.insight_map

def setContentType(self, data):
try:
self.type = data['type']
except:
self.type = ""

def setContentComment(self, data):
try:
self.comment = Url().getText2bitly(data['comments']['data'][0]['message'])
#print self.comment
except:
self.comment = ""

def setLinkUrl(self, data):
try:
self.link = data['link']
#print self.link
except:
self.link = ""

def getLinkUrl(self):
return self.link

def getContentType(self):
return self.type

def getContentCommentUrl(self):
return self.comment