Handle animated GIFs

This commit is contained in:
Klaus-Uwe Mitterer 2017-03-20 17:56:15 +01:00
parent 77fa014b75
commit 7c508ddff5
2 changed files with 19 additions and 4 deletions

View file

@ -3,6 +3,7 @@
import configparser, os.path, tweepy, dbtools, getpass, shutil
os.makedirs("media", exist_ok=True)
os.makedirs("tmp", exist_ok=True)
if not os.path.isfile("filters/filler.py"):
shutil.copyfile("filters/filler.py.dist", "filters/filler.py")

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3
import ast, dbtools, html, io, logging, PIL.Image, setuptools, strings, telegram.ext, twitools, urllib.request, tweepy
import ast, dbtools, html, io, logging, moviepy.editor, PIL.Image, random, setuptools, string, 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__)
@ -98,8 +98,8 @@ def explicitTweet(bot, update, args, reply = None):
try:
two = getTwo(update.message)
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
if update.message.photo or update.message.document or update.message.video or update.message.sticker:
fid = update.message.document.file_id if update.message.document.file_id else 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())
@ -111,10 +111,23 @@ def explicitTweet(bot, update, args, reply = None):
PIL.Image.open(mobj).convert('RGB').save(out, format="JPEG")
filename = "%s.jpg" % filename.split(".")[0]
if update.message.document and filename.split(".")[-1].lower() == "mp4":
temp = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(32))
with open("tmp/%s.%s" % (temp, filename.split(".")[-1]), "wb") as f:
f.write(mobj.getvalue())
moviepy.editor.VideoFileClip("tmp/%s.%s" % (temp, filename.split(".")[-1])).resize(0.3).write_gif("tmp/%s.gif" % temp)
filename = "%s.gif" % temp
out = open("tmp/%s.gif" % temp, "rb")
else:
out = mobj
two.api.update_with_media(filename, update.message.caption, file=mobj)
two.api.update_with_media(filename, update.message.caption, file=out)
out.close()
else:
two.tweet(' '.join(args))
@ -219,6 +232,7 @@ if __name__ == "__main__":
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_handler(telegram.ext.MessageHandler(telegram.ext.Filters.document, tweet))
updater.dispatcher.add_error_handler(log)