Merge branch 'master' of git.klaus-uwe.me:kumitterer/twitools

This commit is contained in:
Klaus-Uwe Mitterer 2017-02-21 16:11:16 +01:00
commit 654954de92
3 changed files with 41 additions and 7 deletions

View file

@ -26,4 +26,4 @@ def postprocess(lid, tid, db = dbtools.dbHelper()):
if __name__ == "__main__": if __name__ == "__main__":
lid, text, ref = getLyrics() lid, text, ref = getLyrics()
postprocess(lid, twitools.tweet(text, ref, setuptools.LYRICS)) postprocess(lid, twitools.tweet("%s (#%i)" % (text, int(lid)), ref, setuptools.LYRICS))

30
lyricsmanager.py Executable file
View file

@ -0,0 +1,30 @@
#!/usr/bin/env python3
import dbtools
def addLyrics(text, ref = 0, db = dbtools.dbHelper()):
db.executeQuery("INSERT INTO lyrics(text, ref, active) VALUES('%s', %i, %i);" % (text, ref, (1 if ref == 0 else 0)))
db.commit()
return db.cur.lastrowid
def queryLyrics(ref = 0):
text = input("Text: ")
if len(text) > 130:
print("Text too long (%i characters)" % len(text))
return queryLyrics(ref)
ref = int(input("Reference [%i]: " % ref) or ref)
row = addLyrics(text, ref)
ans = ""
while ans.lower() not in ("y", "n"):
ans = input("Add follow-up lyrics? [Y/n] ")
if ans.lower() != "n":
queryLyrics(row)
if __name__ == "__main__":
queryLyrics()

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import dbtools, setuptools, twitools import dbtools, setuptools, twitools
import argparse, html, markovify, nltk, operator, random, re, sys import argparse, html, markovify, nltk, operator, random, re, string, sys
class Possy(markovify.NewlineText): class Possy(markovify.NewlineText):
def word_split(self, sentence): def word_split(self, sentence):
@ -14,18 +14,22 @@ class Possy(markovify.NewlineText):
return sentence return sentence
def sanitizeText(text): def sanitizeText(text):
split = text.split()
try: try:
if text[0] == "@": if "@" in (text[0], text[1]):
return sanitizeText(text.partition(" ")[2]) if split[1][0] not in string.ascii_lowercase:
if text.split()[-1][0] == "@": return sanitizeText(text.partition(" ")[2])
return sanitizeText(" ".join(text.split()[:-1])) if split[-1][0] == "@":
return sanitizeText(" ".join(split[:-1]))
if text[:4] == "RT @":
return sanitizeText(text.partition(":")[2])
except: except:
return "" return ""
return text return text
def getText(db = dbtools.dbHelper()): def getText(db = dbtools.dbHelper()):
text = "" text = ""
for string in db.executeQuery('SELECT text FROM tweets WHERE text NOT LIKE "RT %";'): for string in db.executeQuery('SELECT text FROM tweets;'):
text += sanitizeText(string[0]) + "\n" text += sanitizeText(string[0]) + "\n"
return html.unescape("".join([s for s in text.strip().splitlines(True) if s.strip()])) return html.unescape("".join([s for s in text.strip().splitlines(True) if s.strip()]))