2015-04-21 21:07:25 +00:00
|
|
|
import config
|
|
|
|
|
2015-05-14 14:29:55 +00:00
|
|
|
import csv, datetime, os, sqlite3, sys, tweepy
|
2015-04-21 21:07:25 +00:00
|
|
|
|
|
|
|
class dbObject:
|
|
|
|
|
|
|
|
def __init__(self, path=config.dbpath):
|
|
|
|
self.conn = sqlite3.connect(path)
|
|
|
|
self.cur = self.conn.cursor()
|
2015-04-22 00:06:32 +00:00
|
|
|
self.path = path
|
2015-04-21 21:07:25 +00:00
|
|
|
|
|
|
|
def closeConnection(self):
|
|
|
|
return self.conn.close()
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
return self.conn.commit()
|
|
|
|
|
|
|
|
def executeQuery(self, query):
|
2015-04-28 08:39:30 +00:00
|
|
|
return self.cur.execute(query)
|
2015-04-21 21:07:25 +00:00
|
|
|
|
2015-04-22 00:06:32 +00:00
|
|
|
def getNext(self):
|
|
|
|
return self.cur.fetchone()
|
2015-04-21 21:07:25 +00:00
|
|
|
|
2015-04-21 22:16:42 +00:00
|
|
|
def isInitialized(self):
|
|
|
|
try:
|
|
|
|
self.executeQuery("SELECT * FROM tweets")
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2015-05-14 14:29:55 +00:00
|
|
|
def getFLDate(self, val = 0):
|
|
|
|
if val == 0:
|
|
|
|
mode = "MIN"
|
|
|
|
else:
|
|
|
|
mode = "MAX"
|
|
|
|
|
|
|
|
return getDate(str(list(self.executeQuery("SELECT %s(SUBSTR(timestamp,0,11)) FROM tweets" % mode))[0][0]))
|
|
|
|
|
2015-04-21 22:16:42 +00:00
|
|
|
|
2015-04-22 00:06:32 +00:00
|
|
|
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)
|
|
|
|
self.api = tweepy.API(self.auth)
|
|
|
|
|
|
|
|
def search(self, query, savepoint = 0):
|
|
|
|
tweets = list(tweepy.Cursor(self.api.search, q=query, since_id=savepoint).items())
|
|
|
|
tweets.reverse()
|
|
|
|
return tweets
|
|
|
|
|
|
|
|
|
2015-04-21 22:42:20 +00:00
|
|
|
def dbCheck(db, create = False):
|
2015-04-22 00:06:32 +00:00
|
|
|
if (not create and dbInitialized(db)) or (create and not dbInitialized(db)):
|
2015-04-21 22:16:42 +00:00
|
|
|
return True
|
2015-04-21 22:42:20 +00:00
|
|
|
if create:
|
2015-04-22 00:06:32 +00:00
|
|
|
raise ValueError("Provided database file " + db.path + " is already initialized. Remove it manually before trying to recreate it.")
|
|
|
|
raise ValueError("Provided database file " + db.path + " is not initialized. Create it using makedb.py or csvdb.py")
|
2015-04-21 22:16:42 +00:00
|
|
|
|
|
|
|
|
2015-04-21 22:42:20 +00:00
|
|
|
def dbHelper(path, create = False):
|
2015-04-21 22:21:16 +00:00
|
|
|
db = dbObject(path)
|
2015-04-21 22:42:20 +00:00
|
|
|
dbCheck(db, create)
|
2015-04-21 22:21:16 +00:00
|
|
|
return db
|
2015-04-21 22:16:42 +00:00
|
|
|
|
|
|
|
|
2015-04-21 22:21:16 +00:00
|
|
|
def dbInitialized(db):
|
2015-04-21 22:16:42 +00:00
|
|
|
return db.isInitialized()
|
|
|
|
|
2015-04-21 21:07:25 +00:00
|
|
|
|
|
|
|
def fileExists(path):
|
|
|
|
return os.path.isfile(path)
|
|
|
|
|
2015-04-21 22:16:42 +00:00
|
|
|
|
2015-05-14 14:29:55 +00:00
|
|
|
def getDate(date):
|
|
|
|
try:
|
|
|
|
return datetime.datetime.strptime(date, '%Y-%m-%d')
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError("Dates must be in YYYY-MM-DD format.")
|
|
|
|
|
|
|
|
|
2015-04-21 22:16:42 +00:00
|
|
|
def parseArgs(argv):
|
|
|
|
args = []
|
|
|
|
path = None
|
|
|
|
nextpath = False
|
|
|
|
|
|
|
|
for a in argv[1:]:
|
|
|
|
if nextpath:
|
|
|
|
path = a
|
|
|
|
nextpath = False
|
|
|
|
elif a == "-f":
|
|
|
|
if path != None:
|
|
|
|
raise ValueError("You can only pass one database file.")
|
|
|
|
nextpath = True
|
|
|
|
else:
|
|
|
|
args += [a]
|
|
|
|
|
|
|
|
return args, path
|
2015-04-24 21:33:42 +00:00
|
|
|
|
|
|
|
def printCSV(inlist):
|
|
|
|
writer = csv.writer(sys.stdout)
|
|
|
|
writer.writerows(inlist)
|
|
|
|
|