twitools/extras/challenge.py
2016-12-07 14:12:26 +01:00

44 lines
1.1 KiB
Python
Executable file

#!/usr/bin/env python3
import dbtools, setuptools
import operator, re, sys
def getTweets(mon, mode = "@", db = dbtools.dbHelper()):
handles = dict()
tweets = db.executeQuery("SELECT text FROM tweets WHERE SUBSTR(timestamp,0,11)>='%s-01' AND SUBSTR(timestamp,0,11)<='%s-31'" % (mon, mon))
for tweet in tweets:
for word in tweet[0].lower().split():
if word[0] == mode or mode == "":
if mode == "":
handle = word
else:
handle = mode + re.split('[\\W]',word[1:])[0].lower()
if handle != mode:
try:
handles[handle] += 1
except KeyError:
handles[handle] = 1
return handles
if __name__ == "__main__":
mode = "@"
mon = "2016-07"
if len(sys.argv) > 1:
if len(sys.argv) > 3 or (len(sys.argv) == 3 and "-h" not in sys.argv):
raise ValueError("Invalid arguments passed.")
for arg in sys.argv[1:]:
if arg == "-h":
mode = "#"
if arg == "-w":
mode = ""
else:
mon = arg
for handle, tweets in sorted(list(getTweets(mon,mode).items()), key=operator.itemgetter(1), reverse=True):
print(handle + "," + str(tweets))