From 3feb8c04556fd6eac8f4c07b8d6f5cf5a7a7b98c Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Wed, 22 Apr 2015 00:16:42 +0200 Subject: [PATCH] Finally convert datecsv to Python3 --- datecsv.py | 29 +++++++++++++++++++++++++++++ datecsv.sh | 15 --------------- tools.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 15 deletions(-) create mode 100755 datecsv.py delete mode 100755 datecsv.sh diff --git a/datecsv.py b/datecsv.py new file mode 100755 index 0000000..9adfe9d --- /dev/null +++ b/datecsv.py @@ -0,0 +1,29 @@ +#!/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) diff --git a/datecsv.sh b/datecsv.sh deleted file mode 100755 index 17af6e3..0000000 --- a/datecsv.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -DATAFILE=Database.db - -SQLITE="sqlite3 -csv -header" - -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 i in $@; - do QUERY="$QUERY, (SELECT COUNT(*) FROM tweets e WHERE SUBSTR(e.timestamp,0,11) = SUBSTR(t.timestamp,0,11) AND LOWER(e.text) LIKE '%${i,,}%') AS '$i'" -done - -QUERY="$QUERY FROM tweets t GROUP BY SUBSTR(t.timestamp,0,11);" - -$SQLITE $DATAFILE "$QUERY" diff --git a/tools.py b/tools.py index ad5d43b..67b5b1e 100644 --- a/tools.py +++ b/tools.py @@ -23,7 +23,48 @@ class dbObject: def GetCursor(self): return self.cur + def isInitialized(self): + try: + self.executeQuery("SELECT * FROM tweets") + return True + except: + return False + + +def dbCheck(path): + if dbInitialized(path): + return True + raise ValueError("Provided database file " + path + " is not initialized. Create it using makedb.py or csvdb.py") + + +def dbHelper(path): + dbCheck(path) + return dbObject(path) + + +def dbInitialized(path): + db = dbObject(path) + return db.isInitialized() + def fileExists(path): return os.path.isfile(path) + +def parseArgs(argv): + args = [] + path = None + nextpath = False + + for a in argv[1:]: + if nextpath: + path = a + nextpath = False + elif a == "-f": + if path != None: + raise ValueError("You can only pass one database file.") + nextpath = True + else: + args += [a] + + return args, path