De-clutter metadata search

This commit is contained in:
Ritiek Malhotra
2020-05-07 19:36:38 +05:30
parent ec5704e050
commit c9bf0bc020
8 changed files with 410 additions and 166 deletions

View File

@@ -50,7 +50,7 @@ class EmbedderDefault(EmbedderBase):
def as_mp3(self, path, metadata, cached_albumart=None):
""" Embed metadata to MP3 files. """
logger.debug('Writing MP3 metadata to "{path}"'.format(path=path))
logger.debug('Writing MP3 metadata to "{path}".'.format(path=path))
# EasyID3 is fun to use ;)
# For supported easyid3 tags:
# https://github.com/quodlibet/mutagen/blob/master/mutagen/easyid3.py
@@ -108,7 +108,7 @@ class EmbedderDefault(EmbedderBase):
def as_m4a(self, path, metadata, cached_albumart=None):
""" Embed metadata to M4A files. """
logger.debug('Writing M4A metadata to "{path}"'.format(path=path))
logger.debug('Writing M4A metadata to "{path}".'.format(path=path))
audiofile = MP4(path)
self._embed_basic_metadata(audiofile, metadata, "m4a", preset=M4A_TAG_PRESET)
if metadata["year"]:
@@ -132,7 +132,7 @@ class EmbedderDefault(EmbedderBase):
audiofile.save()
def as_flac(self, path, metadata, cached_albumart=None):
logger.debug('Writing FLAC metadata to "{path}"'.format(path=path))
logger.debug('Writing FLAC metadata to "{path}".'.format(path=path))
audiofile = FLAC(path)
self._embed_basic_metadata(audiofile, metadata, "flac")
if metadata["year"]:

View File

@@ -5,6 +5,7 @@ from spotdl.metadata import ProviderBase
from spotdl.metadata.exceptions import SpotifyMetadataNotFoundError
from spotdl.authorize.services import AuthorizeSpotify
import spotdl.util
import logging
logger = logging.getLogger(__name__)
@@ -26,15 +27,18 @@ class ProviderSpotify(ProviderBase):
return self.metadata_to_standard_form(metadata)
def from_query(self, query):
tracks = self.spotify.search(query, limit=1)["tracks"]["items"]
tracks = self.search(query)["tracks"]["items"]
if not tracks:
raise SpotifyMetadataNotFoundError(
'Could not find any tracks matching the given search query ("{}")'.format(
'Spotify returned no tracks for the search query "{}".'.format(
query,
)
)
return self.metadata_to_standard_form(tracks[0])
def search(self, query):
return self.spotify.search(query)
def _generate_token(self, client_id, client_secret):
""" Generate the token. """
credentials = oauth2.SpotifyClientCredentials(
@@ -43,15 +47,12 @@ class ProviderSpotify(ProviderBase):
token = credentials.get_access_token()
return token
def _titlecase(self, string):
return " ".join(word.capitalize() for word in string.split())
def metadata_to_standard_form(self, metadata):
artist = self.spotify.artist(metadata["artists"][0]["id"])
album = self.spotify.album(metadata["album"]["id"])
try:
metadata[u"genre"] = self._titlecase(artist["genres"][0])
metadata[u"genre"] = spotdl.util.titlecase(artist["genres"][0])
except IndexError:
metadata[u"genre"] = None
try:
@@ -78,3 +79,4 @@ class ProviderSpotify(ProviderBase):
del metadata["album"]["available_markets"]
return metadata

View File

@@ -108,10 +108,6 @@ class YouTubeSearch:
logger.debug('Fetching YouTube results for "{}".'.format(search_url))
html = self._fetch_response_html(search_url)
videos = self._fetch_search_results(html)
# print(html)
# print("")
# print(videos)
# exit()
return YouTubeVideos(videos)
@@ -198,7 +194,7 @@ class YouTubeStreams(StreamsBase):
class ProviderYouTube(ProviderBase):
def from_query(self, query):
watch_urls = YouTubeSearch().search(query)
watch_urls = self.search(query)
if not watch_urls:
raise YouTubeMetadataNotFoundError(
'YouTube returned nothing for the given search '
@@ -214,6 +210,9 @@ class ProviderYouTube(ProviderBase):
def from_pytube_object(self, content):
return self.metadata_to_standard_form(content)
def search(self, query):
return YouTubeSearch().search(query)
def _fetch_publish_date(self, content):
# FIXME: This needs to be supported in PyTube itself
# See https://github.com/nficano/pytube/issues/595