def generate_metadata(raw_song): """Fetch a song's metadata from Spotify.""" if misc.is_spotify(raw_song): # fetch track information directly if it is spotify link meta_tags = spotify.track(raw_song) else: # otherwise search on spotify and fetch information from first result try: meta_tags = spotify.search(raw_song, limit=1)['tracks']['items'][0] except: return None artist = spotify.artist(meta_tags['artists'][0]['id']) album = spotify.album(meta_tags['album']['id']) try: meta_tags[u'genre'] = titlecase(artist['genres'][0]) except IndexError: meta_tags[u'genre'] = None try: meta_tags[u'copyright'] = album['copyrights'][0]['text'] except IndexError: meta_tags[u'copyright'] = None meta_tags[u'release_date'] = album['release_date'] meta_tags[u'publisher'] = album['label'] meta_tags[u'total_tracks'] = album['tracks']['total'] return meta_tags
def check_exists(music_file, raw_song, islist): files = os.listdir("Music") for file in files: if file.endswith(".temp"): os.remove("Music/" + file) continue # check if any file with similar name is already present in Music/ dfile = misc.fix_decoding(file) umfile = misc.fix_decoding(misc.generate_filename(music_file)) if dfile.startswith(umfile): # check if the already downloaded song has correct metadata already_tagged = metadata.compare(file, generate_metadata(raw_song)) # if not, remove it and download again without prompt if misc.is_spotify(raw_song) and not already_tagged: os.remove("Music/" + file) return False # do not prompt and skip the current song if already downloaded when using list if islist: return True # if downloading only single song, prompt to re-download else: prompt = misc.user_input('Song with same name has already been downloaded. Re-download? (y/n): ').lower() if prompt == "y": os.remove("Music/" + file) return False else: return True
def check_exists(music_file, raw_song, islist=True): """Check if the input song already exists in the given folder.""" songs = os.listdir(args.folder) for song in songs: if song.endswith('.temp'): os.remove(os.path.join(args.folder, song)) continue # check if any song with similar name is already present in the given folder file_name = misc.sanitize_title(music_file) if song.startswith(file_name): # check if the already downloaded song has correct metadata already_tagged = metadata.compare(os.path.join(args.folder, song), generate_metadata(raw_song)) # if not, remove it and download again without prompt if misc.is_spotify(raw_song) and not already_tagged: os.remove(os.path.join(args.folder, song)) return False # do not prompt and skip the current song # if already downloaded when using list if islist: return True # if downloading only single song, prompt to re-download else: prompt = input( 'Song with same name has already been downloaded. ' 'Re-download? (y/n): ').lower() if prompt == 'y': os.remove(os.path.join(args.folder, song)) return False else: return True return False
def generate_songname(raw_song): if misc.is_spotify(raw_song): tags = generate_metadata(raw_song) raw_song = tags['artists'][0]['name'] + ' - ' + tags['name'] return misc.fix_encoding(raw_song)