Exemple #1
0
    def _download_list(self):
        downloaded_songs = []

        for number, raw_song in enumerate(self.tracks, 1):
            print("")
            try:
                track_dl = Downloader(raw_song, number=number)
                track_dl.download_single()
            except (urllib.request.URLError, TypeError, IOError) as e:
                # detect network problems
                self._cleanup(raw_song, e)
                # TODO: remove this sleep once #397 is fixed
                # wait 0.5 sec to avoid infinite looping
                time.sleep(0.5)
                continue
            except Exception as e:
                if self.skip_on_error:
                    print('Unknown Exception: %s' % str(e))
                    print('Skipping song in list')
                else:
                    raise

            downloaded_songs.append(raw_song)
            # Add track to file of successful downloads
            if self.write_successful_file is not None:
                self._write_successful(raw_song)

            log.debug("Removing downloaded song from tracks file")
            internals.trim_song(self.tracks_file)

        return downloaded_songs
    def _download_list(self):
        downloaded_songs = []

        for number, raw_song in enumerate(self.tracks, 1):
            print("")
            try:
                track_dl = Downloader(raw_song, number=number)
                track_dl.download_single()
            except spotipy.client.SpotifyException:
                # token expires after 1 hour
                self._regenerate_token()
                track_dl.download_single()
            # detect network problems
            except (urllib.request.URLError, TypeError, IOError) as e:
                self._cleanup(raw_song, e)
                # TODO: remove this sleep once #397 is fixed
                # 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
            if self.write_successful_file is not None:
                self._write_successful(raw_song)

            log.debug("Removing downloaded song from tracks file")
            internals.trim_song(self.tracks_file)

        return downloaded_songs
Exemple #3
0
 def _cleanup(self, raw_song, exception):
     self.tracks.append(raw_song)
     # remove the downloaded song from file
     internals.trim_song(self.tracks_file)
     # and append it at the end of file
     with open(self.tracks_file, "a") as f:
         f.write("\n" + raw_song)
     log.exception(exception)
     log.warning("Failed to download song. Will retry after other songs\n")
Exemple #4
0
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
Exemple #5
0
def download_list(text_file):
    """ Download all songs from the list. """
    with open(text_file, 'r') as listed:
        # read tracks into a list and remove any duplicates
        lines = listed.read().splitlines()
        lines = list(set(lines))
    # ignore blank lines in text_file (if any)
    try:
        lines.remove('')
    except ValueError:
        pass

    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
Exemple #6
0
def test_trim(tmpdir):
    text_file = os.path.join(str(tmpdir), "test_trim.txt")
    with open(text_file, "w") as track_file:
        track_file.write("ncs - spectre\nncs - heroes\nncs - hope")

    with open(text_file, "r") as track_file:
        tracks = track_file.readlines()

    expect_number = len(tracks) - 1
    expect_track = tracks[0]
    track = internals.trim_song(text_file)

    with open(text_file, "r") as track_file:
        number = len(track_file.readlines())

    assert expect_number == number and expect_track == track