twitools/challenge.py

45 lines
1.1 KiB
Python
Raw Normal View History

2016-12-06 21:25:24 +00:00
#!/usr/bin/env python3
2016-12-07 13:12:26 +00:00
import dbtools, setuptools
2016-12-06 21:25:24 +00:00
import operator, re, sys
2016-12-07 13:12:26 +00:00
def getTweets(mon, mode = "@", db = dbtools.dbHelper()):
2016-12-06 21:25:24 +00:00
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))
2016-12-07 13:12:26 +00:00
2016-12-06 21:25:24 +00:00
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 = "@"
2016-12-07 13:12:26 +00:00
mon = "2016-07"
2016-12-06 21:25:24 +00:00
2016-12-07 13:12:26 +00:00
if len(sys.argv) > 1:
2016-12-06 21:25:24 +00:00
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
2016-12-07 13:12:26 +00:00
for handle, tweets in sorted(list(getTweets(mon,mode).items()), key=operator.itemgetter(1), reverse=True):
2016-12-06 21:25:24 +00:00
print(handle + "," + str(tweets))
2016-12-07 13:12:26 +00:00