Commit 43a5084e4d319be511ab0321aaa29736682168a0

Authored by Noah ago
1 parent 9c565ef7ef
Exists in master

dau insert

Showing 2 changed files with 58 additions and 4 deletions Side-by-side Diff

  1 +# -*- coding: utf-8 -*-
  2 +import MySQLdb
  3 +
  4 +class DataDB():
  5 + def __init__(self):
  6 + self.init_database()
  7 +
  8 + def init_database(self):
  9 + #mysqlHost = '192.168.20.61'
  10 + mysqlHost = 'data2.piki.work'
  11 + UserName = 'pikids'
  12 + mysqlPassword = 'zmfflr2715'
  13 + DBName = 'piki_data_dashboard'
  14 + self.db = MySQLdb.connect(host=mysqlHost, user=UserName, passwd=mysqlPassword, db=DBName)
  15 + self.db.set_character_set('utf8')
  16 + #charset = self.db.character_set_name()
  17 + self.cursor = self.db.cursor(MySQLdb.cursors.DictCursor)
  18 +
  19 + def ga_insert(self,sql):
  20 + #print sql
  21 + self.cursor.execute(sql)
  22 + try :
  23 + self.db.commit()
  24 + except Exception, e:
  25 + print e
  26 + return -1
  27 + return
... ... @@ -5,11 +5,13 @@
5 5 from oauth2client import client
6 6 from oauth2client import file
7 7 from oauth2client import tools
  8 +from datadb import DataDB
  9 +import datetime
8 10  
9 11 SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
10 12 DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest')
11 13 CLIENT_SECRETS_PATH = './cli.json' # Path to client_secrets.json file.
12   -VIEW_ID = '84815165'
  14 +VIEW_ID = '117918888'
13 15  
14 16  
15 17 def initialize_analyticsreporting():
... ... @@ -49,7 +51,8 @@
49 51 {
50 52 'viewId': VIEW_ID,
51 53 #'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
52   - 'dateRanges': [{'startDate': '2016-06-01', 'endDate': 'today'}],
  54 + #'dateRanges': [{'startDate': '2016-06-01', 'endDate': 'today'}],
  55 + 'dateRanges': [{'startDate': 'yesterday', 'endDate': 'yesterday'}],
53 56 'metrics': [{'expression': 'ga:Users'}]
54 57 }]
55 58 }
56 59  
57 60  
58 61  
... ... @@ -77,12 +80,36 @@
77 80 for metricHeader, value in zip(metricHeaders, values.get('values')):
78 81 print metricHeader.get('name') + ': ' + value
79 82  
  83 +def return_response(response):
  84 + for report in response.get('reports', []):
  85 + columnHeader = report.get('columnHeader', {})
  86 + dimensionHeaders = columnHeader.get('dimensions', [])
  87 + metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
  88 + rows = report.get('data', {}).get('rows', [])
80 89  
81   -def main():
  90 + for row in rows:
  91 + dimensions = row.get('dimensions', [])
  92 + dateRangeValues = row.get('metrics', [])
82 93  
  94 + for header, dimension in zip(dimensionHeaders, dimensions):
  95 + print header + ': ' + dimension
  96 +
  97 + for i, values in enumerate(dateRangeValues):
  98 + print 'Date range (' + str(i) + ')'
  99 + for metricHeader, value in zip(metricHeaders, values.get('values')):
  100 + return metricHeader.get('name') + ': ' + value
  101 +
  102 +
  103 +def main():
83 104 analytics = initialize_analyticsreporting()
84 105 response = get_report(analytics)
85   - print_response(response)
  106 + val = return_response(response)
  107 +
  108 + dau = int(val.split(":")[2])
  109 + yesterday = str(datetime.date.today()+datetime.timedelta(days=-1))
  110 + datadb = DataDB()
  111 + sql = "insert into web_dau (`cdate`, `dau`) values ('%s', %d);"%(yesterday,dau)
  112 + datadb.ga_insert(sql)
86 113  
87 114 if __name__ == '__main__':
88 115 main()