From ba55c7984e85641b984a93d7ef068fce60eb9da9 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Mon, 8 Feb 2016 01:55:12 +0100 Subject: [PATCH] Check in whatever I changed in the meantime, obviously including a setup script --- csvdb.py | 2 +- getmentions.py | 2 +- setup.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools.py | 13 ++++----- 4 files changed, 82 insertions(+), 10 deletions(-) create mode 100755 setup.py diff --git a/csvdb.py b/csvdb.py index 15b6a0a..153f413 100755 --- a/csvdb.py +++ b/csvdb.py @@ -2,7 +2,7 @@ import tools -import sqlite3, csv +import sqlite3, csv, sys def makeDB(path=tools.dbpath()): try: diff --git a/getmentions.py b/getmentions.py index e0c9c1b..833f6ca 100755 --- a/getmentions.py +++ b/getmentions.py @@ -11,7 +11,7 @@ def getTweets(mode = "@", path = tools.dbpath()): tweets = db.executeQuery("SELECT text FROM tweets") for tweet in tweets: - for word in tweet[0].split(): + for word in tweet[0].lower().split(): if word[0] == mode or mode == "": if mode == "": handle = word diff --git a/setup.py b/setup.py new file mode 100755 index 0000000..1f2c445 --- /dev/null +++ b/setup.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 + +import configparser, os.path, sqlite3, tweepy + +if os.path.isfile("config.cfg"): + print("config.cfg already exists. Please remove it before running this script.") + exit(1) + +config = configparser.RawConfigParser() + +config.add_section('Database') + +print('''Twitools will use a database for certain tasks. If this file does not exist yet, +it will be created in this process. The file name defaults to 'Database.db'. +''') + +dbpath = input("Name of the database file [Database.db]: ") +print() + +if dbpath == "": + dbpath = "Database.db" + +config.set('Database', 'path', dbpath) + +if os.path.isfile(dbpath): + pass +else: + conn = sqlite3.connect(dbpath) + cur = conn.cursor() + 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));") + conn.commit() + conn.close() + +config.add_section("Twitter") + +cke = "V6ekVFYtavi6IvRFLS0dHifSh" +cse = "U2duSfBtW0Z8UQFoJyARf3jU80gdQ44EEqWqC82ebuGbIPN3t7" + +config.set("Twitter", "cke", cke) +config.set("Twitter", "cse", cse) + +auth = tweepy.OAuthHandler(cke, cse) + +try: + authurl = auth.get_authorization_url() +except tweepy.TweepError: + print("Error getting request token. Please try again later...") + exit(1) + +print('''In the next step, we'll get you connected to Twitter. Please follow this link, +sign on to Twitter and copy the PIN you will get there. Insert it below, then +press Enter to continue. +''') + +print(authurl) +print() + +pin = input("PIN: ") +print() + +try: + auth.get_access_token(pin) +except tweepy.TweepError: + print("Error getting access token. Please try again later...") + exit(1) + +config.set("Twitter", "ato", auth.access_token) +config.set("Twitter", "ase", auth.access_token_secret) + +print("Seems like everything worked out fine. Let's write that config file...") + +with open('config.cfg', 'wt') as cfg: + config.write(cfg) + +print("We're all done. You can now use Twitools. Have fun!") diff --git a/tools.py b/tools.py index 76dd77a..d39a1eb 100644 --- a/tools.py +++ b/tools.py @@ -20,13 +20,13 @@ def cke(): try: return getSetting("Twitter", "cke") except: - return "V6ekVFYtavi6IvRFLS0dHifSh" + raise SetupException() def cse(): try: return getSetting("Twitter", "cse") except: - return "U2duSfBtW0Z8UQFoJyARf3jU80gdQ44EEqWqC82ebuGbIPN3t7" + raise SetupException() def ato(): try: @@ -36,15 +36,12 @@ def ato(): def ase(): try: - return getSetting("Twitter", "ato") + return getSetting("Twitter", "ase") except: raise SetupException() def user(): - try: - return twObject().whoami() - except: - raise SetupException() + return twObject().whoami() class dbObject: @@ -98,7 +95,7 @@ class twObject: return tweets def whoami(self): - return self.api.me().screen_name + return self.auth.get_username() def dbCheck(db, create = False):