From 19ae6e4a3112fec0a7f8e45a4a204e70bcf7e25b Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 21 Apr 2015 23:07:25 +0200 Subject: [PATCH] Move common functions to tools.py Move gethandles and gethashtags into getmentions Make getmentions and makedb use tools --- gethandles.py | 27 --------------------------- getmentions.py | 41 +++++++++++++++++++++++++++++++++++++++++ makedb.py | 17 +++++++++-------- tools.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 35 deletions(-) delete mode 100755 gethandles.py create mode 100755 getmentions.py create mode 100644 tools.py diff --git a/gethandles.py b/gethandles.py deleted file mode 100755 index 45c5828..0000000 --- a/gethandles.py +++ /dev/null @@ -1,27 +0,0 @@ -#!/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)) diff --git a/getmentions.py b/getmentions.py new file mode 100755 index 0000000..f8b0668 --- /dev/null +++ b/getmentions.py @@ -0,0 +1,41 @@ +#!/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)) diff --git a/makedb.py b/makedb.py index 712ab8a..59646d5 100755 --- a/makedb.py +++ b/makedb.py @@ -1,19 +1,20 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import config, os, sqlite3, sys +import tools -def makeDB(path=config.dbpath): - if os.path.isfile(path): +import sys + +def makeDB(path=tools.config.dbpath): + if tools.fileExists(path): raise IOError(path + " already exists. If you want to recreate it, please delete it first, or provide a different file name.") - conn = sqlite3.connect(path) - cur = conn.cursor() + db = tools.dbObject(path) - cur.execute("CREATE TABLE tweets(`tweet_id` INTEGER NOT NULL, `in_reply_to_status_id` TEXT, `in_reply_to_user_id` TEXT, `timestamp` TEXT, `source` TEXT, `text` TEXT, `retweeted_status_id` TEXT, `retweeted_status_user_id` TEXT, `retweeted_status_timestamp` TEXT, `expanded_urls` TEXT, PRIMARY KEY(tweet_id));") + db.executeQuery("CREATE TABLE tweets(`tweet_id` INTEGER NOT NULL, `in_reply_to_status_id` TEXT, `in_reply_to_user_id` TEXT, `timestamp` TEXT, `source` TEXT, `text` TEXT, `retweeted_status_id` TEXT, `retweeted_status_user_id` TEXT, `retweeted_status_timestamp` TEXT, `expanded_urls` TEXT, PRIMARY KEY(tweet_id));") - conn.commit() - conn.close() + db.commit() + db.closeConnection() if __name__ == "__main__": try: diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..ad5d43b --- /dev/null +++ b/tools.py @@ -0,0 +1,29 @@ +import config + +import os, sqlite3 + +class dbObject: + + def __init__(self, path=config.dbpath): + self.conn = sqlite3.connect(path) + self.cur = self.conn.cursor() + + def closeConnection(self): + return self.conn.close() + + def commit(self): + return self.conn.commit() + + def executeQuery(self, query): + return self.cur.execute(query) + + def GetConnection(self): + return self.conn + + def GetCursor(self): + return self.cur + + +def fileExists(path): + return os.path.isfile(path) +