2017-01-27 22:25:00 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import dbtools, twitools
|
|
|
|
import argparse, operator, random, re, sys
|
|
|
|
|
|
|
|
def getLyrics(db = dbtools.dbHelper()):
|
|
|
|
lyrics = db.executeQuery("SELECT id, text, ref FROM lyrics WHERE active = 1")
|
|
|
|
lyric = random.sample(list(lyrics), 1)[0]
|
|
|
|
ref = list(db.executeQuery("SELECT tweet_id FROM lyrics WHERE id = %s" % lyric[2]))[0][0] if not int(lyric[2]) == 0 else 0
|
|
|
|
|
|
|
|
return lyric[0], lyric[1], ref
|
|
|
|
|
2017-01-27 22:44:52 +00:00
|
|
|
def findParent(lid, db = dbtools.dbHelper()):
|
|
|
|
ref = int(list(db.executeQuery("SELECT ref FROM lyrics WHERE id = %i;" % int(lid)))[0][0])
|
|
|
|
return lid if ref == 0 else findParent(ref)
|
|
|
|
|
2017-01-27 22:25:00 +00:00
|
|
|
def postprocess(lid, tid, db = dbtools.dbHelper()):
|
|
|
|
db.executeQuery("UPDATE lyrics SET tweet_id = %i WHERE id = %i;" % (int(tid), int(lid)))
|
|
|
|
db.executeQuery("UPDATE lyrics SET active = 0 WHERE id = %i;" % int(lid))
|
2017-01-27 22:44:52 +00:00
|
|
|
|
|
|
|
if list(db.executeQuery("SELECT COUNT(*) FROM lyrics WHERE ref = %i;" % int(lid)))[0][0] == 0:
|
|
|
|
db.executeQuery("UPDATE lyrics SET active = 1 WHERE id = %i;" % findParent(lid))
|
|
|
|
else:
|
|
|
|
db.executeQuery("UPDATE lyrics SET active = 1 WHERE ref = %i;" % int(lid))
|
2017-01-27 22:25:00 +00:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
lid, text, ref = getLyrics()
|
2017-02-15 21:42:38 +00:00
|
|
|
postprocess(lid, twitools.tweet(text, ref))
|