2015-04-21 22:16:42 +00:00
#!/usr/bin/env python3
import tools
2015-04-24 21:33:42 +00:00
import sys , datetime
2015-04-21 22:16:42 +00:00
2015-04-22 22:57:24 +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-22 22:57:24 +00:00
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 + " ' "
2015-04-22 22:57:24 +00:00
query + = " FROM tweets t "
status = 0
2015-04-21 22:16:42 +00:00
2015-04-22 22:57:24 +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 )
2015-04-24 21:33:42 +00:00
return db . executeQuery ( queryBuilder ( strings , fr , to ) )
2015-04-21 22:16:42 +00:00
if __name__ == " __main__ " :
2015-04-22 22:57:24 +00:00
strings , fr , to = dateArgs ( )
2015-04-24 22:28:20 +00:00
tools . printCSV ( [ [ " Date " , " Tweets " ] + [ s for s in strings ] ] )
2015-04-24 21:33:42 +00:00
tools . printCSV ( getTweetsByDate ( strings = strings , fr = fr , to = to ) )