Commit 40c655346a959f901c6cb85d6f3fa2e1007ca1cf

Authored by Noah ago
1 parent a2821fd315
Exists in master

getContextsFeedAll fix

Showing 5 changed files with 96 additions and 115 deletions Side-by-side Diff

insight/context.py View file @ 40c6553
... ... @@ -8,7 +8,7 @@
8 8 class Context() :
9 9  
10 10 token = ""
11   -
  11 + contents_list = []
12 12 orderlist = [529491587137957,858096217617804,1670879929842831,1406069429705944,1531526667169602,
13 13 504207933048979,1531526667169602,270103526524526,1702892339935231,886456498082470,
14 14 167968880220563,526019510826052,327293184069664 ,467342726674321,497899983625735,
... ... @@ -29,6 +29,18 @@
29 29 #print "context"
30 30 self.token = token.getToken()
31 31  
  32 + def isNext(self,data):
  33 + if data['data'] == []:
  34 + return False
  35 + else:
  36 + return True
  37 +
  38 + def nextUrl(self,data):
  39 + try:
  40 + return data['paging']['next']
  41 + except:
  42 + return ""
  43 +
32 44 def setOrderList(self, list):
33 45 self.orderlist = list
34 46  
35 47  
36 48  
37 49  
38 50  
39 51  
40 52  
41 53  
... ... @@ -41,22 +53,22 @@
41 53 def getOrderList(self):
42 54 return self.orderlist
43 55  
44   - def getContextsFeed(self):
45 56  
46   - return self.getContextsFeedAndTimestemp(0)
  57 + def setContextsFeed(self):
47 58  
48   - def getContextsFeedAll(self):
  59 + self.setContextsFeedAndTimestemp(0)
49 60  
  61 + def setContextsFeedAll(self):
  62 +
50 63 signed_max_int = 0x7fffffff - 1
51   - return self.getContextsFeedAndTimestemp(signed_max_int)
  64 + self.setContextsFeedAndTimestemp(signed_max_int)
52 65  
  66 + def setContextsFeedAndTimestemp(self,ts):
53 67  
54   - def getContextsFeedAndTimestemp(self,ts):
55   -
56 68 time_now = int(time.time())
57 69  
58 70 return_val = []
59   - for list in self.orderlist:
  71 + for list in self.orderlist[:1]:
60 72 time.sleep(1)
61 73 url = 'https://graph.facebook.com/v2.5/%d/feed?'%list
62 74 txt = requests.get(url + self.token).text
63 75  
64 76  
65 77  
... ... @@ -73,19 +85,15 @@
73 85 txt = requests.get(nextUrl).text
74 86 data = json.loads(txt)
75 87 return_val.append(data)
76   - #print data
77 88  
78   - return return_val
  89 + self.setContentsList(return_val)
79 90  
80   - def isNext(self,data):
81   - if data['data'] == []:
82   - return False
83   - else:
84   - return True
  91 + def setContentsList(self, list):
  92 + for i in list:
  93 + #print i
  94 + for j in i['data']:
  95 + self.contents_list.append(j)
85 96  
86   - def nextUrl(self,data):
87   - try:
88   - return data['paging']['next']
89   - except:
90   - return ""
  97 + def getContentsList(self):
  98 + return self.contents_list
insight/insight.py View file @ 40c6553
1   -#!/usr/bin/env python
2   -# -*- coding: utf-8 -*-
3   -import requests
4   -import json
5   -
6   -class Insight() :
7   -
8   - token = ""
9   - type = ""
10   - data = {}
11   - insight_map = {}
12   -
13   - def __init__(self,token):
14   - self.token = token.getToken()
15   - #print "Insight"
16   -
17   - def setContentInsight(self,content_id):
18   -
19   - url ='https://graph.facebook.com/v2.5/%s?fields=type,insights{values}'%content_id
20   - txt = requests.get(url + self.token).text
21   - self.data = json.loads(txt)
22   - self.setContentType(self.data)
23   - #print txt
24   - for i in self.data['insights']['data']:
25   - name = i['id'].split("/")[2]
26   - values = i['values'][0]
27   - self.insight_map[name]= values
28   -
29   - def getContentInsightByKey(self, key):
30   - #subkey is double only
31   -
32   - sub_key_list = key.split(',')
33   -
34   - try:
35   - if len(sub_key_list) == 2:
36   - ret = self.insight_map[sub_key_list[0]]['value'][sub_key_list[1]]
37   -
38   - else :
39   - ret = self.insight_map[sub_key_list[0]]['value']
40   -
41   - if ret == {} :
42   - return 0
43   -
44   - return ret
45   -
46   - except:
47   - return 0
48   -
49   -
50   - def getContentInsightAll(self):
51   - return self.insight_map
52   -
53   - def setContentType(self, data):
54   - self.type = data['type']
55   - #print self.type
56   -
57   - def getContentType(self):
58   - return self.type
insight/page.py View file @ 40c6553
1   -#!/usr/bin/env python
2   -# -*- coding: utf-8 -*-
3   -class Page() :
4   -
5   - Contents_list = []
6   -
7   - def __init__(self, list):
8   - self.setContentsList(list)
9   -
10   - def setContentsList(self, list):
11   - for i in list:
12   - #print i
13   - for j in i['data']:
14   - self.Contents_list.append(j)
15   -
16   - def getContentsList(self):
17   -
18   - return self.Contents_list
insight/postinsight.py View file @ 40c6553
  1 +#!/usr/bin/env python
  2 +# -*- coding: utf-8 -*-
  3 +
  4 +import requests
  5 +import json
  6 +
  7 +class PostInsight() :
  8 +
  9 + token = ""
  10 + type = ""
  11 + data = {}
  12 + insight_map = {}
  13 +
  14 + def __init__(self,token):
  15 + self.token = token.getToken()
  16 + #print "Insight"
  17 +
  18 + def setContentInsight(self,content_id):
  19 +
  20 + url ='https://graph.facebook.com/v2.5/%s?fields=type,insights{values}'%content_id
  21 + txt = requests.get(url + self.token).text
  22 + self.data = json.loads(txt)
  23 + self.setContentType(self.data)
  24 + #print txt
  25 + try:
  26 + for i in self.data['insights']['data']:
  27 + name = i['id'].split("/")[2]
  28 + values = i['values'][0]
  29 + self.insight_map[name]= values
  30 + except:
  31 + return
  32 +
  33 + def getContentInsightByKey(self, key):
  34 + #subkey is double only
  35 +
  36 + sub_key_list = key.split(',')
  37 + try:
  38 + if len(sub_key_list) == 2:
  39 + ret = self.insight_map[sub_key_list[0]]['value'][sub_key_list[1]]
  40 + else :
  41 + ret = self.insight_map[sub_key_list[0]]['value']
  42 + except:
  43 + return 0
  44 +
  45 + if ret == {} :
  46 + return 0
  47 +
  48 + return ret
  49 +
  50 +
  51 + def getContentInsightAll(self):
  52 + return self.insight_map
  53 +
  54 + def setContentType(self, data):
  55 + try:
  56 + self.type = data['type']
  57 + except:
  58 + self.type = ""
  59 +
  60 + def getContentType(self):
  61 + return self.type
... ... @@ -3,8 +3,7 @@
3 3  
4 4 from insight.context import Context
5 5 from insight.token import Token
6   -from insight.page import Page
7   -from insight.insight import Insight
  6 +from insight.postinsight import PostInsight
8 7 from insight.datadb import DataDB
9 8 import time
10 9  
11 10  
... ... @@ -15,11 +14,9 @@
15 14 token = Token(token_str)
16 15  
17 16 contexts = Context(token)
18   - contexts_list = contexts.getContextsFeedAndTimestemp(3600 * 24 * 10)
  17 + contexts.setContextsFeedAndTimestemp(3600 * 24 * 10)
  18 + contents_list = contexts.getContentsList()
19 19  
20   - page = Page(contexts_list)
21   - contents_list = page.getContentsList()
22   -
23 20 for content in contents_list:
24 21 datadb = DataDB()
25 22 #print content
... ... @@ -34,7 +31,7 @@
34 31 except:
35 32 message = " "
36 33  
37   - insight = Insight(token)
  34 + insight = PostInsight(token)
38 35 insight.setContentInsight(content['id'])
39 36 lists =["post_story_adds_unique",
40 37 "post_story_adds",
... ... @@ -104,9 +101,9 @@
104 101 sqlprefix = sqlprefix[:len(sqlprefix)-2] + ")"
105 102 sqlvalues = sqlvalues[:len(sqlvalues)-2] + ");"
106 103  
107   - datadb.fb_insert(sqlprefix + sqlvalues)
108   - datadb.fb_insert(sqlreplace + sqlprefix[32:] + sqlvalues)
109   - #print sqlprefix + sqlvalues
  104 + #datadb.fb_insert(sqlprefix + sqlvalues)
  105 + #datadb.fb_insert(sqlreplace + sqlprefix[32:] + sqlvalues)
  106 + print sqlprefix + sqlvalues
110 107  
111 108 del(datadb)
112 109 del(insight)