twitools/lyricsbot.py

30 lines
1.2 KiB
Python
Raw Normal View History

#!/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
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)
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))
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))
db.commit()
if __name__ == "__main__":
lid, text, ref = getLyrics()
2017-02-15 21:42:38 +00:00
postprocess(lid, twitools.tweet(text, ref))