-
Notifications
You must be signed in to change notification settings - Fork 0
/
monthly.py
executable file
·66 lines (47 loc) · 1.68 KB
/
monthly.py
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
#!/usr/bin/python
from query import *
from bs4 import BeautifulSoup
import urllib2
from datetime import date, datetime, timedelta
import json
import MySQLdb as mdb
import sys
from dateutil.relativedelta import relativedelta
import config
def monthly(start_date, end_date):
tables = ['AgeGroupGender', 'MonthlyViews', 'VidSharingService']
print "Ran on: ", now
con = None
try:
con = mdb.connect(**config.mysql_params)
cur = con.cursor()
videos = getVideoList()
while (start_date <= end_date):
for table in tables:
if table == 'AgeGroupGender' or table == 'MonthlyViews':
if table == 'MonthlyViews':
end = start_date
else:
end = start_date+relativedelta(months = +1)+relativedelta(days = -1)
response, columns = query(table, "", start_date, end)
insert_statement = table_options[table]['insert']
for row in response:
cur.execute(insert_statement, getValues(table, "", start_date, columns, row))
con.commit()
else:
for video in videos:
end = start_date+relativedelta(months = +1)+relativedelta(days = -1)
response, columns = query(table, video, start_date, end)
insert_statement = table_options[table]['insert']
for row in response:
cur.execute(insert_statement, getValues(table, video, start_date, columns, row))
con.commit()
start_date = start_date+relativedelta(months = +1)
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
finally:
if con:
con.close()
#lifetime_start = datetime(2011, 02, 1)
monthly(first_day_last_month, last_day_last_month)