twitools/getdates.py
2015-04-27 17:29:37 +02:00

80 lines
1.8 KiB
Python
Executable file

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tools
import sys, datetime
def checkDate(date):
try:
datetime.datetime.strptime(date, '%Y-%m-%d')
return True
except ValueError:
return False
def dateArgs(argv = sys.argv):
fr = ""
to = ""
strings = []
mode = 0
for arg in argv[1:]:
if mode == 0:
if arg == "-f":
mode = 1
elif arg == "-t":
mode = 2
else:
strings += [arg]
elif checkDate(arg):
if mode == 1:
fr = arg
mode = 0
else:
to = arg
mode = 0
else:
raise ValueError("Dates must be in YYYY-MM-DD format.")
if not mode == 0:
raise ValueError("Date missing.")
return strings, fr, to
def queryBuilder(strings = [], fr = "", to = ""):
query = "SELECT SUBSTR(t.timestamp,0,11) AS 'Date', (SELECT COUNT(*) FROM tweets e WHERE SUBSTR(e.timestamp,0,11) = SUBSTR(t.timestamp,0,11)) AS 'Tweets'"
for string in strings:
query += ", (SELECT COUNT(*) FROM tweets e WHERE SUBSTR(e.timestamp,0,11) = SUBSTR(t.timestamp,0,11) AND LOWER(e.text) LIKE '%" + string.lower() + "%') AS '" + string + "'"
query += "FROM tweets t "
status = 0
if not fr == "":
status += 1
if not to == "":
status += 2
if status > 0:
query += "WHERE "
if status in (1,3):
query += "SUBSTR(t.timestamp,0,11) >= '" + fr + "' "
if status == 3:
query += "AND "
if status > 1:
query += "SUBSTR(t.timestamp,0,11) <= '" + to + "' "
return query + "GROUP BY SUBSTR(t.timestamp,0,11)"
def getTweetsByDate(strings = [], path = tools.config.dbpath, fr = "", to = ""):
db = tools.dbHelper(path)
return db.executeQuery(queryBuilder(strings,fr,to))
if __name__ == "__main__":
strings, fr, to = dateArgs()
tools.printCSV([["Date", "Tweets"] + [s for s in strings]])
tools.printCSV(getTweetsByDate(strings = strings, fr = fr, to = to))