2017-02-28 11:04:18 +00:00
|
|
|
import ast, configparser, csv, datetime, html.parser, itertools, os, sqlite3, sys, tweepy
|
2015-10-10 22:10:57 +00:00
|
|
|
|
2017-02-18 19:44:30 +00:00
|
|
|
TWITTER = 0
|
|
|
|
MARKOV = 1
|
|
|
|
LYRICS = 2
|
|
|
|
|
2015-10-10 22:10:57 +00:00
|
|
|
class SetupException(Exception):
|
|
|
|
def __str__(self):
|
2016-05-30 19:58:53 +00:00
|
|
|
return "Seems like config.cfg has not been created yet or contains serious errors. Run setup.py to create it."
|
2015-10-10 22:10:57 +00:00
|
|
|
|
2016-05-30 19:58:53 +00:00
|
|
|
def getSetting(section, setting, path = "config.cfg"):
|
2015-10-10 22:10:57 +00:00
|
|
|
config = configparser.RawConfigParser()
|
2016-05-30 19:58:53 +00:00
|
|
|
config.read(path)
|
2015-10-10 22:10:57 +00:00
|
|
|
return config.get(section, setting)
|
|
|
|
|
2017-02-28 10:43:28 +00:00
|
|
|
def getListSetting(section, setting, path = "config.cfg"):
|
|
|
|
return ast.literal_eval(getSetting(section, setting, path))
|
|
|
|
|
2016-05-30 19:58:53 +00:00
|
|
|
def dbtype():
|
|
|
|
try:
|
2016-06-30 10:33:09 +00:00
|
|
|
return int(getSetting("Database", "type"))
|
2016-05-30 19:58:53 +00:00
|
|
|
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()
|
|
|
|
|
2015-10-10 22:10:57 +00:00
|
|
|
def dbpath():
|
|
|
|
try:
|
|
|
|
return getSetting("Database", "path")
|
|
|
|
except:
|
2016-05-30 19:58:53 +00:00
|
|
|
return SetupException()
|
|
|
|
|
|
|
|
###
|
2015-10-10 22:10:57 +00:00
|
|
|
|
2017-02-18 19:44:30 +00:00
|
|
|
def sectionName(section):
|
|
|
|
if section == TWITTER:
|
|
|
|
return "Twitter"
|
|
|
|
elif section == MARKOV:
|
|
|
|
return "Markov"
|
|
|
|
elif section == LYRICS:
|
|
|
|
return "Lyrics"
|
|
|
|
|
2015-10-10 22:10:57 +00:00
|
|
|
def cke():
|
|
|
|
try:
|
|
|
|
return getSetting("Twitter", "cke")
|
|
|
|
except:
|
2016-02-08 00:55:12 +00:00
|
|
|
raise SetupException()
|
2015-10-10 22:10:57 +00:00
|
|
|
|
|
|
|
def cse():
|
|
|
|
try:
|
|
|
|
return getSetting("Twitter", "cse")
|
|
|
|
except:
|
2016-02-08 00:55:12 +00:00
|
|
|
raise SetupException()
|
2015-10-10 22:10:57 +00:00
|
|
|
|
2017-02-18 19:44:30 +00:00
|
|
|
def ato(section = TWITTER):
|
2015-10-10 22:10:57 +00:00
|
|
|
try:
|
2017-02-18 19:44:30 +00:00
|
|
|
return getSetting(sectionName(section), "ato")
|
2015-10-10 22:10:57 +00:00
|
|
|
except:
|
|
|
|
raise SetupException()
|
|
|
|
|
2017-02-18 19:44:30 +00:00
|
|
|
def ase(section = TWITTER):
|
2015-10-10 22:10:57 +00:00
|
|
|
try:
|
2017-02-18 19:44:30 +00:00
|
|
|
return getSetting(sectionName(section), "ase")
|
2015-10-10 22:10:57 +00:00
|
|
|
except:
|
|
|
|
raise SetupException()
|
|
|
|
|
2017-02-08 12:06:17 +00:00
|
|
|
def botname():
|
|
|
|
try:
|
|
|
|
return getSetting("Telegram", "botname")
|
|
|
|
except:
|
|
|
|
return SetupException()
|
|
|
|
|
2017-02-07 21:40:47 +00:00
|
|
|
def token():
|
|
|
|
try:
|
|
|
|
return getSetting("Telegram", "token")
|
|
|
|
except:
|
|
|
|
return SetupException()
|
|
|
|
|
2017-02-08 12:06:17 +00:00
|
|
|
def url():
|
|
|
|
try:
|
|
|
|
return getSetting("Bot", "url")
|
|
|
|
except:
|
|
|
|
return SetupExecption()
|
|
|
|
|
2017-02-07 21:40:47 +00:00
|
|
|
|
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-05-17 19:33:25 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-04-07 22:15:21 +00:00
|
|
|
def unescapeText(text):
|
|
|
|
return html.parser.HTMLParser().unescape(text).replace("'","''")
|
|
|
|
|