postinsight.py 2.94 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
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import json
from url import Url


class PostInsight() :

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

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

def setContentInsight(self,content_id):

try:
url ='https://graph.facebook.com/v2.5/%s?fields=message,created_time,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)
self.setContentMessage(self.data)
self.setContentCreated_time(self.data)
#print self.data
except:
return
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 setContentCreated_time(self, data):
try:
self.created_time = data['created_time']
except:
self.created_time = ""

def setContentMessage(self, data):
try:
self.message = data['message']
except:
self.message = ""

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

def getContentCreated_time(self):
try:
return self.created_time.replace('T'," ").replace('+0000',"")
except:
return ""

def getContentMessage(self):
try:
return self.message.replace('\n'," ").replace('\r'," ").replace('\'',"")
except:
return ""