runpage.py 2.42 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from insight.context import Context
from insight.token import Token
from insight.pageinsight import PageInsight
from insight.datadb import DataDB
import time

if __name__=='__main__':

#token_str = "CAAUTLd5JgaoBABnBUUgOjgaUkuviOZCC1otVIJYwYapCRTZBU8ZAbTOd1uo1SBiqHr1eBhVdzoYDoBjPzqO0ImpxGZAXtlXeVtp4r2muUDt3O53NLYqFgrJqKAowcU7fWMsmMTZAAtKT8MXeF4Lb0rHQ2I1gHmbr6nlJWBHzk6dsNPmYHguMp"
#token_str = "EAAUTLd5JgaoBAJMeeMXqcdExQ1egUHeBaIgVBCilmiH4K9RNyUt7gSgVZCZAtszWCLEaZCDQpxewhICtFjNRICFPWAqUygshcSsdEZBUeZAyUJkON7bfQ2NFFI5AqifNahzjFT83GkWZCZCZBXO3050XSjFf9HSR0iAZD"
token = Token()

context = Context(token)
page_list = context.getPageList()

page_item = ["page_fans/lifetime",
"page_impressions_unique/day",
"page_engaged_users/day",
"page_storytellers_by_story_type/day",
"page_impressions_unique/week",
"page_engaged_users/week",
"page_storytellers_by_story_type/week"]

page_dscp = {'page_fans/lifetime': 'Lifetime_Total_Likes',
'page_impressions_unique/day': 'Daily_Total_Reach',
'page_engaged_users/day': 'Daily_Page_Engaged_Users',
'page_storytellers_by_story_type/day': 'Daily_Talking_About_This_By_Story_Type',
'page_impressions_unique/week': 'Weekly_Total_Reach',
'page_engaged_users/week': 'Weekly_Page_Engaged_Users',
'page_storytellers_by_story_type/week': 'Weekly_Talking_About_This_By_Story_Type'}

for page_id in page_list:

datadb = DataDB()

page_insight = PageInsight(token)
page_report = page_insight.getPageInsight(page_id, page_item)

sqlprefix = "insert into facebook_page_insight ("
sqlvalues = " values ("

sqlprefix += "`p_id`, "
sqlprefix += "`loging_time`, "

sqlvalues += page_id + ", "
sqlvalues += "NOW(), "

for item in page_item:

sqlprefix += "`" + page_dscp[item].lower() + "`, "
sqlvalues += str(page_insight.getPageInsightByKey(page_dscp[item])) + ", "

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

print sqlprefix + sqlvalues

# datadb.fb_insert(sqlprefix + sqlvalues)

del(datadb)
del(page_insight)

print page_id + " done"

print time.time()