twitools/datecsv.py

79 lines
1.7 KiB
Python
Raw Normal View History

2015-04-21 22:16:42 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tools
import sys, datetime
2015-04-21 22:16:42 +00:00
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 = ""):
2015-04-21 22:16:42 +00:00
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'"
2015-04-21 22:16:42 +00:00
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
2015-04-21 22:16:42 +00:00
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 = ""):
2015-04-21 22:16:42 +00:00
db = tools.dbHelper(path)
return db.executeQuery(queryBuilder(strings,fr,to))
2015-04-21 22:16:42 +00:00
if __name__ == "__main__":
strings, fr, to = dateArgs()
tools.printCSV(getTweetsByDate(strings = strings, fr = fr, to = to))