Finally convert datecsv to Python3
This commit is contained in:
parent
19ae6e4a31
commit
3feb8c0455
29
datecsv.py
Executable file
29
datecsv.py
Executable file
|
@ -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)
|
15
datecsv.sh
15
datecsv.sh
|
@ -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"
|
41
tools.py
41
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
|
||||
|
|
Loading…
Reference in a new issue