Move common functions to tools.py
Move gethandles and gethashtags into getmentions Make getmentions and makedb use tools
This commit is contained in:
parent
2cc64919fd
commit
19ae6e4a31
|
@ -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))
|
41
getmentions.py
Executable file
41
getmentions.py
Executable file
|
@ -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))
|
17
makedb.py
17
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:
|
||||
|
|
29
tools.py
Normal file
29
tools.py
Normal file
|
@ -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)
|
||||
|
Loading…
Reference in a new issue