mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Refactor metadata
This commit is contained in:
186
core/metadata.py
186
core/metadata.py
@@ -12,12 +12,9 @@ def compare(music_file, metadata):
|
|||||||
try:
|
try:
|
||||||
if music_file.endswith('.mp3'):
|
if music_file.endswith('.mp3'):
|
||||||
audiofile = EasyID3(music_file)
|
audiofile = EasyID3(music_file)
|
||||||
# fetch track title metadata
|
|
||||||
already_tagged = audiofile['title'][0] == metadata['name']
|
already_tagged = audiofile['title'][0] == metadata['name']
|
||||||
elif music_file.endswith('.m4a'):
|
elif music_file.endswith('.m4a'):
|
||||||
tags = {'title': '\xa9nam'}
|
|
||||||
audiofile = MP4(music_file)
|
audiofile = MP4(music_file)
|
||||||
# fetch track title metadata
|
|
||||||
already_tagged = audiofile[tags['title']] == metadata['name']
|
already_tagged = audiofile[tags['title']] == metadata['name']
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
pass
|
pass
|
||||||
@@ -26,102 +23,107 @@ def compare(music_file, metadata):
|
|||||||
|
|
||||||
def embed(music_file, meta_tags):
|
def embed(music_file, meta_tags):
|
||||||
""" Embed metadata. """
|
""" Embed metadata. """
|
||||||
if meta_tags is None:
|
embedder = EmbedMetadata(music_file, meta_tags)
|
||||||
log.warning('Could not find metadata')
|
if music_file.endswith('.m4a'):
|
||||||
return None
|
|
||||||
elif music_file.endswith('.m4a'):
|
|
||||||
log.info('Applying metadata')
|
log.info('Applying metadata')
|
||||||
return embed_m4a(music_file, meta_tags)
|
return embedder.m4a()
|
||||||
elif music_file.endswith('.mp3'):
|
elif music_file.endswith('.mp3'):
|
||||||
log.info('Applying metadata')
|
log.info('Applying metadata')
|
||||||
return embed_mp3(music_file, meta_tags)
|
return embedder.mp3()
|
||||||
else:
|
else:
|
||||||
log.warning('Cannot embed metadata into given output extension')
|
log.warning('Cannot embed metadata into given output extension')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
class EmbedMetadata:
|
||||||
|
def __init__(self, music_file, meta_tags):
|
||||||
|
self.music_file = music_file
|
||||||
|
self.meta_tags = meta_tags
|
||||||
|
|
||||||
def embed_mp3(music_file, meta_tags):
|
def mp3(self):
|
||||||
""" Embed metadata to MP3 files. """
|
""" Embed metadata to MP3 files. """
|
||||||
# EasyID3 is fun to use ;)
|
music_file = self.music_file
|
||||||
audiofile = EasyID3(music_file)
|
meta_tags = self.meta_tags
|
||||||
audiofile['artist'] = meta_tags['artists'][0]['name']
|
# EasyID3 is fun to use ;)
|
||||||
audiofile['albumartist'] = meta_tags['artists'][0]['name']
|
audiofile = EasyID3(music_file)
|
||||||
audiofile['album'] = meta_tags['album']['name']
|
audiofile['artist'] = meta_tags['artists'][0]['name']
|
||||||
audiofile['title'] = meta_tags['name']
|
audiofile['albumartist'] = meta_tags['artists'][0]['name']
|
||||||
audiofile['tracknumber'] = [meta_tags['track_number'],
|
audiofile['album'] = meta_tags['album']['name']
|
||||||
meta_tags['total_tracks']]
|
audiofile['title'] = meta_tags['name']
|
||||||
audiofile['discnumber'] = [meta_tags['disc_number'], 0]
|
audiofile['tracknumber'] = [meta_tags['track_number'],
|
||||||
audiofile['date'] = meta_tags['release_date']
|
meta_tags['total_tracks']]
|
||||||
audiofile['originaldate'] = meta_tags['release_date']
|
audiofile['discnumber'] = [meta_tags['disc_number'], 0]
|
||||||
audiofile['media'] = meta_tags['type']
|
audiofile['date'] = meta_tags['release_date']
|
||||||
audiofile['author'] = meta_tags['artists'][0]['name']
|
audiofile['originaldate'] = meta_tags['release_date']
|
||||||
audiofile['lyricist'] = meta_tags['artists'][0]['name']
|
audiofile['media'] = meta_tags['type']
|
||||||
audiofile['arranger'] = meta_tags['artists'][0]['name']
|
audiofile['author'] = meta_tags['artists'][0]['name']
|
||||||
audiofile['performer'] = meta_tags['artists'][0]['name']
|
audiofile['lyricist'] = meta_tags['artists'][0]['name']
|
||||||
audiofile['website'] = meta_tags['external_urls']['spotify']
|
audiofile['arranger'] = meta_tags['artists'][0]['name']
|
||||||
audiofile['length'] = str(meta_tags['duration_ms'] / 1000)
|
audiofile['performer'] = meta_tags['artists'][0]['name']
|
||||||
if meta_tags['publisher']:
|
audiofile['website'] = meta_tags['external_urls']['spotify']
|
||||||
audiofile['encodedby'] = meta_tags['publisher']
|
audiofile['length'] = str(meta_tags['duration_ms'] / 1000)
|
||||||
if meta_tags['genre']:
|
if meta_tags['publisher']:
|
||||||
audiofile['genre'] = meta_tags['genre']
|
audiofile['encodedby'] = meta_tags['publisher']
|
||||||
if meta_tags['copyright']:
|
if meta_tags['genre']:
|
||||||
audiofile['copyright'] = meta_tags['copyright']
|
audiofile['genre'] = meta_tags['genre']
|
||||||
if meta_tags['external_ids']['isrc']:
|
if meta_tags['copyright']:
|
||||||
audiofile['isrc'] = meta_tags['external_ids']['isrc']
|
audiofile['copyright'] = meta_tags['copyright']
|
||||||
audiofile.save(v2_version=3)
|
if meta_tags['external_ids']['isrc']:
|
||||||
audiofile = ID3(music_file)
|
audiofile['isrc'] = meta_tags['external_ids']['isrc']
|
||||||
try:
|
audiofile.save(v2_version=3)
|
||||||
albumart = urllib.request.urlopen(meta_tags['album']['images'][0]['url'])
|
audiofile = ID3(music_file)
|
||||||
audiofile["APIC"] = APIC(encoding=3, mime='image/jpeg', type=3,
|
try:
|
||||||
desc=u'Cover', data=albumart.read())
|
albumart = urllib.request.urlopen(meta_tags['album']['images'][0]['url'])
|
||||||
albumart.close()
|
audiofile["APIC"] = APIC(encoding=3, mime='image/jpeg', type=3,
|
||||||
except IndexError:
|
desc=u'Cover', data=albumart.read())
|
||||||
pass
|
albumart.close()
|
||||||
audiofile.save(v2_version=3)
|
except IndexError:
|
||||||
return True
|
pass
|
||||||
|
audiofile.save(v2_version=3)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def m4a(self):
|
||||||
|
""" Embed metadata to M4A files. """
|
||||||
|
music_file = self.music_file
|
||||||
|
meta_tags = self.meta_tags
|
||||||
|
# Apple has specific tags - see mutagen docs -
|
||||||
|
# http://mutagen.readthedocs.io/en/latest/api/mp4.html
|
||||||
|
tags = {'album': '\xa9alb',
|
||||||
|
'artist': '\xa9ART',
|
||||||
|
'date': '\xa9day',
|
||||||
|
'title': '\xa9nam',
|
||||||
|
'originaldate': 'purd',
|
||||||
|
'comment': '\xa9cmt',
|
||||||
|
'group': '\xa9grp',
|
||||||
|
'writer': '\xa9wrt',
|
||||||
|
'genre': '\xa9gen',
|
||||||
|
'tracknumber': 'trkn',
|
||||||
|
'albumartist': 'aART',
|
||||||
|
'disknumber': 'disk',
|
||||||
|
'cpil': 'cpil',
|
||||||
|
'albumart': 'covr',
|
||||||
|
'copyright': 'cprt',
|
||||||
|
'tempo': 'tmpo'}
|
||||||
|
|
||||||
def embed_m4a(music_file, meta_tags):
|
audiofile = MP4(music_file)
|
||||||
""" Embed metadata to M4A files. """
|
audiofile[tags['artist']] = meta_tags['artists'][0]['name']
|
||||||
# Apple has specific tags - see mutagen docs -
|
audiofile[tags['albumartist']] = meta_tags['artists'][0]['name']
|
||||||
# http://mutagen.readthedocs.io/en/latest/api/mp4.html
|
audiofile[tags['album']] = meta_tags['album']['name']
|
||||||
tags = {'album': '\xa9alb',
|
audiofile[tags['title']] = meta_tags['name']
|
||||||
'artist': '\xa9ART',
|
audiofile[tags['tracknumber']] = [(meta_tags['track_number'],
|
||||||
'date': '\xa9day',
|
meta_tags['total_tracks'])]
|
||||||
'title': '\xa9nam',
|
audiofile[tags['disknumber']] = [(meta_tags['disc_number'], 0)]
|
||||||
'originaldate': 'purd',
|
audiofile[tags['date']] = meta_tags['release_date']
|
||||||
'comment': '\xa9cmt',
|
audiofile[tags['originaldate']] = meta_tags['release_date']
|
||||||
'group': '\xa9grp',
|
if meta_tags['genre']:
|
||||||
'writer': '\xa9wrt',
|
audiofile[tags['genre']] = meta_tags['genre']
|
||||||
'genre': '\xa9gen',
|
if meta_tags['copyright']:
|
||||||
'tracknumber': 'trkn',
|
audiofile[tags['copyright']] = meta_tags['copyright']
|
||||||
'albumartist': 'aART',
|
try:
|
||||||
'disknumber': 'disk',
|
albumart = urllib.request.urlopen(meta_tags['album']['images'][0]['url'])
|
||||||
'cpil': 'cpil',
|
audiofile[tags['albumart']] = [MP4Cover(
|
||||||
'albumart': 'covr',
|
albumart.read(), imageformat=MP4Cover.FORMAT_JPEG)]
|
||||||
'copyright': 'cprt',
|
albumart.close()
|
||||||
'tempo': 'tmpo'}
|
except IndexError:
|
||||||
|
pass
|
||||||
audiofile = MP4(music_file)
|
audiofile.save()
|
||||||
audiofile[tags['artist']] = meta_tags['artists'][0]['name']
|
return True
|
||||||
audiofile[tags['albumartist']] = meta_tags['artists'][0]['name']
|
|
||||||
audiofile[tags['album']] = meta_tags['album']['name']
|
|
||||||
audiofile[tags['title']] = meta_tags['name']
|
|
||||||
audiofile[tags['tracknumber']] = [(meta_tags['track_number'],
|
|
||||||
meta_tags['total_tracks'])]
|
|
||||||
audiofile[tags['disknumber']] = [(meta_tags['disc_number'], 0)]
|
|
||||||
audiofile[tags['date']] = meta_tags['release_date']
|
|
||||||
audiofile[tags['originaldate']] = meta_tags['release_date']
|
|
||||||
if meta_tags['genre']:
|
|
||||||
audiofile[tags['genre']] = meta_tags['genre']
|
|
||||||
if meta_tags['copyright']:
|
|
||||||
audiofile[tags['copyright']] = meta_tags['copyright']
|
|
||||||
try:
|
|
||||||
albumart = urllib.request.urlopen(meta_tags['album']['images'][0]['url'])
|
|
||||||
audiofile[tags['albumart']] = [MP4Cover(
|
|
||||||
albumart.read(), imageformat=MP4Cover.FORMAT_JPEG)]
|
|
||||||
albumart.close()
|
|
||||||
except IndexError:
|
|
||||||
pass
|
|
||||||
audiofile.save()
|
|
||||||
return True
|
|
||||||
|
|||||||
36
spotdl.py
36
spotdl.py
@@ -56,7 +56,7 @@ def generate_youtube_url(raw_song, meta_tags, tries_remaining=5):
|
|||||||
if args.music_videos_only:
|
if args.music_videos_only:
|
||||||
query['videoCategoryId'] = '10'
|
query['videoCategoryId'] = '10'
|
||||||
|
|
||||||
if meta_tags is None:
|
if not meta_tags:
|
||||||
song = raw_song
|
song = raw_song
|
||||||
query['q'] = song
|
query['q'] = song
|
||||||
else:
|
else:
|
||||||
@@ -79,7 +79,7 @@ def generate_youtube_url(raw_song, meta_tags, tries_remaining=5):
|
|||||||
'videotime':internals.videotime_from_seconds(duration_s),
|
'videotime':internals.videotime_from_seconds(duration_s),
|
||||||
'seconds': duration_s}
|
'seconds': duration_s}
|
||||||
videos.append(youtubedetails)
|
videos.append(youtubedetails)
|
||||||
if meta_tags is None:
|
if not meta_tags:
|
||||||
break
|
break
|
||||||
|
|
||||||
if not videos:
|
if not videos:
|
||||||
@@ -96,10 +96,10 @@ def generate_youtube_url(raw_song, meta_tags, tries_remaining=5):
|
|||||||
"http://youtube.com/watch?v="+v['link']))
|
"http://youtube.com/watch?v="+v['link']))
|
||||||
# let user select the song to download
|
# let user select the song to download
|
||||||
result = internals.input_link(videos)
|
result = internals.input_link(videos)
|
||||||
if result is None:
|
if not result:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
if meta_tags is None:
|
if not meta_tags:
|
||||||
# if the metadata could not be acquired, take the first result
|
# if the metadata could not be acquired, take the first result
|
||||||
# from Youtube because the proper song length is unknown
|
# from Youtube because the proper song length is unknown
|
||||||
result = videos[0]
|
result = videos[0]
|
||||||
@@ -139,10 +139,10 @@ def go_pafy(raw_song, meta_tags=None):
|
|||||||
else:
|
else:
|
||||||
track_url = generate_youtube_url(raw_song, meta_tags)
|
track_url = generate_youtube_url(raw_song, meta_tags)
|
||||||
|
|
||||||
if track_url is None:
|
if track_url:
|
||||||
track_info = None
|
|
||||||
else:
|
|
||||||
track_info = pafy.new(track_url)
|
track_info = pafy.new(track_url)
|
||||||
|
else:
|
||||||
|
track_info = None
|
||||||
|
|
||||||
return track_info
|
return track_info
|
||||||
|
|
||||||
@@ -150,10 +150,10 @@ def go_pafy(raw_song, meta_tags=None):
|
|||||||
def get_youtube_title(content, number=None):
|
def get_youtube_title(content, number=None):
|
||||||
""" Get the YouTube video's title. """
|
""" Get the YouTube video's title. """
|
||||||
title = content.title
|
title = content.title
|
||||||
if number is None:
|
if number:
|
||||||
return title
|
|
||||||
else:
|
|
||||||
return '{0}. {1}'.format(number, title)
|
return '{0}. {1}'.format(number, title)
|
||||||
|
else:
|
||||||
|
return title
|
||||||
|
|
||||||
|
|
||||||
def download_song(file_name, content):
|
def download_song(file_name, content):
|
||||||
@@ -163,15 +163,15 @@ def download_song(file_name, content):
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if link is None:
|
if link:
|
||||||
return False
|
|
||||||
else:
|
|
||||||
log.debug('Downloading from URL: ' + link.url)
|
log.debug('Downloading from URL: ' + link.url)
|
||||||
filepath = '{0}{1}'.format(os.path.join(args.folder, file_name),
|
filepath = '{0}{1}'.format(os.path.join(args.folder, file_name),
|
||||||
args.input_ext)
|
args.input_ext)
|
||||||
log.debug('Saving to: ' + filepath)
|
log.debug('Saving to: ' + filepath)
|
||||||
link.download(filepath=filepath)
|
link.download(filepath=filepath)
|
||||||
return True
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def check_exists(music_file, raw_song, meta_tags):
|
def check_exists(music_file, raw_song, meta_tags):
|
||||||
@@ -294,7 +294,7 @@ def grab_single(raw_song, number=None):
|
|||||||
meta_tags = spotify_tools.generate_metadata(raw_song)
|
meta_tags = spotify_tools.generate_metadata(raw_song)
|
||||||
content = go_pafy(raw_song, meta_tags)
|
content = go_pafy(raw_song, meta_tags)
|
||||||
|
|
||||||
if content is None:
|
if not content:
|
||||||
log.debug('Found no matching video')
|
log.debug('Found no matching video')
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -305,7 +305,7 @@ def grab_single(raw_song, number=None):
|
|||||||
# generate file name of the song to download
|
# generate file name of the song to download
|
||||||
songname = content.title
|
songname = content.title
|
||||||
|
|
||||||
if meta_tags is not None:
|
if meta_tags:
|
||||||
refined_songname = generate_songname(meta_tags)
|
refined_songname = generate_songname(meta_tags)
|
||||||
log.debug('Refining songname from "{0}" to "{1}"'.format(songname, refined_songname))
|
log.debug('Refining songname from "{0}" to "{1}"'.format(songname, refined_songname))
|
||||||
if not refined_songname == ' - ':
|
if not refined_songname == ' - ':
|
||||||
@@ -333,8 +333,12 @@ def grab_single(raw_song, number=None):
|
|||||||
|
|
||||||
if not args.input_ext == args.output_ext:
|
if not args.input_ext == args.output_ext:
|
||||||
os.remove(os.path.join(args.folder, input_song))
|
os.remove(os.path.join(args.folder, input_song))
|
||||||
|
|
||||||
if not args.no_metadata:
|
if not args.no_metadata:
|
||||||
metadata.embed(os.path.join(args.folder, output_song), meta_tags)
|
if metadata:
|
||||||
|
metadata.embed(os.path.join(args.folder, output_song), meta_tags)
|
||||||
|
else:
|
||||||
|
log.warning('Could not find metadata')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
log.error('No audio streams available')
|
log.error('No audio streams available')
|
||||||
|
|||||||
Reference in New Issue
Block a user