add mutagen / tagging

This commit is contained in:
Tomáš Mládek 2019-05-01 10:50:21 +02:00 committed by Tomáš Mládek
parent 4c7c43f39f
commit e7ad85fac9
3 changed files with 24 additions and 5 deletions

View file

@ -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:

View file

@ -2,5 +2,6 @@ python-telegram-bot
youtube-dl
requests
filetype
mutagen
markovify
pytumblr

View file

@ -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