twitools/datecsv.py
2015-04-22 00:16:42 +02:00

30 lines
902 B
Python
Executable file

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tools
import csv, sys
def queryBuilder(strings = []):
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 + "'"
return query + "FROM tweets t GROUP BY SUBSTR(t.timestamp,0,11)"
def getTweetsByDate(strings = [], path = tools.config.dbpath):
db = tools.dbHelper(path)
tweets = db.executeQuery(queryBuilder(strings))
writer = csv.writer(sys.stdout)
for day in tweets:
writer.writerow(day)
if __name__ == "__main__":
strings, path = tools.parseArgs(sys.argv)
if path == None:
path = tools.config.dbpath
getTweetsByDate(strings, path)