twitools/getdates.py
Klaus-Uwe Mitterer aad1531392 Futher preparation for MySQL, which is not working yet though...
Make filler handle direct messages
Add table and functions for direct messages
2016-06-30 12:33:09 +02:00

111 lines
2.1 KiB
Python
Executable file

#!/usr/bin/env python3
import dbtools, setuptools
import sys, datetime
def dateArgs(argv = sys.argv[1:]):
strings = []
fr = None
to = None
av = 0
mode = 0
for arg in argv:
if mode == 0:
if arg == "-f":
mode = 1
elif arg == "-t":
mode = 2
elif arg == "-a":
mode = 3
else:
strings += [arg]
mode = 0
elif mode == 3:
try:
av = int(arg)
except ValueError:
raise ValueError("Number of days for running average must be an integer.")
mode = 0
elif mode == 1:
fr = setuptools.getDate(arg)
mode = 0
else:
to = setuptools.getDate(arg)
mode = 0
if mode in (1, 2):
raise ValueError("Date missing.")
if to != None and fr != None and to < fr:
raise ValueError("From date must be before To date.")
return strings, fr, to, av
def queryBuilder(date, string = ""):
return "SELECT COUNT(*) FROM tweets WHERE SUBSTR(timestamp,0,11) = '%s' AND LOWER(text) LIKE '%%%s%%'" % (date, string)
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)]
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
def fillList(db, string, cur, av):
for day in cur:
day += list(db.executeQuery(queryBuilder(day[0], string)))[0]
if not av == 0:
cur = fillAverage(cur, av)
return cur
def getHeaders(strings, av):
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]
def getTweetsByDate(strings = [], fr = None, to = None, av = 0, db = dbtools.dbHelper(), headers = False):
if fr == None:
fr = db.getFLDate()
if to == None:
to = db.getFLDate(1)
cur = dateList(fr, to)
for string in [""] + strings:
cur = fillList(db, string, cur, av)
if headers:
cur = getHeaders(strings, av) + cur
return cur
if __name__ == "__main__":
setuptools.printCSV(getTweetsByDate(*dateArgs(), headers = True))