2015-04-21 21:07:25 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2016-05-30 18:40:21 +00:00
|
|
|
import dbtools
|
2016-05-30 18:58:55 +00:00
|
|
|
import argparse, operator, re, sys
|
2015-04-21 21:07:25 +00:00
|
|
|
|
2016-06-30 10:33:09 +00:00
|
|
|
def getTweets(mode = "@", db = dbtools.dbHelper()):
|
2015-04-21 21:07:25 +00:00
|
|
|
handles = dict()
|
|
|
|
tweets = db.executeQuery("SELECT text FROM tweets")
|
|
|
|
|
|
|
|
for tweet in tweets:
|
2016-02-08 00:55:12 +00:00
|
|
|
for word in tweet[0].lower().split():
|
2015-04-24 23:25:33 +00:00
|
|
|
if word[0] == mode or mode == "":
|
|
|
|
if mode == "":
|
|
|
|
handle = word
|
|
|
|
else:
|
|
|
|
handle = mode + re.split('[\\W]',word[1:])[0].lower()
|
2015-04-21 21:07:25 +00:00
|
|
|
if handle != mode:
|
|
|
|
try:
|
|
|
|
handles[handle] += 1
|
|
|
|
except KeyError:
|
|
|
|
handles[handle] = 1
|
|
|
|
|
|
|
|
return handles
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-05-30 18:58:55 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
g = parser.add_mutually_exclusive_group()
|
|
|
|
g.add_argument("-t", "--hashtags", help="count only #hashtags", action="store_true")
|
|
|
|
g.add_argument("-w", "--words", help="count all words", action="store_true")
|
|
|
|
g.add_argument("-m", "--mentions", help="count only @mentions (default)", action="store_true")
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.hashtags:
|
|
|
|
mode = "#"
|
|
|
|
elif args.words:
|
|
|
|
mode = ""
|
|
|
|
else:
|
|
|
|
mode = "@"
|
|
|
|
|
2016-06-30 10:33:09 +00:00
|
|
|
for handle, tweets in sorted(list(getTweets(mode=mode).items()), key=operator.itemgetter(1), reverse=True):
|
2015-04-21 21:07:25 +00:00
|
|
|
print(handle + "," + str(tweets))
|