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
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.WARN)
logger = logging.getLogger(__name__)
def getTwo(message):
try:
return twitools.twoBotHelper(message.chat_id)
@ -316,10 +319,11 @@ def explicitTweet(bot, update, args, reply = None):
else:
try:
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:
logging.exception(e)
bottools.methods.tweetMessage(status, update.message.chat_id, bot)
logger.exception(e)
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
@ -476,7 +480,7 @@ def tweetMessage(status, cid, bot, force = False, callback = None, notified = No
try:
two = twitools.twoBotHelper(cid)
except tweepy.error.TweepError as e:
logging.exception(e)
logger.exception(e)
if not callback:
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:
rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons, header=header))
except Exception as e:
logging.exception(e)
logger.exception(e)
if callback:
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:
bot.sendMessage(chat_id = u, text = ' '.join(args))
except:
logging.exception("Could not send broadcast.")
logger.exception("Could not send broadcast.")
else:
bottools.methods.unknown(bot, update)
@ -730,7 +734,7 @@ def emergency(bot, update, args):
try:
bot.sendMessage(chat_id = u, text = ' '.join(args))
except:
logging.exception("Could not send emergency broadcast.")
logger.exception("Could not send emergency broadcast.")
else:
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.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():
if setuptools.dbtype() == SQLITE:
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()):
remote = two.auth.oauth.get(url, auth=two.api.auth.apply_auth())
@ -17,6 +17,6 @@ def shortURL(url):
try:
return pyshorteners.Shortener('Google', api_key=setuptools.getSetting("Google", "googl")).short(longURL(url))
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)
return False

View File

@ -1,7 +1,23 @@
#!/usr/bin/env python3
import dbtools, setuptools, twitools, filters.markov
import argparse, datetime, html, markovify, nltk, operator, os, random, re, string, sys, time
import dbtools
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):
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()]))
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())
outtext = filters.markov.textFilter(text)
if outtext:
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 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 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 timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));")
db.executeQuery("CREATE TABLE verified(`uid` INT PRIMARY KEY);")
db.commit()
db.closeConnection()

View File

@ -1,32 +1,45 @@
#!/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)
logger = logging.getLogger(__name__)
logger.addHandler(logging.handlers.SysLogHandler(address='/dev/log'))
def log(bot, update, error):
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__":
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():
updater.dispatcher.add_handler(telegram.ext.CommandHandler(k, v, pass_args = True if v in bottools.methods.pargs else False))
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.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))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.sticker, 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))
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(t, bottools.methods.captionHelper))
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.idle()
updater.start_polling()
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("@", "@")
try:
two.tweet(html.parser.HTMLParser().unescape(tstring[:140]))
two.tweet(html.parser.HTMLParser().unescape(tstring[:280]))
tw_counter += 1
except Exception as e:
print(e)