From 60601ea061b03e3e820cfb5e7658a1d0113b4d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Fri, 31 May 2019 17:53:40 +0200 Subject: [PATCH 1/5] avoid infinite loops in case of 403 errors --- delojza.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/delojza.py b/delojza.py index 4f6a9bf..01854b5 100755 --- a/delojza.py +++ b/delojza.py @@ -14,6 +14,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 @@ -190,13 +191,16 @@ class DelojzaBot: }] 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: From 29dcef7e29a0642e863483296326254cdefbc3cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Fri, 31 May 2019 17:55:04 +0200 Subject: [PATCH 2/5] `pip-compile --upgrade` --- requirements.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 From ff5526eeed5c4ad3ec0ceda34d9724b3f7f1bd36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Fri, 31 May 2019 17:57:31 +0200 Subject: [PATCH 3/5] wrangle all audio files to 44.1k for proper crossfading --- delojza.py | 1 + 1 file changed, 1 insertion(+) diff --git a/delojza.py b/delojza.py index 01854b5..689efd9 100755 --- a/delojza.py +++ b/delojza.py @@ -189,6 +189,7 @@ class DelojzaBot: 'preferredcodec': 'mp3', 'preferredquality': '256' }] + ytdl['postprocessor_args'] = ['-ar', '44100'] filenames = [] with youtube_dl.YoutubeDL(ytdl) as ytdl: attempts = 0 From 45a7594ef3e725b140916f1189036465e2d52103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Fri, 31 May 2019 19:39:43 +0200 Subject: [PATCH 4/5] fix crash on no Tumblr --- delojza.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/delojza.py b/delojza.py index 689efd9..f7d0f35 100755 --- a/delojza.py +++ b/delojza.py @@ -73,6 +73,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 [] From a33e3417947cf5e5001fb3d19df62496b8c439ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Fri, 31 May 2019 19:43:22 +0200 Subject: [PATCH 5/5] upload mp4s to tumblr as gifs (fixes #7) --- delojza.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/delojza.py b/delojza.py index f7d0f35..1194b1d 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 @@ -332,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'])