Check in old refactoring, update maximum tweet length

This commit is contained in:
Klaus-Uwe Mitterer 2017-12-06 18:20:13 +01:00
parent 34e0214e81
commit c8a3e38575
7 changed files with 84 additions and 31 deletions

View file

@ -1,5 +1,8 @@
import ast, dbtools, html, io, logging, moviepy.editor, os, PIL.Image, random, re, setuptools, string, bottools.strings, sys, telegram.ext, telegram, time, twitools, bottools.streaming, bottools.thread, urllib.request, tweepy import ast, dbtools, html, io, logging, moviepy.editor, os, PIL.Image, random, re, setuptools, string, bottools.strings, sys, telegram.ext, telegram, time, twitools, bottools.streaming, bottools.thread, urllib.request, tweepy
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.WARN)
logger = logging.getLogger(__name__)
def getTwo(message): def getTwo(message):
try: try:
return twitools.twoBotHelper(message.chat_id) return twitools.twoBotHelper(message.chat_id)
@ -316,10 +319,11 @@ def explicitTweet(bot, update, args, reply = None):
else: else:
try: try:
status = two.tweet(' '.join(args), reply) status = two.tweet(' '.join(args), reply)
bottools.methods.tweetMessage(status, update.message.chat_id, bot)
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
except Exception as e: except Exception as e:
logging.exception(e) logger.exception(e)
bottools.methods.tweetMessage(status, update.message.chat_id, bot)
except tweepy.error.TweepError as e: except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message) bottools.methods.twoExceptions(e, update.message)
@ -476,7 +480,7 @@ def tweetMessage(status, cid, bot, force = False, callback = None, notified = No
try: try:
two = twitools.twoBotHelper(cid) two = twitools.twoBotHelper(cid)
except tweepy.error.TweepError as e: except tweepy.error.TweepError as e:
logging.exception(e) logger.exception(e)
if not callback: if not callback:
if status.user.screen_name.strip("@") == two.whoami().strip("@") and not db.getCStatus(cid): if status.user.screen_name.strip("@") == two.whoami().strip("@") and not db.getCStatus(cid):
@ -526,7 +530,7 @@ def tweetMessage(status, cid, bot, force = False, callback = None, notified = No
try: try:
rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons, header=header)) rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons, header=header))
except Exception as e: except Exception as e:
logging.exception(e) logger.exception(e)
if callback: if callback:
bot.editMessageReplyMarkup(chat_id=callback.message.chat_id, message_id=callback.message.message_id, reply_markup=rmu) bot.editMessageReplyMarkup(chat_id=callback.message.chat_id, message_id=callback.message.message_id, reply_markup=rmu)
@ -720,7 +724,7 @@ def broadcast(bot, update, args):
try: try:
bot.sendMessage(chat_id = u, text = ' '.join(args)) bot.sendMessage(chat_id = u, text = ' '.join(args))
except: except:
logging.exception("Could not send broadcast.") logger.exception("Could not send broadcast.")
else: else:
bottools.methods.unknown(bot, update) bottools.methods.unknown(bot, update)
@ -730,7 +734,7 @@ def emergency(bot, update, args):
try: try:
bot.sendMessage(chat_id = u, text = ' '.join(args)) bot.sendMessage(chat_id = u, text = ' '.join(args))
except: except:
logging.exception("Could not send emergency broadcast.") logger.exception("Could not send emergency broadcast.")
else: else:
bottools.methods.unknown(bot, update) bottools.methods.unknown(bot, update)

View file

@ -249,6 +249,14 @@ class dbObject:
self.executeQuery("UPDATE tokens SET active = 1 WHERE cid = %i AND ato = '%s';" % (int(cid), ato)) self.executeQuery("UPDATE tokens SET active = 1 WHERE cid = %i AND ato = '%s';" % (int(cid), ato))
self.commit() self.commit()
def setVerified(self, uid):
self.executeQuery("INSERT INTO verified VALUES(%i);" % int(uid))
self.commit()
def unsetVerified(self, uid):
self.executeQuery("DELETE FROM verified WHERE uid = %i;" % int(uid))
self.commit()
def dbHelper(): def dbHelper():
if setuptools.dbtype() == SQLITE: if setuptools.dbtype() == SQLITE:
return dbObject(dbtype=SQLITE, path=setuptools.dbpath()) return dbObject(dbtype=SQLITE, path=setuptools.dbpath())

View file

@ -1,4 +1,4 @@
import setuptools, pyshorteners, requests, twitools import codecs, setuptools, pyshorteners, requests, twitools, sys
def downloadMedia(url, tid, mid, two = twitools.twoHelper()): def downloadMedia(url, tid, mid, two = twitools.twoHelper()):
remote = two.auth.oauth.get(url, auth=two.api.auth.apply_auth()) remote = two.auth.oauth.get(url, auth=two.api.auth.apply_auth())
@ -17,6 +17,6 @@ def shortURL(url):
try: try:
return pyshorteners.Shortener('Google', api_key=setuptools.getSetting("Google", "googl")).short(longURL(url)) return pyshorteners.Shortener('Google', api_key=setuptools.getSetting("Google", "googl")).short(longURL(url))
except pyshorteners.exceptions.ShorteningErrorException as e: except pyshorteners.exceptions.ShorteningErrorException as e:
print("Error handling %s" % url) codecs.getwriter("utf8")(sys.stdout.buffer).write("Error handling %s\n" % url)
print(e) print(e)
return False return False

View file

@ -1,7 +1,23 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import dbtools, setuptools, twitools, filters.markov import dbtools
import argparse, datetime, html, markovify, nltk, operator, os, random, re, string, sys, time import setuptools
import twitools
import filters.markov
import argparse
import datetime
import html
import markovify
import multiprocessing
import nltk
import operator
import os
import random
import re
import string
import sys
import time
class Possy(markovify.NewlineText): class Possy(markovify.NewlineText):
def word_split(self, sentence): def word_split(self, sentence):
@ -34,10 +50,21 @@ def getText(db = dbtools.dbHelper()):
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()]))
def markovifyText(text): def markovifyText(text):
return Possy(text).make_short_sentence(130).replace("@", "@") return Possy(text).make_short_sentence(270).replace("@", "@")
if __name__ == "__main__": def worker():
text = markovifyText(getText()) text = markovifyText(getText())
outtext = filters.markov.textFilter(text) outtext = filters.markov.textFilter(text)
if outtext: if outtext:
filters.markov.tweetFilter(twitools.tweet(outtext, section = setuptools.MARKOV), text) filters.markov.tweetFilter(twitools.tweet(outtext, section = setuptools.MARKOV), text)
if __name__ == "__main__":
p = multiprocessing.Process(target=worker)
p.start()
time.sleep(20*60)
if p.is_alive():
p.terminate()
p.join()

View file

@ -64,9 +64,10 @@ if not db.isInitialized():
db.executeQuery("CREATE TABLE following(`id` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until));") db.executeQuery("CREATE TABLE following(`id` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until));")
db.executeQuery("CREATE TABLE names(`id` TEXT NOT NULL, `name` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until));") db.executeQuery("CREATE TABLE names(`id` TEXT NOT NULL, `name` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until));")
db.executeQuery("CREATE TABLE retweets(id INT PRIMARY KEY, author VARCHAR(30), created_at VARCHAR(30), text TEXT);") db.executeQuery("CREATE TABLE retweets(id INT PRIMARY KEY, author VARCHAR(30), created_at VARCHAR(30), text TEXT);")
db.executeQuery("CREATE TABLE lyrics(id INTEGER PRIMARY KEY AUTOINCREMENT, text VARCHAR(140) NOT NULL, ref INT NOT NULL default '0', tweet_id INT, active BOOLEAN default '0');") db.executeQuery("CREATE TABLE lyrics(id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT NOT NULL, ref INT NOT NULL default '0', tweet_id INT, active BOOLEAN default '0');")
db.executeQuery("CREATE TABLE tokens(`cid` INT PRIMARY KEY, `ato` TEXT, `ase` TEXT, `tweet` BOOLEAN DEFAULT 1, `fish` INT DEFAULT 0, `mentions` BOOLEAN DEFAULT 0, `broadcast` BOOLEAN DEFAULT 1, `confirmations` BOOLEAN DEFAULT 1, `lat` REAL DEFAULT NULL, `lon` REAL DEFAULT NULL, `active` BOOLEAN DEFAULT 1);") db.executeQuery("CREATE TABLE tokens(`cid` INT PRIMARY KEY, `ato` TEXT, `ase` TEXT, `tweet` BOOLEAN DEFAULT 1, `fish` INT DEFAULT 0, `mentions` BOOLEAN DEFAULT 0, `broadcast` BOOLEAN DEFAULT 1, `confirmations` BOOLEAN DEFAULT 1, `lat` REAL DEFAULT NULL, `lon` REAL DEFAULT NULL, `active` BOOLEAN DEFAULT 1);")
db.executeQuery("CREATE TABLE timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));") db.executeQuery("CREATE TABLE timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));")
db.executeQuery("CREATE TABLE verified(`uid` INT PRIMARY KEY);")
db.commit() db.commit()
db.closeConnection() db.closeConnection()

View file

@ -1,32 +1,45 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import bottools.methods, logging, setuptools, telegram.ext import bottools.methods, logging, logging.handlers, os, setuptools, sys, telegram.ext
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.WARN) logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.WARN)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.addHandler(logging.handlers.SysLogHandler(address='/dev/log'))
def log(bot, update, error): def log(bot, update, error):
logger.warn("Error %s caused by '%s'" % (error, update)) logger.warn("Error %s caused by '%s'" % (error, update))
def shutdown(e = None, u = None, restart = False):
if e:
logger.warn(e)
if u:
u.stop()
if restart:
os.execl(sys.executable, sys.executable, *sys.argv)
else:
sys.exit(1 if e else 0)
if __name__ == "__main__": if __name__ == "__main__":
updater = telegram.ext.Updater(token=setuptools.token()) try:
updater = telegram.ext.Updater(token=setuptools.token())
updater.dispatcher.add_error_handler(log)
for k, v in bottools.methods.commands.items(): for k, v in bottools.methods.commands.items():
updater.dispatcher.add_handler(telegram.ext.CommandHandler(k, v, pass_args = True if v in bottools.methods.pargs else False)) updater.dispatcher.add_handler(telegram.ext.CommandHandler(k, v, pass_args = True if v in bottools.methods.pargs else False))
updater.dispatcher.add_handler(telegram.ext.RegexHandler("^@TweepBot \/.*", bottools.methods.mentionHelper)) updater.dispatcher.add_handler(telegram.ext.RegexHandler("^@TweepBot \/.*", bottools.methods.mentionHelper))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.photo, bottools.methods.captionHelper)) for t in [telegram.ext.Filters.photo, telegram.ext.Filters.sticker, telegram.ext.Filters.video, telegram.ext.Filters.document]:
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.sticker, bottools.methods.captionHelper)) updater.dispatcher.add_handler(telegram.ext.MessageHandler(t, bottools.methods.captionHelper))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.video, bottools.methods.captionHelper))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.document, bottools.methods.captionHelper))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.location, bottools.methods.storeLocation))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, bottools.methods.tweet))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, bottools.methods.unknown))
updater.dispatcher.add_handler(telegram.ext.CallbackQueryHandler(bottools.methods.callback)) updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.location, bottools.methods.storeLocation))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, bottools.methods.tweet))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, bottools.methods.unknown))
updater.dispatcher.add_error_handler(log) updater.dispatcher.add_handler(telegram.ext.CallbackQueryHandler(bottools.methods.callback))
updater.start_polling() updater.start_polling()
updater.idle() updater.idle()
except Exception as e:
shutdown(e, updater, True)

View file

@ -56,7 +56,7 @@ for status in timeline:
tstring = translator.translate(intext, target_language=a[0])['translatedText'].replace("@", "@") tstring = translator.translate(intext, target_language=a[0])['translatedText'].replace("@", "@")
try: try:
two.tweet(html.parser.HTMLParser().unescape(tstring[:140])) two.tweet(html.parser.HTMLParser().unescape(tstring[:280]))
tw_counter += 1 tw_counter += 1
except Exception as e: except Exception as e:
print(e) print(e)