diff --git a/delojza.py b/delojza.py index b21b414..0b9504e 100755 --- a/delojza.py +++ b/delojza.py @@ -6,6 +6,7 @@ import os import pprint import re import shutil +import subprocess import sys import tempfile import unicodedata @@ -14,6 +15,7 @@ from datetime import datetime, timedelta from glob import glob from operator import itemgetter from random import random +from time import sleep import acoustid import filetype @@ -72,6 +74,8 @@ class DelojzaBot: if tumblr_name and tumblr_keys: self.tumblr_name = tumblr_name self.tumblr_client = pytumblr.TumblrRestClient(*tumblr_keys) + else: + self.tumblr_client = None self.protected_chats = protected_chats or [] self.protected_tags = protected_tags or [] @@ -188,15 +192,19 @@ class DelojzaBot: 'preferredcodec': 'mp3', 'preferredquality': '256' }] + ytdl['postprocessor_args'] = ['-ar', '44100'] filenames = [] with youtube_dl.YoutubeDL(ytdl) as ytdl: + attempts = 0 while True: try: ytdl.download(urls) break except DownloadError as exc: - if '403' in str(exc): + attempts += 1 + if '403' in str(exc) and attempts < 5: self.logger.warning("Received a 403!") + sleep(1.357) if self.markov: message.reply_text(self.markov.make_sentence) else: @@ -325,8 +333,16 @@ class DelojzaBot: now = cmd_hashtag == 'TUMBLR_NOW' reply = '(btw, {})'.format("***FIRING TO TUMBLR RIGHT AWAY***" if now else "queueing to tumblr") message.reply_text(reply, parse_mode=telegram.ParseMode.MARKDOWN) - for filetitle in filenames: - response = self.tumblr_client.create_photo(self.tumblr_name, data=filetitle, + for filename in filenames: + if filename.endswith(".mp4"): + try: + output_filename = filename[:-len(".mp4")] + ".gif" + subprocess.check_output(['ffmpeg', '-i', filename, output_filename]) + filename = output_filename + except subprocess.CalledProcessError: + message.reply_text("Conversion to gif failed, sorry! Check log...") + continue + response = self.tumblr_client.create_photo(self.tumblr_name, data=filename, state="published" if now else "queue") if 'id' in response: tumblr_ids.append(response['id']) diff --git a/requirements.txt b/requirements.txt index 93dfd7e..0f2ad13 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,27 +2,27 @@ # This file is autogenerated by pip-compile # To update, run: # -# pip-compile +# pip-compile --upgrade # asn1crypto==0.24.0 # via cryptography -audioread==2.1.6 # via pyacoustid +audioread==2.1.8 # via pyacoustid certifi==2019.3.9 # via python-telegram-bot, requests cffi==1.12.3 # via cryptography chardet==3.0.4 # via requests -cryptography==2.6.1 # via python-telegram-bot +cryptography==2.7 # via python-telegram-bot filetype==1.0.5 future==0.17.1 # via python-telegram-bot, pytumblr idna==2.8 # via requests markovify==0.7.1 mutagen==1.42.0 oauthlib==3.0.1 # via requests-oauthlib -pyacoustid==1.1.5 +pyacoustid==1.1.7 pycparser==2.19 # via cffi python-telegram-bot==11.1.0 pytumblr==0.0.8 requests-oauthlib==1.2.0 # via pytumblr -requests==2.21.0 +requests==2.22.0 six==1.12.0 # via cryptography unidecode==1.0.23 # via markovify -urllib3==1.24.2 # via requests -youtube-dl==2019.4.30 +urllib3==1.25.3 # via requests +youtube-dl==2019.5.20