From a07b6cd653cef950ca7b68f25e95d33ef09c1f43 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 6 Dec 2016 22:25:24 +0100 Subject: [PATCH] Add #mentionchallenge tool --- extras/challenge.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 extras/challenge.py diff --git a/extras/challenge.py b/extras/challenge.py new file mode 100755 index 0000000..2fa4677 --- /dev/null +++ b/extras/challenge.py @@ -0,0 +1,46 @@ +#!/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))