twitools/gethandles.py

28 lines
700 B
Python
Executable file

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import config, operator, re, sqlite3
def getTweets(database_filename = config.dbpath):
sql_conn = sqlite3.connect(database_filename)
cur = sql_conn.cursor()
handles = dict()
tweets = cur.execute("SELECT text FROM tweets")
for tweet in tweets:
for word in tweet[0].split():
if word[0] == "@":
handle = "@" + re.split('[\\W]',word[1:])[0].lower()
if handle != "@":
try:
handles[handle] += 1
except KeyError:
handles[handle] = 1
return handles
if __name__ == "__main__":
for handle, tweets in sorted(list(getTweets().items()), key=operator.itemgetter(1), reverse=True):
print(handle + "," + str(tweets))