From e7ad85fac9b09b75ba576bb65de1ea715b07e085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Wed, 1 May 2019 10:50:21 +0200 Subject: [PATCH] add mutagen / tagging --- delojza.py | 25 +++++++++++++++++++++---- requirements.in | 1 + requirements.txt | 3 ++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/delojza.py b/delojza.py index 3e80e9e..5b0aa45 100755 --- a/delojza.py +++ b/delojza.py @@ -11,6 +11,7 @@ from glob import glob import filetype import markovify +import mutagen.id3 import pytumblr import requests import youtube_dl @@ -70,7 +71,7 @@ class DelojzaBot: return True return False - def download_ytdl(self, urls, subdir, date, extract=False, filename=None): + def download_ytdl(self, urls, subdir, date, message, extract=False, filename=None): ydl_opts = { 'noplaylist': True, 'restrictfilenames': True, @@ -86,14 +87,30 @@ class DelojzaBot: with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download(urls) out_dir = os.path.join(self.out_dir, subdir) - for filename in map(ydl.prepare_filename, map(ydl.extract_info, urls)): + for info in [ydl.extract_info(url, download=False) for url in urls]: + filename = ydl.prepare_filename(info) globbeds = glob(os.path.splitext(filename)[0] + '.*') for globbed in globbeds: + if globbed.endswith("mp3"): + title = info['track'] or (info['title'].split("-")[1] + if "-" in info['title'] else info['title']) + title = title.strip() + artist = info['artist'] or (info['title'].split("-")[0] + if "-" in info['title'] else info['title']) + artist = artist.strip() + message.reply_text("Tagging as \"{}\" by \"{}\"".format(title, artist)) + self.logger.info("Tagging %s w/ $s - $s...".format(globbed, title, artist)) + id3 = mutagen.id3.ID3(globbed) + id3.add(mutagen.id3.TIT2(encoding=3, text=title)) + if artist and (not info['artist'] and artist != title): + id3.add(mutagen.id3.TOPE(encoding=3, text=artist)) + id3.add(mutagen.id3.TPE1(encoding=3, text=artist)) + id3.save() self.logger.info("Moving %s to %s..." % (globbed, out_dir)) shutil.move(globbed, out_dir) return [] - def download_raw(self, urls, subdir, date, extract=False, filename=None): + def download_raw(self, urls, subdir, date, _, extract=False, filename=None): filenames = [] for url in urls: local_filename = os.path.join(self.out_dir, subdir, @@ -163,7 +180,7 @@ class DelojzaBot: message.reply_text(reply) - filenames = download_fn(urls, hashtag or '.', message.date, extract=extract, filename=filename) + filenames = download_fn(urls, hashtag or '.', message.date, message, extract=extract, filename=filename) if hashtag == 'TUMBLR' and self.client: message.reply_text('(btw, queueing to tumblr)') for filename in filenames: diff --git a/requirements.in b/requirements.in index 65f5c9b..119de12 100644 --- a/requirements.in +++ b/requirements.in @@ -2,5 +2,6 @@ python-telegram-bot youtube-dl requests filetype +mutagen markovify pytumblr \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index bfff790..71f8606 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ # This file is autogenerated by pip-compile # To update, run: # -# pip-compile --output-file requirements.txt requirements.in +# pip-compile # asn1crypto==0.24.0 # via cryptography certifi==2018.8.24 # via python-telegram-bot, requests @@ -13,6 +13,7 @@ filetype==1.0.1 future==0.16.0 # via python-telegram-bot, pytumblr idna==2.7 # via cryptography, requests markovify==0.7.1 +mutagen==1.42.0 oauthlib==2.1.0 # via requests-oauthlib pycparser==2.19 # via cffi python-telegram-bot==11.1.0