twitools/getmentions.py

46 lines
1 KiB
Python
Executable file

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tools
import operator, re, sys
def getTweets(mode = "@", path = tools.config.dbpath):
db = tools.dbHelper(path)
handles = dict()
tweets = db.executeQuery("SELECT text FROM tweets")
for tweet in tweets:
for word in tweet[0].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.config.dbpath
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:
path = arg
for handle, tweets in sorted(list(getTweets(mode,path).items()), key=operator.itemgetter(1), reverse=True):
print(handle + "," + str(tweets))