runmain.py 4.59 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
 121
 122
 123
 124
 125
 126
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from insight.context import Context
from insight.token import Token
from insight.postinsight import PostInsight
from insight.datadb import DataDB

from insight.url import Url
import time


if __name__=='__main__':

token = Token()

contexts = Context(token)
contexts.setContextsFeedAndTimestemp(3600 * 24 * 10)
contents_list = contexts.getContentsList()

for content in contents_list:
datadb = DataDB()
time.sleep(1)
p_id = str(content['id'].split('_')[0])
c_id = str(content['id'].split('_')[1])

insight = PostInsight(token)
insight.setContentInsight(content['id'])

created_time = insight.getContentCreated_time()
message = insight.getContentMessage()
message_url = Url().getText2bitly(message)


lists =["post_story_adds_unique",
"post_story_adds",
"post_story_adds_by_action_type_unique,comment",
"post_story_adds_by_action_type_unique,like",
"post_story_adds_by_action_type_unique,share",
"post_story_adds_by_action_type,comment",
"post_impressions",
"post_impressions_paid_unique",
"post_impressions_paid",
"post_story_adds_by_action_type,like",
"post_story_adds_by_action_type,share",
"post_impressions_unique",
"post_impressions_organic_unique",
"post_impressions_organic",
"post_impressions_by_story_type_unique,other",
"post_impressions_by_story_type,other",
"post_consumptions_by_type_unique,other clicks",
"post_consumptions_by_type_unique,photo view",
"post_consumptions_by_type_unique,video play",
"post_consumptions_by_type_unique,link clicks",
"post_consumptions_by_type,other clicks",
"post_consumptions_by_type,photo view",
"post_consumptions_by_type,video play",
"post_consumptions_by_type,link clicks",
"post_engaged_users",
"post_video_views",
"post_video_views_unique",
"post_video_views_paid",
"post_video_views_autoplayed",
"post_video_views_10s",
"post_video_views_10s_unique",
"post_video_views_10s_paid",
"post_video_views_10s_organic",
"post_video_views_10s_clicked_to_play",
"post_video_views_10s_autoplayed",
"post_video_views_10s_sound_on",
"post_video_views_sound_on",
"post_video_view_time",
"post_video_complete_views_organic",
"post_video_complete_views_paid"]

sqlprefix = "insert into facebook_insights2 ("
sqlreplace = "REPLACE into facebook_insights2_last ("
sqlvalues = " values ("

sqlprefix += "`p_id`, "
sqlprefix += "`c_id`, "
sqlprefix += "`type`, "
sqlprefix += "`message`, "
sqlprefix += "`message_url`, "
sqlprefix += "`comment_url`, "
sqlprefix += "`created_time`, "
sqlprefix += "`loging_time`, "

sqlprefix += "`bit_url`, "
sqlprefix += "`bit_click`, "
sqlprefix += "`origin_url`, "
sqlprefix += "`piki_cid`, "
sqlprefix += "`rpiki_click`, "

sqlvalues += p_id + ", "
sqlvalues += c_id + ", "
sqlvalues += "'" + insight.getContentType() + "', "
sqlvalues += "'" + message + "', "
sqlvalues += "'" + message_url + "', "
sqlvalues += "'" + insight.getContentCommentUrl() + "', "
sqlvalues += "'" + created_time + "', "
sqlvalues += "NOW(), "

url_data = Url().url2dic([insight.getLinkUrl(),message_url,insight.getContentCommentUrl()])[0]

sqlvalues += "'" + url_data['bitly_url'] + "', "
sqlvalues += url_data['bitly_click'] + ", "
sqlvalues += "'" + url_data['origin_url'] + "', "
sqlvalues += url_data['piki_cid'] + ", "
sqlvalues += url_data['rpiki_click'] + ", "

for list in lists:
sqlprefix += "`" + list.replace(',','_') + "`, "
sqlvalues += str(insight.getContentInsightByKey(list)) + ", "

sqlprefix = sqlprefix[:len(sqlprefix)-2] + ")"
sqlvalues = sqlvalues[:len(sqlvalues)-2] + ");"

#print sqlprefix + sqlvalues

datadb.fb_insert(sqlprefix + sqlvalues)
datadb.fb_insert(sqlreplace + sqlprefix[32:] + sqlvalues)


del(datadb)
del(insight)