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 Side-by-side Diff

insight/pageinsight.py View file @ 08123a6
... ... @@ -10,6 +10,13 @@
10 10 token = ""
11 11 data = {}
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  
14 21 def __init__(self, token):
15 22 self.token = token.getToken()
... ... @@ -22,11 +29,15 @@
22 29 % (page_id, i, int(time.time()), int(time.time()) + 12 * 60 * 60, self.token)
23 30 txt = requests.get(url).text
24 31 self.data = json.loads(txt)
25   - name = str(self.data['data'][0]['title'])
26   - val = self.data['data'][0]['values'][0]['value']
27   - if type(val) == type({}):
28   - val = sum(val.values())
29   - self.page_insight[name] = str(val)
  32 +
  33 + try:
  34 + name = str(self.data['data'][0]['title'])
  35 + val = self.data['data'][0]['values'][0]['value']
  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
30 41 return self.page_insight
31 42  
32 43 def getPageInsightByKey(self, key):
... ... @@ -56,9 +56,9 @@
56 56 sqlprefix = sqlprefix[:len(sqlprefix) - 2] + ")"
57 57 sqlvalues = sqlvalues[:len(sqlvalues) - 2] + ");"
58 58  
59   - # print sqlprefix + sqlvalues
  59 + print sqlprefix + sqlvalues
60 60  
61   - datadb.fb_insert(sqlprefix + sqlvalues)
  61 + # datadb.fb_insert(sqlprefix + sqlvalues)
62 62  
63 63 del(datadb)
64 64 del(page_insight)