import config import csv, os, sqlite3, sys, tweepy class dbObject: def __init__(self, path=config.dbpath): self.conn = sqlite3.connect(path) self.cur = self.conn.cursor() self.path = path def closeConnection(self): return self.conn.close() def commit(self): return self.conn.commit() def executeQuery(self, query): return self.cur.execute(query) def getNext(self): return self.cur.fetchone() def isInitialized(self): try: self.executeQuery("SELECT * FROM tweets") return True except: return False 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 def dbCheck(db, create = False): if (not create and dbInitialized(db)) or (create and not dbInitialized(db)): return True if create: 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") def dbHelper(path, create = False): db = dbObject(path) dbCheck(db, create) return db def dbInitialized(db): return db.isInitialized() def fileExists(path): return os.path.isfile(path) 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 def printCSV(inlist): writer = csv.writer(sys.stdout) writer.writerows(inlist)