def match_video_and_metadata(track): """ Get and match track data from YouTube and Spotify. """ meta_tags = None def fallback_metadata(meta_tags): fallback_metadata_info = "Track not found on Spotify, falling back on YouTube metadata" skip_fallback_metadata_warning = "Fallback condition not met, shall not embed metadata" if meta_tags is None: if const.args.no_fallback_metadata: log.warning(skip_fallback_metadata_warning) sys.stdout.flush() else: log.info(fallback_metadata_info) sys.stdout.flush() meta_tags = generate_metadata(content) return meta_tags if internals.is_youtube(track): log.debug("Input song is a YouTube URL") sys.stdout.flush() content = go_pafy(track, meta_tags=None) track = slugify(content.title).replace("-", " ") if not const.args.no_metadata: meta_tags = spotify_tools.generate_metadata(track) meta_tags = fallback_metadata(meta_tags) elif internals.is_spotify(track): log.debug("Input song is a Spotify URL") sys.stdout.flush() # Let it generate metadata, YouTube doesn't know Spotify slang meta_tags = spotify_tools.generate_metadata(track) content = go_pafy(track, meta_tags) if const.args.no_metadata: meta_tags = None else: log.debug("Input song is plain text based") sys.stdout.flush() if const.args.no_metadata: content = go_pafy(track, meta_tags=None) else: meta_tags = spotify_tools.generate_metadata(track) content = go_pafy(track, meta_tags=meta_tags) meta_tags = fallback_metadata(meta_tags) return content, meta_tags
def create_file(input_file, link): log.info("Get metadata for {}".format(input_file)) try: file_ext = '.{}'.format(input_file.split('.')[-1]) except Exception as e: file_ext = '' meta_tags = spotify_tools.generate_metadata(link) if not meta_tags: log.error("No metadata found.") else: file_name = "{} - {}{}".format(meta_tags["artists"][0]["name"], meta_tags["name"], file_ext) log.info("Create {}".format(file_name)) output_file = os.path.join(const.args.folder, file_name) command = ( "ffmpeg -y -nostdin -hide_banner -nostats -v panic -i".split() + [input_file] + "-c copy -map_metadata -1 -map 0".split() + [output_file]) try: subprocess.call(command) metadata.embed(output_file, meta_tags) except Exception as e: log.error(str(e))
def get_name_for_list_widget(category_list, url): "This function get the album/song/playlist/artist name from url" try: if category_list == 'playlist': playlist = spotify_tools.fetch_playlist(url) name = playlist['name'] text_file = "Playlist: " + name elif category_list == 'track': track = spotify_tools.generate_metadata(url) name = track['name'] artist_name = track['album']['artists'][0]['name'] text_file = 'Song: ' + name + ' - ' + artist_name elif category_list == 'artist': artist = spotify_tools.fetch_albums_from_artist(url) name = artist[0]['artists'][0]['name'] text_file = u"Complete albums of " + artist[0]['artists'][0]['name'] elif category_list == 'album': album = spotify_tools.fetch_album(url) name = u"{0}".format(slugify(album["name"], ok="-_()[]{}")) text_file = 'Album: ' + name + ' - ' + album['artists'][0]['name'] else: text_file = 'Not Found name' name = '' except Exception as e: print( "{} name not found! Setting standard name.".format(category_list)) text_file = str(category_list) + url[31:-1] name = ' ' return text_file, name
def get_song_data(url): """Retrieve song, artist, album, playlist name from url""" song_name = '' album_name = '' album_url = '' artist_name = '' artist_url = '' duration = None try: track = spotify_tools.generate_metadata(url) song_name = track['name'] artist_name = track['album']['artists'][0]['name'] album_name = track['album']['name'] album_url = track['album']['external_urls']['spotify'] artist_url = track['album']['artists'][0]['external_urls']['spotify'] duration = track['duration'] except Exception as e: print('Error as :', e) return [ song_name, album_name, artist_name, album_url, artist_url, duration ]
def get_song_metadata(raw_song): """ Get song metadata on Spotify :param raw_song: Raw song name string / Spotify link for song :return: Song's metadata on Spotify """ return spotify_tools.generate_metadata(raw_song)
def match_video_and_metadata(track, force_pafy=True): """ Get and match track data from YouTube and Spotify. """ meta_tags = None if internals.is_youtube(track): log.debug("Input song is a YouTube URL") content = go_pafy(track, meta_tags=None) track = slugify(content.title).replace("-", " ") if not const.args.no_metadata: meta_tags = spotify_tools.generate_metadata(track) else: if not const.args.no_metadata: meta_tags = spotify_tools.generate_metadata(track) if force_pafy: content = go_pafy(track, meta_tags) else: content = None return content, meta_tags
def test_metadata(): expect_metadata = None global metadata metadata = spotify_tools.generate_metadata(raw_song) assert metadata == expect_metadata
def metadata_fixture(): metadata = spotify_tools.generate_metadata(TRACK_SEARCH) return metadata
def test_metadata(): expect_number = 23 global meta_tags meta_tags = spotify_tools.generate_metadata(TRACK_URL) assert len(meta_tags) == expect_number
def download_single(raw_song, number=None): """ Logic behind downloading a song. """ if internals.is_youtube(raw_song): log.debug('Input song is a YouTube URL') content = youtube_tools.go_pafy(raw_song, meta_tags=None) raw_song = slugify(content.title).replace('-', ' ') meta_tags = spotify_tools.generate_metadata(raw_song) else: meta_tags = spotify_tools.generate_metadata(raw_song) content = youtube_tools.go_pafy(raw_song, meta_tags) if content is None: log.debug('Found no matching video') return if const.args.download_only_metadata and meta_tags is None: log.info('Found no metadata. Skipping the download') return # "[number]. [artist] - [song]" if downloading from list # otherwise "[artist] - [song]" youtube_title = youtube_tools.get_youtube_title(content, number) log.info('{} ({})'.format(youtube_title, content.watchv_url)) # generate file name of the song to download songname = content.title if meta_tags is not None: refined_songname = internals.format_string(const.args.file_format, meta_tags, slugification=True) log.debug('Refining songname from "{0}" to "{1}"'.format( songname, refined_songname)) if not refined_songname == ' - ': songname = refined_songname else: log.warning('Could not find metadata') songname = internals.sanitize_title(songname) if const.args.dry_run: return if not check_exists(songname, raw_song, meta_tags): # deal with file formats containing slashes to non-existent directories songpath = os.path.join(const.args.folder, os.path.dirname(songname)) os.makedirs(songpath, exist_ok=True) input_song = songname + const.args.input_ext output_song = songname + const.args.output_ext if youtube_tools.download_song(input_song, content): try: convert.song(input_song, output_song, const.args.folder, avconv=const.args.avconv, trim_silence=const.args.trim_silence) except FileNotFoundError: encoder = 'avconv' if const.args.avconv else 'ffmpeg' log.warning( 'Could not find {0}, skipping conversion'.format(encoder)) const.args.output_ext = const.args.input_ext output_song = songname + const.args.output_ext if not const.args.input_ext == const.args.output_ext: os.remove(os.path.join(const.args.folder, input_song)) if not const.args.no_metadata and meta_tags is not None: metadata.embed(os.path.join(const.args.folder, output_song), meta_tags) return True
def metadata_fixture(): meta_tags = spotify_tools.generate_metadata(SPOTIFY_TRACK_URL) return meta_tags
def test_metadata(): expect_metadata = None global metadata metadata = spotify_tools.generate_metadata(TRACK_SEARCH) assert metadata == expect_metadata
def metadata_fixture(self): metadata = spotify_tools.generate_metadata("ncs - spectre") return metadata
def test_correct_token(self): assert spotify_tools.generate_metadata("ncs - spectre")
def test_fake_token_generator(self, monkeypatch): spotify_tools.spotify = None monkeypatch.setattr(spotify_tools, "generate_token", lambda: 123123) with pytest.raises(spotipy.client.SpotifyException): spotify_tools.generate_metadata("ncs - spectre")