Changed config system
This commit is contained in:
parent
3fcb87d145
commit
7af8c609d0
10 changed files with 90 additions and 43 deletions
11
config.py
11
config.py
|
@ -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"
|
||||
|
42
csvdb.py
42
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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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));")
|
||||
|
|
60
tools.py
60
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)):
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue