2015-04-21 22:16:42 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2016-05-30 18:40:21 +00:00
|
|
|
import dbtools, setuptools
|
2015-04-24 21:33:42 +00:00
|
|
|
import sys, datetime
|
2015-04-21 22:16:42 +00:00
|
|
|
|
2015-04-27 21:43:36 +00:00
|
|
|
def dateArgs(argv = sys.argv[1:]):
|
2015-04-22 22:57:24 +00:00
|
|
|
strings = []
|
2015-04-27 21:43:36 +00:00
|
|
|
fr = None
|
|
|
|
to = None
|
|
|
|
av = 0
|
2015-04-22 22:57:24 +00:00
|
|
|
|
|
|
|
mode = 0
|
|
|
|
|
2015-04-27 21:43:36 +00:00
|
|
|
for arg in argv:
|
2015-04-28 19:56:42 +00:00
|
|
|
if mode == 0:
|
2015-04-22 22:57:24 +00:00
|
|
|
if arg == "-f":
|
|
|
|
mode = 1
|
|
|
|
elif arg == "-t":
|
|
|
|
mode = 2
|
2015-04-27 21:43:36 +00:00
|
|
|
elif arg == "-a":
|
|
|
|
mode = 3
|
2015-04-22 22:57:24 +00:00
|
|
|
else:
|
|
|
|
strings += [arg]
|
|
|
|
mode = 0
|
2015-04-27 21:43:36 +00:00
|
|
|
elif mode == 3:
|
2015-04-28 19:56:42 +00:00
|
|
|
try:
|
|
|
|
av = int(arg)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError("Number of days for running average must be an integer.")
|
2015-04-27 21:43:36 +00:00
|
|
|
mode = 0
|
|
|
|
elif mode == 1:
|
2016-05-30 18:40:21 +00:00
|
|
|
fr = setuptools.getDate(arg)
|
2015-04-22 22:57:24 +00:00
|
|
|
mode = 0
|
|
|
|
else:
|
2016-05-30 18:40:21 +00:00
|
|
|
to = setuptools.getDate(arg)
|
2015-04-27 21:43:36 +00:00
|
|
|
mode = 0
|
2015-04-22 22:57:24 +00:00
|
|
|
|
2015-04-27 21:43:36 +00:00
|
|
|
if mode in (1, 2):
|
2015-04-22 22:57:24 +00:00
|
|
|
raise ValueError("Date missing.")
|
2015-04-27 21:43:36 +00:00
|
|
|
|
|
|
|
if to != None and fr != None and to < fr:
|
|
|
|
raise ValueError("From date must be before To date.")
|
2015-04-28 19:56:42 +00:00
|
|
|
|
2015-04-27 21:43:36 +00:00
|
|
|
return strings, fr, to, av
|
2015-04-22 22:57:24 +00:00
|
|
|
|
|
|
|
|
2015-04-27 21:43:36 +00:00
|
|
|
def queryBuilder(date, string = ""):
|
|
|
|
return "SELECT COUNT(*) FROM tweets WHERE SUBSTR(timestamp,0,11) = '%s' AND LOWER(text) LIKE '%%%s%%'" % (date, string)
|
2015-04-22 22:57:24 +00:00
|
|
|
|
2015-04-21 22:16:42 +00:00
|
|
|
|
2015-04-27 21:43:36 +00:00
|
|
|
def dateList(fr, to):
|
|
|
|
return [[(fr+datetime.timedelta(days=i)).strftime('%Y-%m-%d')] for i in range((to+datetime.timedelta(days=1)-fr).days)]
|
|
|
|
|
|
|
|
|
2015-04-28 19:56:42 +00:00
|
|
|
def avg(list):
|
|
|
|
sum = 0
|
|
|
|
|
|
|
|
for i in list:
|
|
|
|
sum += i
|
|
|
|
|
|
|
|
return int(sum / len(list))
|
|
|
|
|
|
|
|
|
|
|
|
def fillAverage(cur, av):
|
|
|
|
vals = []
|
|
|
|
for day in cur:
|
|
|
|
vals = ([day[len(day)-1]] + vals)[:av]
|
|
|
|
day += [avg(vals)]
|
|
|
|
return cur
|
|
|
|
|
|
|
|
|
2015-04-27 21:43:36 +00:00
|
|
|
def fillList(db, string, cur, av):
|
|
|
|
for day in cur:
|
2015-04-28 08:41:49 +00:00
|
|
|
day += list(db.executeQuery(queryBuilder(day[0], string)))[0]
|
2015-04-27 21:43:36 +00:00
|
|
|
|
2015-04-28 19:56:42 +00:00
|
|
|
if not av == 0:
|
|
|
|
cur = fillAverage(cur, av)
|
|
|
|
|
2015-04-27 21:43:36 +00:00
|
|
|
return cur
|
2015-04-22 22:57:24 +00:00
|
|
|
|
|
|
|
|
2015-04-27 21:43:36 +00:00
|
|
|
def getHeaders(strings, av):
|
2015-04-28 19:56:42 +00:00
|
|
|
if av == 0:
|
|
|
|
return [["Date", "Tweets"] + [string for string in strings]]
|
|
|
|
|
|
|
|
headers = ["Date", "Tweets", "Average"]
|
|
|
|
|
|
|
|
for string in strings:
|
|
|
|
headers += [string, "Average " + string]
|
|
|
|
|
|
|
|
return [headers]
|
2015-04-27 21:43:36 +00:00
|
|
|
|
|
|
|
|
2016-06-30 10:33:09 +00:00
|
|
|
def getTweetsByDate(strings = [], fr = None, to = None, av = 0, db = dbtools.dbHelper(), headers = False):
|
2015-04-27 21:43:36 +00:00
|
|
|
if fr == None:
|
2015-05-14 14:29:55 +00:00
|
|
|
fr = db.getFLDate()
|
2015-04-27 21:43:36 +00:00
|
|
|
if to == None:
|
2015-05-14 14:29:55 +00:00
|
|
|
to = db.getFLDate(1)
|
2015-04-27 21:43:36 +00:00
|
|
|
|
|
|
|
cur = dateList(fr, to)
|
|
|
|
|
|
|
|
for string in [""] + strings:
|
|
|
|
cur = fillList(db, string, cur, av)
|
|
|
|
|
|
|
|
if headers:
|
|
|
|
cur = getHeaders(strings, av) + cur
|
|
|
|
|
|
|
|
return cur
|
2015-04-21 22:16:42 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-05-30 18:40:21 +00:00
|
|
|
setuptools.printCSV(getTweetsByDate(*dateArgs(), headers = True))
|