def img_bing(album): """ Bing image search """ setup() log.log_indented('* Trying to extract album art from Bing') album += " Album Art" api_key = "Key" endpoint = "https://api.cognitive.microsoft.com/bing/v5.0/images/search" links_dict = {} headers = {'Ocp-Apim-Subscription-Key': str(BING_KEY)} param = {'q': album, 'count': '1'} response = requests.get(endpoint, headers=headers, params=param) response = response.json() key = 0 try: for i in response['value']: links_dict[str(key)] = str((i['contentUrl'])) key = key + 1 return links_dict["0"] except KeyError: return None
def add_albumart(query, song_title): """ Adds the album art to a song by editing ID3 tags using Mutagen """ try: log.log_indented('* Trying to extract album art from Google.com') albumart = albumsearch.img_search_google(query) except: log.log_error('* Could not extract from Google, trying Bing') albumart = albumsearch.img_search_bing(query) try: img = urlopen(albumart) # Gets album art from url except Exception: log.log_error("* Could not add album art", indented=True) return None audio = EasyMP3(song_title, ID3=ID3) try: audio.add_tags() except _util.error: pass audio.tags.add( APIC( encoding=3, # UTF-8 mime='image/png', type=3, # 3 is for album art desc='Cover', data=img.read())) audio.save() log.log("> Added album art")
def get_details_spotify(song_name): ''' Tries finding metadata through Spotify ''' song_name = improvename.songname(song_name) spotify = spotipy.Spotify() results = spotify.search(song_name, limit=1) # Find top result log.log_indented('* Finding metadata from Spotify.') try: album = (results['tracks']['items'][0]['album'] ['name']) # Parse json dictionary artist = (results['tracks']['items'][0]['album']['artists'][0]['name']) song_title = (results['tracks']['items'][0]['name']) try: log_indented("* Finding lyrics from Genius.com") lyrics = get_lyrics_genius(song_title) except: log_error("* Could not find lyrics from Genius.com, trying something else") lyrics = get_lyrics_letssingit(song_title) match_bool, score = matching_details(song_name, song_title, artist) if match_bool: return artist, album, song_title, lyrics, match_bool, score else: return None except IndexError: log.log_error( '* Could not find metadata from spotify, trying something else.', indented=True) return None
def fix_music(file_name): ''' Searches for '.mp3' files in directory (optionally recursive) and checks whether they already contain album art and album name tags or not. ''' setup() if not Py3: file_name = file_name.encode('utf-8') tags = File(file_name) log.log(file_name) log.log('> Adding metadata') try: artist, album, song_name, lyrics, match_bool, score = get_details_spotify( file_name) # Try finding details through spotify except Exception: artist, album, song_name, lyrics, match_bool, score = get_details_letssingit( file_name) # Use bad scraping method as last resort try: log.log_indented('* Trying to extract album art from Google.com') albumart = albumsearch.img_search_google(artist + ' ' + album) except Exception: log.log_indented('* Trying to extract album art from Bing.com') albumart = albumsearch.img_search_bing(artist + ' ' + album) if match_bool: add_albumart(albumart, file_name) add_details(file_name, song_name, artist, album, lyrics) try: rename(file_name, artist + ' - ' + song_name + '.mp3') except Exception: log.log_error("Couldn't rename file") pass else: log.log_error("* Couldn't find appropriate details of your song", indented=True) log.log("Match score: %s/10.0" % round(score * 10, 1)) log.log(LOG_LINE_SEPERATOR) log.log_success()
def fix_music(file_name): ''' Searches for '.mp3' files in directory (optionally recursive) and checks whether they already contain album art and album name tags or not. ''' setup() if not Py3: file_name = file_name.encode('utf-8') tags = File(file_name) log.log(file_name) log.log('> Adding metadata') try: artist, album, song_name, lyrics, match_bool, score = get_details_spotify( file_name) # Try finding details through spotify except Exception: artist, album, song_name, lyrics, match_bool, score = get_details_letssingit( file_name) # Use bad scraping method as last resort try: log.log_indented('* Trying to extract album art from Google.com') albumart = albumsearch.img_search_google(artist+' '+album) except Exception: log.log_indented('* Trying to extract album art from Bing.com') albumart = albumsearch.img_search_bing(artist+' '+album) if match_bool: add_albumart(albumart, file_name) add_details(file_name, song_name, artist, album, lyrics) try: rename(file_name, artist+' - '+song_name+'.mp3') except Exception: log.log_error("Couldn't rename file") pass else: log.log_error( "* Couldn't find appropriate details of your song", indented=True) log.log("Match score: %s/10.0" % round(score * 10, 1)) log.log(LOG_LINE_SEPERATOR) log.log_success()
def img_google(album): """ Google image search """ log.log_indented('* Trying to extract album art from Google') album += " Album Art" url = ("https://www.google.com/search?q=" + quote(album.encode('utf-8')) + "&source=lnms&tbm=isch") header = {'User-Agent': '''Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/43.0.2357.134 Safari/537.36''' } soup = BeautifulSoup(urlopen(Request(url, headers=header)), "html.parser") albumart_div = soup.find("div", {"class": "rg_meta"}) albumart = json.loads(albumart_div.text)["ou"] return albumart
def get_details_spotify(song_name): """ Use Spotipy to send an API call to Spotify to fetch the details of a track """ #Remove useless words such as - 'lyrics', 'HD', 'audio' from file name song_name = improvename.songname(song_name) spotify = spotipy.Spotify() #Choose top result results = spotify.search(song_name, limit=1) log.log_indented('* Finding metadata from Spotify.') try: album = (results['tracks']['items'][0]['album']['name']) artist = (results['tracks']['items'][0]['album']['artists'][0]['name']) song_title = (results['tracks']['items'][0]['name']) except IndexError: log.log_error( '* Could not find metadata from spotify, trying something else.', indented=True) return None try: log.log_indented("* Finding lyrics from Genius.com") lyrics = get_lyrics_genius(artist + ' ' + song_title) except: lyrics = '' log.log_error("* Could not find lyrics from Genius.com", indented=True) match_bool, score = matching_details(song_name, song_title, artist) if match_bool: log.log("Match score: %s/10.0" % round(score * 10, 1)) return artist, album, song_title, lyrics else: return None
def get_details_spotify(song_name): ''' Tries finding metadata through Spotify ''' song_name = improvename.songname(song_name) spotify = spotipy.Spotify() results = spotify.search(song_name, limit=1) # Find top result log.log_indented('* Finding metadata from Spotify.') try: album = (results['tracks']['items'][0]['album']['name'] ) # Parse json dictionary artist = (results['tracks']['items'][0]['album']['artists'][0]['name']) song_title = (results['tracks']['items'][0]['name']) try: log_indented("* Finding lyrics from Genius.com") lyrics = get_lyrics_genius(song_title) except: log_error( "* Could not find lyrics from Genius.com, trying something else" ) lyrics = get_lyrics_letssingit(song_title) match_bool, score = matching_details(song_name, song_title, artist) if match_bool: return artist, album, song_title, lyrics, match_bool, score else: return None except IndexError: log.log_error( '* Could not find metadata from spotify, trying something else.', indented=True) return None
def add_details(file_path, title, artist, album, lyrics=""): """ Adds the details of a song using Mutagen """ tags = EasyMP3(file_path) tags["title"] = title tags["artist"] = artist tags["album"] = album tags.save() tags = ID3(file_path) uslt_output = USLT(encoding=3, lang=u'eng', desc=u'desc', text=lyrics) tags["USLT::'eng'"] = uslt_output tags.save(file_path) log.log("> Adding properties") log.log_indented("[*] Title: %s" % title) log.log_indented("[*] Artist: %s" % artist) log.log_indented("[*] Album: %s " % album)
def add_details(file_name, title, artist, album, lyrics=""): ''' Adds the details to song ''' tags = EasyMP3(file_name) tags["title"] = title tags["artist"] = artist tags["album"] = album tags.save() tags = ID3(file_name) uslt_output = USLT(encoding=3, lang=u'eng', desc=u'desc', text=lyrics) tags["USLT::'eng'"] = uslt_output tags.save(file_name) log.log("> Adding properties") log.log_indented("[*] Title: %s" % title) log.log_indented("[*] Artist: %s" % artist) log.log_indented("[*] Album: %s " % album)
def add_details(file_name, title, artist, album, lyrics=''): """ Adds the details to song """ tags = EasyMP3(file_name) tags['title'] = title tags['artist'] = artist tags['album'] = album tags.save() tags = ID3(file_name) uslt_output = USLT(encoding=3, lang=u'eng', desc=u'desc', text=lyrics) tags['USLT::\'eng\''] = uslt_output tags.save(file_name) log.log('> Adding properties') log.log_indented('[*] Title: %s' % title) log.log_indented('[*] Artist: %s' % artist) log.log_indented('[*] Album: %s ' % album)
def img_spotify(query): log.log_indented('* Trying to extract album art from Spotify') spotify = spotipy.Spotify() album = spotify.search(q='album:' + query, limit=1) return album['tracks']['items'][0]['album']['images'][0]['url']