From 7af8c609d08cc4385e67400535181c382f334059 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 11 Oct 2015 00:10:57 +0200 Subject: [PATCH] Changed config system --- config.py | 11 --------- csvdb.py | 42 +++++++++++++++++++++-------------- filler.py | 2 +- followertxt.py | 4 ++-- getdates.py | 2 +- getmentions.py | 4 ++-- gettweets.py | 2 +- makedb.py | 2 +- tools.py | 60 +++++++++++++++++++++++++++++++++++++++++++++----- tweleter.py | 4 ++-- 10 files changed, 90 insertions(+), 43 deletions(-) delete mode 100644 config.py diff --git a/config.py b/config.py deleted file mode 100644 index 1bef04c..0000000 --- a/config.py +++ /dev/null @@ -1,11 +0,0 @@ -dbpath = "Database.db" # Path to the database file - -user = "User Name" # Your twitter handle (without the @) - -''' Details about your app ''' - -cke = "Consumer Key" -cse = "Consumer Secret" -ato = "Access Token" -ase = "Access Secret" - diff --git a/csvdb.py b/csvdb.py index e613942..15b6a0a 100755 --- a/csvdb.py +++ b/csvdb.py @@ -4,22 +4,32 @@ import tools import sqlite3, csv -try: - infile = open('tweets.csv') -except IOError: - raise IOError("Please make sure that the tweets.csv from the Twitter download is located in this directory.") +def makeDB(path=tools.dbpath()): + try: + infile = open('tweets.csv') + except IOError: + raise IOError("Please make sure that the tweets.csv from the Twitter download is located in this directory.") + + input = list(csv.reader(infile)) + + conn = sqlite3.connect(path) + cur = conn.cursor() + + try: + 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));") + except sqlite3.OperationalError: + raise IOError("%s already exists. Please delete it before trying to create a new one." % path) -input = list(csv.reader(infile)) + for row in input[1:]: + cur.execute("INSERT INTO tweets VALUES(" + row[0].replace("'","''") + ",'" + row[1].replace("'","''") + "','" + row[2].replace("'","''") + "','" + row[3].replace("'","''") + "','" + row[4].replace("'","''") + "','" + row[5].replace("'","''") + "','" + row[6].replace("'","''") + "','" + row[7].replace("'","''") + "','" + row[8].replace("'","''") + "','" + row[9].replace("'","''") + "');") + + conn.commit() -conn = sqlite3.connect('Database.db') -cur = conn.cursor() +if __name__ == "__main__": + if len(sys.argv) > 2: + raise ValueError(sys.argv[0] + " only takes one argument, the path of the new database file.") + try: + makeDB(sys.argv[1]) + except IndexError: + makeDB() -try: - 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));") -except sqlite3.OperationalError: - raise IOError("Database.db already exists. Please delete it before trying to create a new one.") - -for row in input[1:]: - cur.execute("INSERT INTO tweets VALUES(" + row[0].replace("'","''") + ",'" + row[1].replace("'","''") + "','" + row[2].replace("'","''") + "','" + row[3].replace("'","''") + "','" + row[4].replace("'","''") + "','" + row[5].replace("'","''") + "','" + row[6].replace("'","''") + "','" + row[7].replace("'","''") + "','" + row[8].replace("'","''") + "','" + row[9].replace("'","''") + "');") - -conn.commit() diff --git a/filler.py b/filler.py index 8cabded..8ef8710 100755 --- a/filler.py +++ b/filler.py @@ -15,7 +15,7 @@ def getSavepoint(db): def unescapeText(text): return html.parser.HTMLParser().unescape(text).replace("'","''") -def fill(dbpath=tools.config.dbpath, user=tools.config.user, two=tools.twObject()): +def fill(dbpath=tools.dbpath(), user=tools.user(), two=tools.twObject()): query = "from:" + user db = tools.dbHelper(dbpath) diff --git a/followertxt.py b/followertxt.py index dd14090..6b15653 100755 --- a/followertxt.py +++ b/followertxt.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 -import config, tools +import tools import os, time, tweepy def getFollowerIDs(two=tools.twObject()): ''' Returns 5,000 follower IDs at most ''' - return two.api.followers_ids(screen_name=config.user) + return two.api.followers_ids(screen_name=tools.user()) def getNamesByIDs(fids=getFollowerIDs(), two=tools.twObject()): for page in tools.paginate(fids, 100): diff --git a/getdates.py b/getdates.py index 0c77141..2d58eac 100755 --- a/getdates.py +++ b/getdates.py @@ -92,7 +92,7 @@ def getHeaders(strings, av): return [headers] -def getTweetsByDate(strings = [], fr = None, to = None, av = 0, path = tools.config.dbpath, headers = False): +def getTweetsByDate(strings = [], fr = None, to = None, av = 0, path = tools.dbpath(), headers = False): db = tools.dbHelper(path) if fr == None: diff --git a/getmentions.py b/getmentions.py index 10cf739..e0c9c1b 100755 --- a/getmentions.py +++ b/getmentions.py @@ -4,7 +4,7 @@ import tools import operator, re, sys -def getTweets(mode = "@", path = tools.config.dbpath): +def getTweets(mode = "@", path = tools.dbpath()): db = tools.dbHelper(path) handles = dict() @@ -27,7 +27,7 @@ def getTweets(mode = "@", path = tools.config.dbpath): if __name__ == "__main__": mode = "@" - path = tools.config.dbpath + path = tools.dbpath() if len(sys.argv) > 1: if len(sys.argv) > 3 or (len(sys.argv) == 3 and "-h" not in sys.argv): diff --git a/gettweets.py b/gettweets.py index 23f1d88..448794b 100755 --- a/gettweets.py +++ b/gettweets.py @@ -39,7 +39,7 @@ def queryBuilder(fr, to): return "SELECT * FROM tweets WHERE SUBSTR(timestamp,0,11) >= '%s' AND SUBSTR(timestamp,0,11) <= '%s'" % (fr, to) -def getDataByDate(fr, to, path = tools.config.dbpath, headers = True): +def getDataByDate(fr, to, path = tools.dbpath(), headers = True): db = tools.dbHelper(path) if fr == None: diff --git a/makedb.py b/makedb.py index 0b74a98..a1b8fb6 100755 --- a/makedb.py +++ b/makedb.py @@ -4,7 +4,7 @@ import tools import sys -def makeDB(path=tools.config.dbpath): +def makeDB(path=tools.dbpath()): db = tools.dbHelper(path, create = True) 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));") diff --git a/tools.py b/tools.py index 1957cf4..76dd77a 100644 --- a/tools.py +++ b/tools.py @@ -1,10 +1,55 @@ -import config +import configparser, csv, datetime, itertools, os, sqlite3, sys, tweepy + +class SetupException(Exception): + def __str__(self): + return "Seems like config.cfg has not been created yet. Run setup.py to do so." + + +def getSetting(section, setting): + config = configparser.RawConfigParser() + config.read('config.cfg') + return config.get(section, setting) + +def dbpath(): + try: + return getSetting("Database", "path") + except: + return "Database.db" + +def cke(): + try: + return getSetting("Twitter", "cke") + except: + return "V6ekVFYtavi6IvRFLS0dHifSh" + +def cse(): + try: + return getSetting("Twitter", "cse") + except: + return "U2duSfBtW0Z8UQFoJyARf3jU80gdQ44EEqWqC82ebuGbIPN3t7" + +def ato(): + try: + return getSetting("Twitter", "ato") + except: + raise SetupException() + +def ase(): + try: + return getSetting("Twitter", "ato") + except: + raise SetupException() + +def user(): + try: + return twObject().whoami() + except: + raise SetupException() -import csv, datetime, itertools, os, sqlite3, sys, tweepy class dbObject: - def __init__(self, path=config.dbpath): + def __init__(self, path=dbpath()): self.conn = sqlite3.connect(path) self.cur = self.conn.cursor() self.path = path @@ -39,9 +84,9 @@ class dbObject: class twObject: - def __init__(self, cke = config.cke, cse = config.cse, ato = config.ato, ase = config.ase): - self.auth = tweepy.OAuthHandler(config.cke, config.cse) - self.auth.set_access_token(config.ato, config.ase) + def __init__(self, cke = cke(), cse = cse(), ato = ato(), ase = ase()): + self.auth = tweepy.OAuthHandler(cke, cse) + self.auth.set_access_token(ato, ase) self.api = tweepy.API(self.auth) def delete(self, id): @@ -52,6 +97,9 @@ class twObject: tweets.reverse() return tweets + def whoami(self): + return self.api.me().screen_name + def dbCheck(db, create = False): if (not create and dbInitialized(db)) or (create and not dbInitialized(db)): diff --git a/tweleter.py b/tweleter.py index dd799c1..274cfdc 100755 --- a/tweleter.py +++ b/tweleter.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import config, tools +import tools import tkinter, tkinter.messagebox, html.parser, os @@ -41,7 +41,7 @@ def addStatus(id, text): list.insert(0, element.encode("UTF-8")) def getTweets(): - query = "from:" + config.user + query = "from:" + tools.user() try: timeline = two.search(query, 0)