Copy over setuptools from twitools, add function for API token
This commit is contained in:
parent
9036a7bee7
commit
c4f0549d70
1 changed files with 144 additions and 0 deletions
144
setuptools/__init__.py
Normal file
144
setuptools/__init__.py
Normal file
|
@ -0,0 +1,144 @@
|
|||
import configparser, csv, datetime, html.parser, itertools, os, sqlite3, sys, tweepy
|
||||
|
||||
class SetupException(Exception):
|
||||
def __str__(self):
|
||||
return "Seems like config.cfg has not been created yet or contains serious errors. Run setup.py to create it."
|
||||
|
||||
def getSetting(section, setting, path = "config.cfg"):
|
||||
config = configparser.RawConfigParser()
|
||||
config.read(path)
|
||||
return config.get(section, setting)
|
||||
|
||||
def dbtype():
|
||||
try:
|
||||
return int(getSetting("Database", "type"))
|
||||
except:
|
||||
return 0 # for SQLite3
|
||||
|
||||
### Must only be called AFTER dbtype()! ###
|
||||
|
||||
def dbhost():
|
||||
try:
|
||||
return getSetting("Database", "host")
|
||||
except:
|
||||
raise SetupException()
|
||||
|
||||
def dbuser():
|
||||
try:
|
||||
return getSetting("Database", "user")
|
||||
except:
|
||||
raise SetupException()
|
||||
|
||||
def dbpass():
|
||||
try:
|
||||
return getSetting("Database", "pass")
|
||||
except:
|
||||
raise SetupException()
|
||||
|
||||
def dbname():
|
||||
try:
|
||||
return getSetting("Database", "name")
|
||||
except:
|
||||
raise SetupException()
|
||||
|
||||
def dbpath():
|
||||
try:
|
||||
return getSetting("Database", "path")
|
||||
except:
|
||||
return SetupException()
|
||||
|
||||
###
|
||||
|
||||
def token():
|
||||
try:
|
||||
return getSetting("Telegram", "token")
|
||||
except:
|
||||
return SetupException()
|
||||
|
||||
def cke():
|
||||
try:
|
||||
return getSetting("Twitter", "cke")
|
||||
except:
|
||||
raise SetupException()
|
||||
|
||||
def cse():
|
||||
try:
|
||||
return getSetting("Twitter", "cse")
|
||||
except:
|
||||
raise SetupException()
|
||||
|
||||
def ato():
|
||||
try:
|
||||
return getSetting("Twitter", "ato")
|
||||
except:
|
||||
raise SetupException()
|
||||
|
||||
def ase():
|
||||
try:
|
||||
return getSetting("Twitter", "ase")
|
||||
except:
|
||||
raise SetupException()
|
||||
|
||||
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 getDate(date):
|
||||
try:
|
||||
return datetime.datetime.strptime(date, '%Y-%m-%d')
|
||||
except ValueError:
|
||||
raise ValueError("Dates must be in YYYY-MM-DD format.")
|
||||
|
||||
|
||||
def paginate(iterable, page_size):
|
||||
while True:
|
||||
i1, i2 = itertools.tee(iterable)
|
||||
iterable, page = (itertools.islice(i1, page_size, None), list(itertools.islice(i2, page_size)))
|
||||
if len(page) == 0:
|
||||
break
|
||||
yield page
|
||||
|
||||
|
||||
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)
|
||||
|
||||
def unescapeText(text):
|
||||
return html.parser.HTMLParser().unescape(text).replace("'","''")
|
||||
|
Loading…
Reference in a new issue