Add media handling. Apparently webp files already work if you pass a wrong extension...

This commit is contained in:
Klaus-Uwe Mitterer 2017-03-18 21:45:02 +01:00
parent ab0e32bf27
commit fb7afcbb9c

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3
import ast, dbtools, logging, setuptools, strings, telegram.ext, twitools, tweepy
import ast, dbtools, io, logging, PIL.Image, setuptools, strings, telegram.ext, twitools, urllib.request, tweepy
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
@ -8,7 +8,7 @@ logger = logging.getLogger(__name__)
def log(bot, update, error):
logger.warn("Error %s caused by '%s'" % (error, update))
def twoExceptions(code, message):
def twoExceptions(e, message):
text = {
32: strings.badToken,
36: strings.selfSpam,
@ -28,9 +28,9 @@ def twoExceptions(code, message):
323: strings.multipleGIFs,
326: strings.accountLocked,
354: strings.longTweet
}.get(code, strings.twoFail)
}.get(e.api_code)
message.reply_text(text)
message.reply_text(text or strings.twoFail)
def getTwo(message):
try:
@ -38,7 +38,7 @@ def getTwo(message):
except ValueError:
noauth(message)
except tweepy.error.TweepError as e:
twoExceptions(e.api_code, message)
twoExceptions(e, message)
def noauth(message):
message.reply_text(strings.noauth)
@ -89,9 +89,32 @@ def fish(bot, update):
def explicitTweet(bot, update, args):
try:
two = getTwo(update.message)
two.tweet(' '.join(args))
if update.message.photo or update.message.video or update.message.sticker:
fid = update.message.sticker.file_id if update.message.sticker else update.message.video.file_id if update.message.video else update.message.photo[-1].file_id
path = bot.getFile(fid).file_path
media = urllib.request.urlopen(path)
mobj = io.BytesIO(media.read())
filename = path.split("/")[-1]
if filename.split(".")[-1].lower() == "webp":
out = io.BytesIO()
PIL.Image.open(mobj).convert('RGB').save(out, "jpeg")
out.read()
filename = "%s.jpg" % filename.split(".")[0]
two.api.update_with_media(filename, ' '.join(args), file=mobj)
else:
two.tweet(' '.join(args))
except tweepy.error.TweepError as e:
twoExceptions(e.api_code, update.message)
logging.exception("Meh.")
twoExceptions(e, update.message)
except:
logging.exception("Meh.")
def tweet(bot, update):
try:
@ -111,7 +134,7 @@ def timeline(bot, update, args = [10]):
for status in two.api.home_timeline(count=count):
update.message.reply_text("%s (%s) at %s: %s" % (status.author.name, status.author.screen_name, status.created_at, status.text))
except tweepy.error.TweepError as e:
twoExceptions(e.api_code, update.message)
twoExceptions(e, update.message)
def toggleTweet(bot, update):
try:
@ -141,6 +164,9 @@ if __name__ == "__main__":
updater.dispatcher.add_handler(telegram.ext.CommandHandler("verify", verify, pass_args=True))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, tweet))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.photo, tweet))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.sticker, tweet))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.video, tweet))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, unknown))
updater.dispatcher.add_error_handler(log)