def download_list(tracks_file, skip_file=None, write_successful_file=None): """ Download all songs from the list. """ log.info("Checking and removing any duplicate tracks") tracks = internals.get_unique_tracks(tracks_file) # override file with unique tracks with open(tracks_file, "w") as f: f.write("\n".join(tracks)) # Remove tracks to skip from tracks list if skip_file is not None: skip_tracks = internals.get_unique_tracks(skip_file) len_before = len(tracks) tracks = [track for track in tracks if track not in skip_tracks] log.info("Skipping {} tracks".format(len_before - len(tracks))) log.info(u"Preparing to download {} songs".format(len(tracks))) downloaded_songs = [] for number, raw_song in enumerate(tracks, 1): print("") try: download_single(raw_song, number=number) # token expires after 1 hour except spotipy.client.SpotifyException: # refresh token when it expires log.debug("Token expired, generating new one and authorizing") spotify_tools.refresh_token() download_single(raw_song, number=number) # detect network problems except (urllib.request.URLError, TypeError, IOError) as e: tracks.append(raw_song) # remove the downloaded song from file internals.trim_song(tracks_file) # and append it at the end of file with open(tracks_file, "a") as f: f.write("\n" + raw_song) log.exception(e) log.warning( "Failed to download song. Will retry after other songs\n") # wait 0.5 sec to avoid infinite looping time.sleep(0.5) continue downloaded_songs.append(raw_song) # Add track to file of successful downloads log.debug("Adding downloaded song to write successful file") if write_successful_file is not None: with open(write_successful_file, "a") as f: f.write("\n" + raw_song) log.debug("Removing downloaded song from tracks file") internals.trim_song(tracks_file) return downloaded_songs
def test_get_unique_tracks(tmpdir, duplicates, expected): file_path = os.path.join(str(tmpdir), "test_duplicates.txt") with open(file_path, "w") as f: f.write("\n".join(duplicates)) unique_tracks = internals.get_unique_tracks(file_path) assert tuple(unique_tracks) == expected
def generate_m3u(track_file): tracks = internals.get_unique_tracks(track_file) target_file = "{}.m3u".format(track_file.split(".")[0]) total_tracks = len(tracks) log.info("Generating {0} from {1} YouTube URLs".format( target_file, total_tracks)) with open(target_file, "w") as output_file: output_file.write("#EXTM3U\n\n") videos = [] for n, track in enumerate(tracks, 1): content, _ = match_video_and_metadata(track) if content is None: log.warning("Skipping {}".format(track)) else: log.info("Matched track {0}/{1} ({2})".format( n, total_tracks, content.watchv_url)) log.debug(track) m3u_key = "#EXTINF:{duration},{title}\n{youtube_url}\n".format( duration=internals.get_sec(content.duration), title=content.title, youtube_url=content.watchv_url, ) log.debug(m3u_key) with open(target_file, "a") as output_file: output_file.write(m3u_key) videos.append(content.watchv_url) return videos
def _filter_tracks_against_skip_file(self): skip_tracks = internals.get_unique_tracks(self.skip_file) len_before = len(self.tracks) tracks = [track for track in self.tracks if track not in skip_tracks] log.info("Skipping {} tracks".format(len_before - len(tracks))) sys.stdout.flush() return tracks
def test_get_unique_tracks(tmpdir, duplicates, expected): file_path = os.path.join(str(tmpdir), 'test_duplicates.txt') with open(file_path, 'w') as tin: tin.write('\n'.join(duplicates)) unique_tracks = internals.get_unique_tracks(file_path) assert tuple(unique_tracks) == expected
def __init__(self, tracks_file, skip_file=None, write_successful_file=None): self.tracks_file = tracks_file self.skip_file = skip_file self.write_successful_file = write_successful_file self.tracks = internals.get_unique_tracks(self.tracks_file)
def download_list(text_file): """ Download all songs from the list. """ log.info('Checking and removing any duplicate tracks') lines = internals.get_unique_tracks(text_file) # override file with unique tracks with open(text_file, 'w') as listed: listed.write('\n'.join(lines)) log.info(u'Preparing to download {} songs'.format(len(lines))) downloaded_songs = [] for number, raw_song in enumerate(lines, 1): print('') try: download_single(raw_song, number=number) # token expires after 1 hour except spotipy.client.SpotifyException: # refresh token when it expires log.debug('Token expired, generating new one and authorizing') new_token = spotify_tools.generate_token() spotify_tools.spotify = spotipy.Spotify(auth=new_token) download_single(raw_song, number=number) # detect network problems except (urllib.request.URLError, TypeError, IOError): lines.append(raw_song) # remove the downloaded song from file internals.trim_song(text_file) # and append it at the end of file with open(text_file, 'a') as myfile: myfile.write(raw_song + '\n') log.warning( 'Failed to download song. Will retry after other songs\n') # wait 0.5 sec to avoid infinite looping time.sleep(0.5) continue downloaded_songs.append(raw_song) log.debug('Removing downloaded song from text file') internals.trim_song(text_file) return downloaded_songs