mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2026-02-07 00:56:40 +00:00
Custom YouTube search string (#261)
* Custom YouTube search string * Fix sorting issues on < Python 3.6
This commit is contained in:
@@ -23,6 +23,7 @@ default_conf = { 'spotify-downloader':
|
||||
'music-videos-only' : False,
|
||||
'no-spaces' : False,
|
||||
'file-format' : '{artist} - {track_name}',
|
||||
'search-format' : '{artist} - {track_name} lyrics',
|
||||
'youtube-api-key' : None,
|
||||
'log-level' : 'INFO' }
|
||||
}
|
||||
@@ -71,7 +72,8 @@ def override_config(config_file, parser, raw_args=None):
|
||||
parser.set_defaults(music_videos_only=config['music-videos-only'])
|
||||
parser.set_defaults(no_spaces=config['no-spaces'])
|
||||
parser.set_defaults(file_format=config['file-format'])
|
||||
parser.set_defaults(no_spaces=config['youtube-api-key'])
|
||||
parser.set_defaults(search_format=config['search-format'])
|
||||
parser.set_defaults(youtube_api_key=config['youtube-api-key'])
|
||||
parser.set_defaults(log_level=config['log-level'])
|
||||
|
||||
return parser.parse_args(raw_args)
|
||||
@@ -131,8 +133,12 @@ def get_arguments(raw_args=None, to_group=True, to_merge=True):
|
||||
'-ff', '--file-format', default=config['file-format'],
|
||||
help='File format to save the downloaded song with, each tag '
|
||||
'is surrounded by curly braces. Possible formats: '
|
||||
'{}'.format([internals.formats[x] for x in internals.formats]),
|
||||
action='store_true')
|
||||
'{}'.format([internals.formats[x] for x in internals.formats]))
|
||||
parser.add_argument(
|
||||
'-sf', '--search-format', default=config['search-format'],
|
||||
help='Search format to search for on YouTube, each tag '
|
||||
'is surrounded by curly braces. Possible formats: '
|
||||
'{}'.format([internals.formats[x] for x in internals.formats]))
|
||||
parser.add_argument(
|
||||
'-dm', '--download-only-metadata', default=config['download-only-metadata'],
|
||||
help='download songs for which metadata is found',
|
||||
|
||||
@@ -66,7 +66,7 @@ def is_youtube(raw_song):
|
||||
return status
|
||||
|
||||
|
||||
def generate_songname(file_format, tags):
|
||||
def format_string(string_format, tags):
|
||||
""" Generate a string of the format '[artist] - [song]' for the given spotify song. """
|
||||
format_tags = dict(formats)
|
||||
format_tags[0] = tags['name']
|
||||
@@ -83,13 +83,13 @@ def generate_songname(file_format, tags):
|
||||
format_tags[11] = tags['external_ids']['isrc']
|
||||
|
||||
for x in formats:
|
||||
file_format = file_format.replace('{' + formats[x] + '}',
|
||||
string_format = string_format.replace('{' + formats[x] + '}',
|
||||
str(format_tags[x]))
|
||||
|
||||
if const.args.no_spaces:
|
||||
file_format = file_format.replace(' ', '_')
|
||||
string_format = string_format.replace(' ', '_')
|
||||
|
||||
return file_format
|
||||
return string_format
|
||||
|
||||
|
||||
def sanitize_title(title):
|
||||
|
||||
@@ -68,12 +68,12 @@ def download_song(file_name, content):
|
||||
return False
|
||||
|
||||
|
||||
def generate_search_url(song):
|
||||
def generate_search_url(query):
|
||||
""" Generate YouTube search URL for the given song. """
|
||||
# urllib.request.quote() encodes URL with special characters
|
||||
song = urllib.request.quote(song)
|
||||
# urllib.request.quote() encodes string with special characters
|
||||
quoted_query = urllib.request.quote(query)
|
||||
# Special YouTube URL filter to search only for videos
|
||||
url = 'https://www.youtube.com/results?sp=EgIQAQ%253D%253D&q={0}'.format(song)
|
||||
url = 'https://www.youtube.com/results?sp=EgIQAQ%253D%253D&q={0}'.format(quoted_query)
|
||||
return url
|
||||
|
||||
|
||||
@@ -109,6 +109,12 @@ class GenerateYouTubeURL:
|
||||
self.raw_song = raw_song
|
||||
self.meta_tags = meta_tags
|
||||
|
||||
if meta_tags is None:
|
||||
self.search_query = raw_song
|
||||
else:
|
||||
self.search_query = internals.format_string(const.args.search_format,
|
||||
meta_tags)
|
||||
|
||||
def _best_match(self, videos):
|
||||
""" Select the best matching video from a list of videos. """
|
||||
if const.args.manual:
|
||||
@@ -161,13 +167,7 @@ class GenerateYouTubeURL:
|
||||
log.debug('No tries left. I quit.')
|
||||
return
|
||||
|
||||
if self.meta_tags is None:
|
||||
song = self.raw_song
|
||||
search_url = generate_search_url(song)
|
||||
else:
|
||||
song = internals.generate_songname(const.args.file_format,
|
||||
self.meta_tags)
|
||||
search_url = generate_search_url(song)
|
||||
search_url = generate_search_url(self.search_query)
|
||||
log.debug('Opening URL: {0}'.format(search_url))
|
||||
|
||||
item = urllib.request.urlopen(search_url).read()
|
||||
@@ -212,9 +212,7 @@ class GenerateYouTubeURL:
|
||||
song = self.raw_song
|
||||
query['q'] = song
|
||||
else:
|
||||
song = '{0} - {1}'.format(self.meta_tags['artists'][0]['name'],
|
||||
self.meta_tags['name'])
|
||||
query['q'] = song
|
||||
query['q'] = self.search_query
|
||||
log.debug('query: {0}'.format(query))
|
||||
|
||||
data = pafy.call_gdata('search', query)
|
||||
|
||||
Reference in New Issue
Block a user