diff --git a/setup.py b/setup.py index ea9513a..3c98c65 100755 --- a/setup.py +++ b/setup.py @@ -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") diff --git a/telegrambot.py b/telegrambot.py index 5f2d326..1c73b6d 100755 --- a/telegrambot.py +++ b/telegrambot.py @@ -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)