twitools/getmentions.py

41 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import dbtools
import argparse, operator, re, sys
def getTweets(mode = "@", db = dbtools.dbObject()):
handles = dict()
tweets = db.executeQuery("SELECT text FROM tweets")
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__":
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 = "@"
for handle, tweets in sorted(list(getTweets(mode,dbtools.dbObject()).items()), key=operator.itemgetter(1), reverse=True):
print(handle + "," + str(tweets))