def test_download_cover_none(): songs = [{ 'album': 'Queen II (Deluxe Remastered Version)', 'artist': 'Queen', 'cover': None, 'genre': 'classic rock', 'name': "The Fairy Feller's Master-Stroke - Remastered 2011", 'num': 7, 'num_tracks': 16, 'year': '1974' }] yt.download_songs(songs, download_directory=os.path.dirname( os.path.realpath(__file__)), format_string='best', skip_mp3=False) music = MP3( "tests/Queen - The Fairy Feller's Master-Stroke - Remastered 2011.mp3", ID3=EasyID3) tags = ID3( "tests/Queen - The Fairy Feller's Master-Stroke - Remastered 2011.mp3") assert (music['artist'][0] == 'Queen') assert (music['album'][0] == 'Queen II (Deluxe Remastered Version)') assert (music['genre'][0] == 'classic rock') assert (len(tags.getall("APIC")) == 0)
def test_download_one_false_skip(): songs = [{ 'album': 'Hell Freezes Over (Remaster 2018)', 'artist': 'Eagles', 'cover': 'https://i.scdn.co/image/ab67616d0000b27396d28597a5ae44ab66552183', 'genre': 'album rock', 'name': 'Hotel California - Live On MTV, 1994', 'num': 6, 'num_tracks': 15, 'year': '1994' }] yt.download_songs(songs, download_directory=os.path.dirname( os.path.realpath(__file__)), format_string='best', skip_mp3=False) music = MP3("tests/Eagles - Hotel California - Live On MTV, 1994.mp3", ID3=EasyID3) tags = ID3("tests/Eagles - Hotel California - Live On MTV, 1994.mp3") assert (music['artist'][0] == 'Eagles') assert (music['album'][0] == 'Hell Freezes Over (Remaster 2018)') assert (music['genre'][0] == 'album rock') assert (tags.getall("APIC")[0].data == APIC( encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=urllib.request.urlopen(songs[0].get('cover')).read()))
def download_spotify(sp, args): if args.output: item_type, item_id = parse_spotify_url(args.url) directory_name = get_item_name(sp, item_type, item_id) save_path = Path(args.output) if args.createdir: save_path = Path( PurePath.joinpath(Path(args.output), Path(directory_name))) save_path.mkdir(parents=True, exist_ok=True) log.info("Saving songs to: {}".format(directory_name)) songs = fetch_tracks(sp, item_type, args.url) if args.download is True: download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order)
def test_download_one_false_skip(): songs = [{ 'album': 'Hell Freezes Over (Remaster 2018)', 'artist': 'Eagles', 'cover': 'https://i.scdn.co/image/ab67616d0000b27396d28597a5ae44ab66552183', 'genre': 'album rock', 'name': 'Hotel California - Live On MTV, 1994', 'num': 6, 'num_tracks': 15, 'year': '1994' }] yt.download_songs(songs, download_directory='~/Downloads', format_string='best', skip_mp3=False)
def download_youtube(sp, args): if args.output: directory_name, item_type, songs = fetch_tracks_yt(args.url) save_path = Path(args.output) if args.createdir: save_path = Path( PurePath.joinpath(Path(args.output), Path(directory_name))) save_path.mkdir(parents=True, exist_ok=True) log.info("Saving songs to: {}".format(directory_name)) if args.download is True: download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order, is_yt=True)
def update_files(self): """Check if files in the job directory match the contents of the playlist. Delete or download old/new files. """ log.info('Updating files ...') (_, _, filenames) = next(os.walk(self.directory)) filenames = [f for f in filenames if f.endswith('.mp3')] newtracks = list(self.tracks.keys()) for f in filenames: name, extension = os.path.splitext(f) if f.endswith('.json'): continue elif name in newtracks: # track already downloaded newtracks.remove(name) # newtracks.remove(name) elif name not in newtracks: # track downloaded but no longer needed log.info(f'Deleting file {name} since no longer in playlist.') os.remove(os.path.join(self.directory, name + extension)) log.info(f'Checked files for {self.PLname}: {len(newtracks)}' ' new tracks to download.') for uri in newtracks: data = self.tracks[uri] songs = fetch_tracks(self.sp_client, 'track', data['external_urls']['spotify']) download_songs(songs, self.directory, 'bestaudio/best', False) # Filename convention for spotify_dl.youtube (this could break) # --> f"{song.get('artist')} - {song.get('name')}.mp3" # Rename file to {uri}.mp3 (that's how this script operates) os.rename( os.path.join( self.directory, f"{songs[0].get('artist')} - {songs[0].get('name')}.mp3"), os.path.join(self.directory, f"{uri}.mp3")) log.info(f'Downloaded track {data["name"]} ({uri}).') log.info('Finished updating files ...')
def spotify_dl(): """Main entry point of the script.""" parser = argparse.ArgumentParser(prog='spotify_dl') parser.add_argument('-l', '--url', action="store", help="Spotify Playlist link URL", type=str, required=True) parser.add_argument('-o', '--output', type=str, action='store', help='Specify download directory.', required=True) parser.add_argument('-d', '--download', action='store_true', help='Download using youtube-dl', default=True) parser.add_argument('-f', '--format_str', type=str, action='store', help='Specify youtube-dl format string.', default='bestaudio/best') parser.add_argument( '-k', '--keep_playlist_order', default=False, action='store_true', help='Whether to keep original playlist ordering or not.') parser.add_argument('-m', '--skip_mp3', action='store_true', help='Don\'t convert downloaded songs to mp3') parser.add_argument('-s', '--scrape', action="store", help="Use HTML Scraper for YouTube Search", default=True) parser.add_argument('-V', '--verbose', action='store_true', help='Show more information on what' 's happening.') parser.add_argument('-v', '--version', action='store_true', help='Shows current version of the program') args = parser.parse_args() if args.version: print("spotify_dl v{}".format(VERSION)) exit(0) db.connect() db.create_tables([Song]) if os.path.isfile(os.path.expanduser('~/.spotify_dl_settings')): with open(os.path.expanduser('~/.spotify_dl_settings')) as file: config = json.loads(file.read()) for key, value in config.items(): if value and (value.lower() == 'true' or value.lower() == 't'): setattr(args, key, True) else: setattr(args, key, value) if args.verbose: log.setLevel(DEBUG) log.info('Starting spotify_dl') log.debug('Setting debug mode on spotify_dl') if not check_for_tokens(): exit(1) sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials()) log.debug('Arguments: {}'.format(args)) if args.url: valid_item = validate_spotify_url(args.url) if not valid_item: sys.exit(1) if args.output: item_type, item_id = parse_spotify_url(args.url) directory_name = get_item_name(sp, item_type, item_id) save_path = Path( PurePath.joinpath(Path(args.output), Path(directory_name))) save_path.mkdir(parents=True, exist_ok=True) log.info("Saving songs to: {}".format(directory_name)) songs = fetch_tracks(sp, item_type, args.url) if args.download is True: file_name_f = default_filename if args.keep_playlist_order: file_name_f = playlist_num_filename download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order, file_name_f)