Commit ba735d8157d3e36d24d5ea9d2d72f69d86d1196c

Authored by steve ago
Exists in master

Merge branch 'master' of http://gitlab.pikicast.com/Noah/dsfacebook

Showing 4 changed files Side-by-side Diff

insight/postinsight.py View file @ ba735d8
... ... @@ -12,6 +12,8 @@
12 12 type = ""
13 13 link = ""
14 14 comment = ""
  15 + created_time = ""
  16 + message = ""
15 17 data = {}
16 18 insight_map = {}
17 19  
18 20  
... ... @@ -21,13 +23,14 @@
21 23  
22 24 def setContentInsight(self,content_id):
23 25  
24   - url ='https://graph.facebook.com/v2.5/%s?fields=link,comments.limit(5),type,insights{values}'%content_id
  26 + url ='https://graph.facebook.com/v2.5/%s?fields=message,created_time,link,comments.limit(5),type,insights{values}'%content_id
25 27 txt = requests.get(url + self.token).text
26 28 #print url + self.token
27 29 self.data = json.loads(txt)
28 30 self.setContentType(self.data)
29 31 self.setContentComment(self.data)
30 32 self.setLinkUrl(self.data)
  33 + self.setContentCreated_time(self.data)
31 34 #print self.data
32 35 try:
33 36 for i in self.data['insights']['data']:
... ... @@ -58,6 +61,19 @@
58 61 def getContentInsightAll(self):
59 62 return self.insight_map
60 63  
  64 +
  65 + def setContentCreated_time(self, data):
  66 + try:
  67 + self.created_time = data['created_time']
  68 + except:
  69 + self.created_time = ""
  70 +
  71 + def setContentMessage(self, data):
  72 + try:
  73 + self.message = data['created_time']
  74 + except:
  75 + self.message = ""
  76 +
61 77 def setContentType(self, data):
62 78 try:
63 79 self.type = data['type']
... ... @@ -86,4 +102,16 @@
86 102  
87 103 def getContentCommentUrl(self):
88 104 return self.comment
  105 +
  106 + def getContentCreated_time(self):
  107 + try:
  108 + return self.created_time.replace('T'," ").replace('+0000',"")
  109 + except:
  110 + return ""
  111 +
  112 + def getContentMessage(self):
  113 + try:
  114 + return self.message.replace('\n'," ").replace('\r'," ").replace('\'',"")
  115 + except:
  116 + return ""
insight/url.py View file @ ba735d8
... ... @@ -17,6 +17,8 @@
17 17  
18 18 link = link.replace('\n'," ").replace('\r'," ").replace('\'',"")
19 19  
  20 + print link
  21 +
20 22 if len(link) < 5:
21 23 continue
22 24  
... ... @@ -31,7 +33,6 @@
31 33 })
32 34  
33 35 elif self.isrpiki(link) :
34   - print "여기?"
35 36 print link
36 37 data.append({
37 38 'bitly_url' : "",
... ... @@ -42,7 +43,6 @@
42 43 })
43 44  
44 45 elif self.isbitly(link):
45   -
46 46 try:
47 47 link = "http://bit.ly/" + re.compile('[^./a-zA-Z0-9]+').sub("",link.split("//bit.ly/")[1].split(" ")[0])
48 48  
... ... @@ -68,6 +68,8 @@
68 68 except:
69 69 piki_url = str(0)
70 70  
  71 + print self.rpiki2click(txt[source_bgn:source_end])
  72 +
71 73 if self.isrpiki(txt[source_bgn:source_end]) :
72 74 data.append({
73 75 'bitly_url' : str(link),
74 76  
75 77  
76 78  
77 79  
78 80  
79 81  
80 82  
81 83  
82 84  
... ... @@ -105,32 +107,51 @@
105 107 return "0"
106 108  
107 109 def rpiki2click(self,url):
108   - #print url
  110 + print url
109 111 api = self.server + "contents_RPIKI_api/"
110 112  
111 113 try:
112 114 fr = url.split("fr=")[1].split("&")[0]
113 115 except:
114 116 fr = ""
115   -
116 117 try:
117 118 cid = url.split("cid=")[1].split("&")[0]
  119 + except:
  120 + cid = ""
  121 + try:
118 122 m = url.split("m=")[1].split("&")[0]
  123 + except:
  124 + m = ""
  125 + try:
119 126 c = url.split("c=")[1].split("&")[0]
  127 + except:
  128 + c =""
  129 + try:
120 130 v = url.split("v=")[1].split("&")[0]
  131 + except:
  132 + v = ""
  133 + try:
121 134 t = url.split("t=")[1].split("&")[0]
  135 + except:
  136 + t = ""
  137 +
  138 + try:
122 139 data = json.loads(requests.get(api + cid + '_' + fr + '_' + m + '_' + c + '_' + v + '_' + t).text)
  140 +
123 141 ret = data['data']['real']
124 142 if ret == "":
125 143 ret = "0"
126   - except:
  144 + except Exception as e:
  145 + print e
127 146 ret = "0"
128 147  
129 148 return ret
130 149  
131 150 def getText2bitly(self,text):
132   - return "http://bit.ly/" + text.split("http://bit.ly/")[1].split(" ")[0]
133   -
  151 + try:
  152 + return "http://bit.ly/" + text.split("http://bit.ly/")[1].split(" ")[0]
  153 + except:
  154 + return ""
134 155  
135 156 def isdeep(self, url):
136 157  
runcontent.py View file @ ba735d8
  1 +#!/usr/bin/env python
  2 +# -*- coding: utf-8 -*-
  3 +
  4 +from insight.context import Context
  5 +from insight.token import Token
  6 +from insight.postinsight import PostInsight
  7 +from insight.datadb import DataDB
  8 +
  9 +from insight.url import Url
  10 +import time
  11 +
  12 +
  13 +if __name__=='__main__':
  14 +
  15 + token = Token()
  16 + contents_list = []
  17 +
  18 +
  19 + contents_list.append({"id": "467342726674321_1159260310815889"})
  20 +
  21 +
  22 +
  23 + for content in contents_list:
  24 + datadb = DataDB()
  25 + time.sleep(1)
  26 + p_id = str(content['id'].split('_')[0])
  27 + c_id = str(content['id'].split('_')[1])
  28 +
  29 + insight = PostInsight(token)
  30 + insight.setContentInsight(content['id'])
  31 +
  32 + created_time = insight.getContentCreated_time()
  33 + message = insight.getContentMessage()
  34 + message_url = Url().getText2bitly(message)
  35 +
  36 + lists =["post_story_adds_unique",
  37 + "post_story_adds",
  38 + "post_story_adds_by_action_type_unique,comment",
  39 + "post_story_adds_by_action_type_unique,like",
  40 + "post_story_adds_by_action_type_unique,share",
  41 + "post_story_adds_by_action_type,comment",
  42 + "post_impressions",
  43 + "post_impressions_paid_unique",
  44 + "post_impressions_paid",
  45 + "post_story_adds_by_action_type,like",
  46 + "post_story_adds_by_action_type,share",
  47 + "post_impressions_unique",
  48 + "post_impressions_organic_unique",
  49 + "post_impressions_organic",
  50 + "post_impressions_by_story_type_unique,other",
  51 + "post_impressions_by_story_type,other",
  52 + "post_consumptions_by_type_unique,other clicks",
  53 + "post_consumptions_by_type_unique,photo view",
  54 + "post_consumptions_by_type_unique,video play",
  55 + "post_consumptions_by_type_unique,link clicks",
  56 + "post_consumptions_by_type,other clicks",
  57 + "post_consumptions_by_type,photo view",
  58 + "post_consumptions_by_type,video play",
  59 + "post_consumptions_by_type,link clicks",
  60 + "post_engaged_users",
  61 + "post_video_views",
  62 + "post_video_views_unique",
  63 + "post_video_views_paid",
  64 + "post_video_views_autoplayed",
  65 + "post_video_views_10s",
  66 + "post_video_views_10s_unique",
  67 + "post_video_views_10s_paid",
  68 + "post_video_views_10s_organic",
  69 + "post_video_views_10s_clicked_to_play",
  70 + "post_video_views_10s_autoplayed",
  71 + "post_video_views_10s_sound_on",
  72 + "post_video_views_sound_on",
  73 + "post_video_view_time",
  74 + "post_video_complete_views_organic",
  75 + "post_video_complete_views_paid"]
  76 +
  77 + sqlprefix = "insert into facebook_insights2 ("
  78 + sqlreplace = "REPLACE into facebook_insights2_last ("
  79 + sqlvalues = " values ("
  80 +
  81 + sqlprefix += "`p_id`, "
  82 + sqlprefix += "`c_id`, "
  83 + sqlprefix += "`type`, "
  84 + sqlprefix += "`message`, "
  85 + sqlprefix += "`message_url`, "
  86 + sqlprefix += "`comment_url`, "
  87 + sqlprefix += "`created_time`, "
  88 + sqlprefix += "`loging_time`, "
  89 +
  90 + sqlprefix += "`bit_url`, "
  91 + sqlprefix += "`bit_click`, "
  92 + sqlprefix += "`origin_url`, "
  93 + sqlprefix += "`piki_cid`, "
  94 + sqlprefix += "`rpiki_click`, "
  95 +
  96 + sqlvalues += p_id + ", "
  97 + sqlvalues += c_id + ", "
  98 + sqlvalues += "'" + insight.getContentType() + "', "
  99 + sqlvalues += "'" + message + "', "
  100 + sqlvalues += "'" + message_url + "', "
  101 + sqlvalues += "'" + insight.getContentCommentUrl() + "', "
  102 + sqlvalues += "'" + created_time + "', "
  103 + sqlvalues += "NOW(), "
  104 +
  105 +
  106 +
  107 + url_data = Url().url2dic([insight.getLinkUrl(),message_url,insight.getContentCommentUrl()])[0]
  108 +
  109 + sqlvalues += "'" + url_data['bitly_url'] + "', "
  110 + sqlvalues += url_data['bitly_click'] + ", "
  111 + sqlvalues += "'" + url_data['origin_url'] + "', "
  112 + sqlvalues += url_data['piki_cid'] + ", "
  113 + sqlvalues += url_data['rpiki_click'] + ", "
  114 +
  115 + for list in lists:
  116 + sqlprefix += "`" + list.replace(',','_') + "`, "
  117 + sqlvalues += str(insight.getContentInsightByKey(list)) + ", "
  118 +
  119 + sqlprefix = sqlprefix[:len(sqlprefix)-2] + ")"
  120 + sqlvalues = sqlvalues[:len(sqlvalues)-2] + ");"
  121 +
  122 + print sqlprefix + sqlvalues
  123 +
  124 + datadb.fb_insert(sqlprefix + sqlvalues)
  125 + datadb.fb_insert(sqlreplace + sqlprefix[32:] + sqlvalues)
  126 +
  127 +
  128 + del(datadb)
  129 + del(insight)
... ... @@ -12,7 +12,6 @@
12 12  
13 13 if __name__=='__main__':
14 14  
15   - #token_str = "EAAUTLd5JgaoBAJMeeMXqcdExQ1egUHeBaIgVBCilmiH4K9RNyUt7gSgVZCZAtszWCLEaZCDQpxewhICtFjNRICFPWAqUygshcSsdEZBUeZAyUJkON7bfQ2NFFI5AqifNahzjFT83GkWZCZCZBXO3050XSjFf9HSR0iAZD"
16 15 token = Token()
17 16  
18 17 contexts = Context(token)
19 18  
20 19  
... ... @@ -24,21 +23,15 @@
24 23 time.sleep(1)
25 24 p_id = str(content['id'].split('_')[0])
26 25 c_id = str(content['id'].split('_')[1])
27   - created_time = content['created_time'].replace('T'," ").replace('+0000',"")
28 26  
29   - try:
30   - message = content['message'].replace('\n'," ").replace('\r'," ").replace('\'',"")
31   - except:
32   - message = ""
33   -
34   - try:
35   - message_url = Url().getText2bitly(message)
36   - except:
37   - message_url = ""
38   -
39 27 insight = PostInsight(token)
40 28 insight.setContentInsight(content['id'])
41 29  
  30 + created_time = insight.getContentCreated_time()
  31 + message = insight.getContentMessage()
  32 + message_url = Url().getText2bitly(message)
  33 +
  34 +
42 35 lists =["post_story_adds_unique",
43 36 "post_story_adds",
44 37 "post_story_adds_by_action_type_unique,comment",
... ... @@ -123,7 +116,7 @@
123 116 sqlprefix = sqlprefix[:len(sqlprefix)-2] + ")"
124 117 sqlvalues = sqlvalues[:len(sqlvalues)-2] + ");"
125 118  
126   - #print sqlprefix + sqlvalues
  119 + print sqlprefix + sqlvalues
127 120  
128 121 datadb.fb_insert(sqlprefix + sqlvalues)
129 122 datadb.fb_insert(sqlreplace + sqlprefix[32:] + sqlvalues)