twitools/extras/challenge.py
2016-12-06 22:25:24 +01:00

46 lines
1.1 KiB
Python
Executable file

#!/usr/bin/env python3
import tools
import operator, re, sys
def getTweets(path, mon, mode = "@"):
db = tools.dbHelper(path)
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 = "@"
path = tools.dbpath()
mon = "2016-03"
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(path,mon,mode).items()), key=operator.itemgetter(1), reverse=True):
print(handle + "," + str(tweets))