twitools/getmentions.py
Klaus-Uwe Mitterer 19ae6e4a31 Move common functions to tools.py
Move gethandles and gethashtags into getmentions
Make getmentions and makedb use tools
2015-04-21 23:07:25 +02:00

42 lines
952 B
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.dbObject(path)
handles = dict()
tweets = db.executeQuery("SELECT text FROM tweets")
for tweet in tweets:
for word in tweet[0].split():
if word[0] == mode:
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 = "#"
else:
path = arg
for handle, tweets in sorted(list(getTweets(mode,path).items()), key=operator.itemgetter(1), reverse=True):
print(handle + "," + str(tweets))