Commit 08123a66ad521eb704df1b7fd477a8b2e431f300

Authored by Leon ago
1 parent e9cb1d7f06
Exists in master

add try-excp in pageinsight

Showing 2 changed files with 18 additions and 7 deletions Inline Diff

insight/pageinsight.py View file @ 08123a6
#!/usr/bin/env python 1 1 #!/usr/bin/env python
# -*- coding: utf-8 -*- 2 2 # -*- coding: utf-8 -*-
3 3
import requests 4 4 import requests
import json 5 5 import json
import time 6 6 import time
7 7
class PageInsight() : 8 8 class PageInsight() :
9 9
token = "" 10 10 token = ""
data = {} 11 11 data = {}
page_insight = {} 12 12 page_insight = {}
13 page_dscp = {'page_fans/lifetime': 'Lifetime_Total_Likes',
14 'page_impressions_unique/day': 'Daily_Total_Reach',
15 'page_engaged_users/day': 'Daily_Page_Engaged_Users',
16 'page_storytellers_by_story_type/day': 'Daily_Talking_About_This_By_Story_Type',
17 'page_impressions_unique/week': 'Weekly_Total_Reach',
18 'page_engaged_users/week': 'Weekly_Page_Engaged_Users',
19 'page_storytellers_by_story_type/week': 'Weekly_Talking_About_This_By_Story_Type'}
13 20
def __init__(self, token): 14 21 def __init__(self, token):
self.token = token.getToken() 15 22 self.token = token.getToken()
16 23
def getPageInsight(self, page_id, item): 17 24 def getPageInsight(self, page_id, item):
18 25
for i in item: 19 26 for i in item:
time.sleep(0.5) 20 27 time.sleep(0.5)
url = 'https://graph.facebook.com/v2.5/%s/insights/%s?debug=all&method=get&pretty=0&suppress_http_code=1&since=%d&until=%d%s' \ 21 28 url = 'https://graph.facebook.com/v2.5/%s/insights/%s?debug=all&method=get&pretty=0&suppress_http_code=1&since=%d&until=%d%s' \
% (page_id, i, int(time.time()), int(time.time()) + 12 * 60 * 60, self.token) 22 29 % (page_id, i, int(time.time()), int(time.time()) + 12 * 60 * 60, self.token)
txt = requests.get(url).text 23 30 txt = requests.get(url).text
self.data = json.loads(txt) 24 31 self.data = json.loads(txt)
name = str(self.data['data'][0]['title']) 25 32
val = self.data['data'][0]['values'][0]['value'] 26 33 try:
if type(val) == type({}): 27 34 name = str(self.data['data'][0]['title'])
val = sum(val.values()) 28 35 val = self.data['data'][0]['values'][0]['value']
self.page_insight[name] = str(val) 29 36 if type(val) == type({}):
37 val = sum(val.values())
38 self.page_insight[name] = str(val)
39 except:
40 self.page_insight[self.page_dscp[i].replace("_", " ")] = 0
return self.page_insight 30 41 return self.page_insight
31 42
def getPageInsightByKey(self, key): 32 43 def getPageInsightByKey(self, key):
33 44
key_x = key.replace("_", " ") 34 45 key_x = key.replace("_", " ")
35 46
try: 36 47 try:
#!/usr/bin/env python 1 1 #!/usr/bin/env python
# -*- coding: utf-8 -*- 2 2 # -*- coding: utf-8 -*-
3 3
from insight.context import Context 4 4 from insight.context import Context
from insight.token import Token 5 5 from insight.token import Token
from insight.pageinsight import PageInsight 6 6 from insight.pageinsight import PageInsight
from insight.datadb import DataDB 7 7 from insight.datadb import DataDB
import time 8 8 import time
9 9
if __name__=='__main__': 10 10 if __name__=='__main__':
11 11
#token_str = "CAAUTLd5JgaoBABnBUUgOjgaUkuviOZCC1otVIJYwYapCRTZBU8ZAbTOd1uo1SBiqHr1eBhVdzoYDoBjPzqO0ImpxGZAXtlXeVtp4r2muUDt3O53NLYqFgrJqKAowcU7fWMsmMTZAAtKT8MXeF4Lb0rHQ2I1gHmbr6nlJWBHzk6dsNPmYHguMp" 12 12 #token_str = "CAAUTLd5JgaoBABnBUUgOjgaUkuviOZCC1otVIJYwYapCRTZBU8ZAbTOd1uo1SBiqHr1eBhVdzoYDoBjPzqO0ImpxGZAXtlXeVtp4r2muUDt3O53NLYqFgrJqKAowcU7fWMsmMTZAAtKT8MXeF4Lb0rHQ2I1gHmbr6nlJWBHzk6dsNPmYHguMp"
#token_str = "EAAUTLd5JgaoBAJMeeMXqcdExQ1egUHeBaIgVBCilmiH4K9RNyUt7gSgVZCZAtszWCLEaZCDQpxewhICtFjNRICFPWAqUygshcSsdEZBUeZAyUJkON7bfQ2NFFI5AqifNahzjFT83GkWZCZCZBXO3050XSjFf9HSR0iAZD" 13 13 #token_str = "EAAUTLd5JgaoBAJMeeMXqcdExQ1egUHeBaIgVBCilmiH4K9RNyUt7gSgVZCZAtszWCLEaZCDQpxewhICtFjNRICFPWAqUygshcSsdEZBUeZAyUJkON7bfQ2NFFI5AqifNahzjFT83GkWZCZCZBXO3050XSjFf9HSR0iAZD"
token = Token() 14 14 token = Token()
15 15
context = Context(token) 16 16 context = Context(token)
page_list = context.getPageList() 17 17 page_list = context.getPageList()
18 18
page_item = ["page_fans/lifetime", 19 19 page_item = ["page_fans/lifetime",
"page_impressions_unique/day", 20 20 "page_impressions_unique/day",
"page_engaged_users/day", 21 21 "page_engaged_users/day",
"page_storytellers_by_story_type/day", 22 22 "page_storytellers_by_story_type/day",
"page_impressions_unique/week", 23 23 "page_impressions_unique/week",
"page_engaged_users/week", 24 24 "page_engaged_users/week",
"page_storytellers_by_story_type/week"] 25 25 "page_storytellers_by_story_type/week"]
26 26
page_dscp = {'page_fans/lifetime': 'Lifetime_Total_Likes', 27 27 page_dscp = {'page_fans/lifetime': 'Lifetime_Total_Likes',
'page_impressions_unique/day': 'Daily_Total_Reach', 28 28 'page_impressions_unique/day': 'Daily_Total_Reach',
'page_engaged_users/day': 'Daily_Page_Engaged_Users', 29 29 'page_engaged_users/day': 'Daily_Page_Engaged_Users',
'page_storytellers_by_story_type/day': 'Daily_Talking_About_This_By_Story_Type', 30 30 'page_storytellers_by_story_type/day': 'Daily_Talking_About_This_By_Story_Type',
'page_impressions_unique/week': 'Weekly_Total_Reach', 31 31 'page_impressions_unique/week': 'Weekly_Total_Reach',
'page_engaged_users/week': 'Weekly_Page_Engaged_Users', 32 32 'page_engaged_users/week': 'Weekly_Page_Engaged_Users',
'page_storytellers_by_story_type/week': 'Weekly_Talking_About_This_By_Story_Type'} 33 33 'page_storytellers_by_story_type/week': 'Weekly_Talking_About_This_By_Story_Type'}
34 34
for page_id in page_list: 35 35 for page_id in page_list:
36 36
datadb = DataDB() 37 37 datadb = DataDB()
38 38
page_insight = PageInsight(token) 39 39 page_insight = PageInsight(token)
page_report = page_insight.getPageInsight(page_id, page_item) 40 40 page_report = page_insight.getPageInsight(page_id, page_item)
41 41
sqlprefix = "insert into facebook_page_insight (" 42 42 sqlprefix = "insert into facebook_page_insight ("
sqlvalues = " values (" 43 43 sqlvalues = " values ("
44 44
sqlprefix += "`p_id`, " 45 45 sqlprefix += "`p_id`, "
sqlprefix += "`loging_time`, " 46 46 sqlprefix += "`loging_time`, "
47 47
sqlvalues += page_id + ", " 48 48 sqlvalues += page_id + ", "
sqlvalues += "NOW(), " 49 49 sqlvalues += "NOW(), "
50 50