mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Faster generation of m3u playlists
This commit is contained in:
@@ -121,43 +121,36 @@ class Spotdl:
|
|||||||
target_file = "{}.m3u".format(track_file.split(".")[0])
|
target_file = "{}.m3u".format(track_file.split(".")[0])
|
||||||
|
|
||||||
total_tracks = len(tracks)
|
total_tracks = len(tracks)
|
||||||
logger.info("Generating {0} from {1} YouTube URLs".format(target_file, total_tracks))
|
logger.info("Generating {0} from {1} YouTube URLs.".format(target_file, total_tracks))
|
||||||
with open(target_file, "w") as output_file:
|
with open(target_file, "w") as output_file:
|
||||||
output_file.write("#EXTM3U\n\n")
|
output_file.write("#EXTM3U\n\n")
|
||||||
|
|
||||||
youtube_searcher = YouTubeSearch()
|
|
||||||
videos = []
|
videos = []
|
||||||
for n, track in enumerate(tracks, 1):
|
for n, track in enumerate(tracks, 1):
|
||||||
try:
|
try:
|
||||||
search_results = youtube_searcher.search(search_query)
|
search_metadata = MetadataSearch(
|
||||||
if not search_results:
|
track,
|
||||||
raise NoYouTubeVideoFoundError(
|
lyrics=False,
|
||||||
'YouTube returned no videos for the search query "{}".'.format(search_query)
|
yt_search_format=self.arguments["search_format"],
|
||||||
)
|
yt_manual=self.arguments["manual"]
|
||||||
video = search_results.bestmatch()
|
)
|
||||||
if not video:
|
video = search_metadata.best_on_youtube_search()
|
||||||
raise NoYouTubeVideoMatchError(
|
|
||||||
'No matching videos found on YouTube for the search query "{}".'.format(search_query)
|
|
||||||
)
|
|
||||||
except (NoYouTubeVideoFoundError, NoYouTubeVideoMatchError) as e:
|
except (NoYouTubeVideoFoundError, NoYouTubeVideoMatchError) as e:
|
||||||
logger.error(e.args[0])
|
logger.error(e.args[0])
|
||||||
else:
|
else:
|
||||||
print(video)
|
|
||||||
exit()
|
|
||||||
logger.info(
|
logger.info(
|
||||||
"Matched track {0}/{1} ({2})".format(
|
"Matched track {0}/{1} ({2})".format(
|
||||||
str(n).zfill(len(str(total_tracks))),
|
str(n).zfill(len(str(total_tracks))),
|
||||||
total_tracks,
|
total_tracks,
|
||||||
content.watchv_url
|
video["url"],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
logger.debug(track)
|
|
||||||
m3u_key = "#EXTINF:{duration},{title}\n{youtube_url}\n".format(
|
m3u_key = "#EXTINF:{duration},{title}\n{youtube_url}\n".format(
|
||||||
duration=internals.get_sec(content.duration),
|
duration=spotdl.util.get_sec(video["duration"]),
|
||||||
title=content.title,
|
title=video["title"],
|
||||||
youtube_url=content.watchv_url,
|
youtube_url=video["url"],
|
||||||
)
|
)
|
||||||
logger.debug(m3u_key)
|
logger.debug(m3u_key.strip())
|
||||||
with open(target_file, "a") as output_file:
|
with open(target_file, "a") as output_file:
|
||||||
output_file.write(m3u_key)
|
output_file.write(m3u_key)
|
||||||
|
|
||||||
|
|||||||
@@ -138,6 +138,45 @@ class MetadataSearch:
|
|||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
|
|
||||||
|
def best_on_youtube_search(self):
|
||||||
|
track_type_mapper = {
|
||||||
|
"spotify": self._best_on_youtube_search_for_type_spotify,
|
||||||
|
"youtube": self._best_on_youtube_search_for_type_youtube,
|
||||||
|
"query": self._best_on_youtube_search_for_type_query,
|
||||||
|
}
|
||||||
|
caller = track_type_mapper[self.track_type]
|
||||||
|
video = caller(self.track)
|
||||||
|
return video
|
||||||
|
|
||||||
|
def _best_on_youtube_search_for_type_query(self, query):
|
||||||
|
videos = self.providers["youtube"].search(query)
|
||||||
|
if not videos:
|
||||||
|
raise NoYouTubeVideoFoundError(
|
||||||
|
'YouTube returned no videos for the search query "{}".'.format(query)
|
||||||
|
)
|
||||||
|
if self.yt_manual:
|
||||||
|
video = prompt_for_youtube_search_result(videos)
|
||||||
|
else:
|
||||||
|
video = videos.bestmatch()
|
||||||
|
|
||||||
|
if video is None:
|
||||||
|
raise NoYouTubeVideoMatchError(
|
||||||
|
'No matching videos found on YouTube for the search query "{}".'.format(
|
||||||
|
search_query
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return video
|
||||||
|
|
||||||
|
def _best_on_youtube_search_for_type_youtube(self, url):
|
||||||
|
video = self._best_on_youtube_search_for_type_query(url)
|
||||||
|
return video
|
||||||
|
|
||||||
|
def _best_on_youtube_search_for_type_spotify(self, url):
|
||||||
|
spotify_metadata = self._on_spotify_for_type_spotify(self.track)
|
||||||
|
search_query = spotdl.metadata.format_string(self.yt_search_format, spotify_metadata)
|
||||||
|
video = self._best_on_youtube_search_for_type_query(search_query)
|
||||||
|
return video
|
||||||
|
|
||||||
def _on_youtube_and_spotify_for_type_spotify(self):
|
def _on_youtube_and_spotify_for_type_spotify(self):
|
||||||
logger.debug("Extracting YouTube and Spotify metadata for input Spotify URI.")
|
logger.debug("Extracting YouTube and Spotify metadata for input Spotify URI.")
|
||||||
spotify_metadata = self._on_spotify_for_type_spotify(self.track)
|
spotify_metadata = self._on_spotify_for_type_spotify(self.track)
|
||||||
@@ -146,23 +185,7 @@ class MetadataSearch:
|
|||||||
spotify_metadata,
|
spotify_metadata,
|
||||||
)
|
)
|
||||||
search_query = spotdl.metadata.format_string(self.yt_search_format, spotify_metadata)
|
search_query = spotdl.metadata.format_string(self.yt_search_format, spotify_metadata)
|
||||||
videos = self.providers["youtube"].search(search_query)
|
youtube_video = self._best_on_youtube_search_for_type_spotify(search_query)
|
||||||
if not videos:
|
|
||||||
raise NoYouTubeVideoFoundError(
|
|
||||||
'YouTube returned no videos for the search query "{}".'.format(search_query)
|
|
||||||
)
|
|
||||||
if self.yt_manual:
|
|
||||||
youtube_video = prompt_for_youtube_search_result(videos)
|
|
||||||
else:
|
|
||||||
youtube_video = videos.bestmatch()
|
|
||||||
|
|
||||||
if youtube_video is None:
|
|
||||||
raise NoYouTubeVideoMatchError(
|
|
||||||
'No matching videos found on YouTube for the search query "{}".'.format(
|
|
||||||
search_query
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
youtube_metadata = self.providers["youtube"].from_url(youtube_video["url"])
|
youtube_metadata = self.providers["youtube"].from_url(youtube_video["url"])
|
||||||
metadata = spotdl.util.merge(
|
metadata = spotdl.util.merge(
|
||||||
youtube_metadata,
|
youtube_metadata,
|
||||||
@@ -207,23 +230,7 @@ class MetadataSearch:
|
|||||||
spotify_metadata,
|
spotify_metadata,
|
||||||
)
|
)
|
||||||
search_query = spotdl.metadata.format_string(self.yt_search_format, spotify_metadata)
|
search_query = spotdl.metadata.format_string(self.yt_search_format, spotify_metadata)
|
||||||
videos = self.providers["youtube"].search(search_query)
|
youtube_video = self._best_on_youtube_search_for_type_spotify(search_query)
|
||||||
if not videos:
|
|
||||||
raise NoYouTubeVideoFoundError(
|
|
||||||
'YouTube returned no videos for the search query "{}".'.format(search_query)
|
|
||||||
)
|
|
||||||
if self.yt_manual:
|
|
||||||
youtube_video = prompt_for_youtube_search_result(videos)
|
|
||||||
else:
|
|
||||||
youtube_video = videos.bestmatch()
|
|
||||||
|
|
||||||
if youtube_video is None:
|
|
||||||
raise NoYouTubeVideoMatchError(
|
|
||||||
'No matching videos found on YouTube for the search query "{}".'.format(
|
|
||||||
search_query
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
youtube_metadata = self.providers["youtube"].from_url(youtube_video["url"])
|
youtube_metadata = self.providers["youtube"].from_url(youtube_video["url"])
|
||||||
return youtube_metadata
|
return youtube_metadata
|
||||||
|
|
||||||
@@ -234,21 +241,7 @@ class MetadataSearch:
|
|||||||
|
|
||||||
def _on_youtube_for_type_query(self, query):
|
def _on_youtube_for_type_query(self, query):
|
||||||
logger.debug("Extracting YouTube metadata for input track query.")
|
logger.debug("Extracting YouTube metadata for input track query.")
|
||||||
videos = self.providers["youtube"].search(query)
|
youtube_video = self._best_on_youtube_search_for_type_query(query)
|
||||||
if not videos:
|
|
||||||
raise NoYouTubeVideoFoundError(
|
|
||||||
'YouTube returned no videos for the search query "{}".'.format(query)
|
|
||||||
)
|
|
||||||
if self.yt_manual:
|
|
||||||
youtube_video = prompt_for_youtube_search_result(videos)
|
|
||||||
else:
|
|
||||||
youtube_video = videos.bestmatch()
|
|
||||||
if youtube_video is None:
|
|
||||||
raise NoYouTubeVideoMatchError(
|
|
||||||
'No matching videos found on YouTube for the search query "{}".'.format(
|
|
||||||
query
|
|
||||||
)
|
|
||||||
)
|
|
||||||
youtube_metadata = self.providers["youtube"].from_url(youtube_video["url"])
|
youtube_metadata = self.providers["youtube"].from_url(youtube_video["url"])
|
||||||
return youtube_metadata
|
return youtube_metadata
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user