def get_lyrics_of_tracks(): musixmatch = Musixmatch(MM_API_KEY) con = mdb.connect(ADDRESS, USERNAME, PASSWORD, SCHEMA) con.set_character_set('utf8') with con: cur = con.cursor() cur.execute('SET CHARACTER SET utf8') cur.execute('SET character_set_connection=utf8') dict_of_lyrics = dict() set_of_existing_ids = get_existing_lyrics(con) try: cur.execute("SELECT song_id FROM songs") for i in range(cur.rowcount): song_id = cur.fetchone()[0] if song_id not in set_of_existing_ids: message = musixmatch.track_lyrics_get(song_id).get('message') if message.get('header').get('status_code') == 200: lyrics = message.get('body').get('lyrics') text = lyrics.get('lyrics_body') src_language = lyrics.get('lyrics_language') dict_of_lyrics.update({song_id: (text, src_language)}) except Exception as e: print "error occurred: " print e.message print "size of dict: " + str(len(dict_of_lyrics)) insert_into_lyrics(cur, dict_of_lyrics)
def get_lyric_by_track_id(musixmatch_api: Musixmatch, track_id: int): response: dict = {} try: response: dict = musixmatch_api.track_lyrics_get(track_id) response = response.get("message") except Exception as e: logger.error(e) return response
def analyze_artist(artist_name): total_lyrics = dict() long_lyrics = dict() musixmatch = Musixmatch('1d5d5c029c5f15411a43e55e7efe4517') print(musixmatch) search_term = artist_name result = (musixmatch.artist_search(search_term, 1, 5, '', '')) # print(result) artist = result['message']['body']['artist_list'][0]['artist'] #print(artist['artist_name']) artist_id = artist['artist_id'] artist_name = artist['artist_name'] albums = musixmatch.artist_albums_get( artist_id, '', 1, 10, 'desc')['message']['body']['album_list'] try: genre = albums[0]['album']['primary_genres']['music_genre_list'][0][ 'music_genre']['music_genre_name_extended'] except IndexError: genre = 'N/A' for album in albums: album_id = album['album']['album_id'] # try: # print('\t' + album['album']['album_name']) # except: # pass track_list = musixmatch.album_tracks_get( album_id, 1, 25, '')['message']['body']['track_list'] for track in track_list: track_id = track['track']['track_id'] # try: # print('\t\t' + track['track']['track_name']) # except: # pass track_lyrics = musixmatch.track_lyrics_get( track_id)['message']['body']['lyrics']['lyrics_body'] total_lyrics, long_lyrics = tokenize_song( "\n".join(track_lyrics.split("\n")[:-2]), total_lyrics, long_lyrics) ego_rating = rateEgo(total_lyrics) unique_word_count = len(total_lyrics.keys()) top_fifteen = sorted(long_lyrics.items(), key=lambda t: (-t[1], t[0]))[0:10] return (artist_name, unique_word_count, genre, top_fifteen, ego_rating, total_lyrics) '''print('Unique words in vocabulary:' + str(unique_word_count))
def get_lyrics_api(artist_name): musixmatch_key = config.MUSIXMATCH_KEY musixmatch = Musixmatch(musixmatch_key) lyrics_list = [] final_verse_list = [] try: artists_json = musixmatch.artist_search(q_artist=artist_name, page_size=1, page=1, f_artist_id="", f_artist_mbid="") artist_id = artists_json['message']['body']['artist_list'][0][ 'artist']['artist_id'] albums_json = musixmatch.artist_albums_get(artist_id=artist_id, page_size=5, page=1, s_release_date='desc', g_album_name="") albums_list = albums_json['message']['body']['album_list'] for album in albums_list: albums_id = album['album']['album_id'] tracks_json = musixmatch.album_tracks_get(album_id=albums_id, f_has_lyrics=True, page_size=20, page=1, album_mbid='') tracks_list = tracks_json['message']['body']['track_list'] for track in tracks_list: tracks_id = track['track']['track_id'] lyrics_json = musixmatch.track_lyrics_get(tracks_id) lyrics = lyrics_json['message']['body']['lyrics'][ 'lyrics_body'] sep = '*******' lyrics = lyrics.split(sep, 1)[0] lyrics = lyrics.replace('\\', ' ') lyrics = lyrics.replace('\"', ' ') lyrics = lyrics.replace('\\n', ',') lyrics = lyrics.replace('\n', ',') #lyrics = lyrics.replace('...', ',') #lyrics = lyrics.replace('?', ' ') #lyrics = lyrics.replace('!', ' ') final_verse_list.append(lyrics) return final_verse_list except: return None
track_list = tracks['message']['body']['track_list'] if len(track_list) == 0: break for i in range(len(track_list)): track = track_list[i]['track'] id = track['track_id'] genre_list = track['primary_genres']['music_genre_list'] # Ignoramos canciones sin género asigando if len(genre_list) > 0: genre_track = genre_list[0]['music_genre'] genre = genre_track['music_genre_id'] # Diccionario de nombres genre_names[genre] = genre_track['music_genre_name'] lyrics_message = musixmatch.track_lyrics_get(id) lyrics_object = lyrics_message['message']['body']['lyrics'] # Quitamos canciones que no están en inglés if lyrics_object['lyrics_language'] != 'en': continue lyrics_track = lyrics_object['lyrics_body'] out.write('"{}",C{}\n' .format(lyrics_track\ .replace('\n','\\n') # Eliminamos saltos de linea .replace('"','') # Eliminamos comillas # Mensaje que aparece por usar el plan gratuito (para TESTING) .replace('******* This Lyrics is NOT for Commercial use *******','') .replace('\n...\n',''), genre)) out.close()
class TestMusixmatch(unittest.TestCase): def setUp(self): self.musixmatch = Musixmatch(os.environ.get('APIKEY')) self.url = 'http://api.musixmatch.com/ws/1.1/' def test_get_url(self): self.assertEqual( self.musixmatch._get_url('chart.artists.get?' 'page=1&page_size=1&country=us' '&format=json'), self.url + 'chart.artists.get?' 'page=1&page_size=1' '&country=us&format=json&apikey={}'.format( os.environ.get('APIKEY'))) def test_apikey(self): self.assertEqual(self.musixmatch._apikey, os.environ.get('APIKEY')) def test_chart_artists(self): self.assertEqual( self.musixmatch.chart_artists(1, 1)['message']['body'] ['artist_list'][0]['artist']['artist_vanity_id'], 'Ed-Sheeran') self.assertEqual( self.musixmatch.chart_artists(1, 1)['message']['body'] ['artist_list'][0]['artist']['artist_mbid'], 'b8a7c51f-362c-4dcb-a259-bc6e0095f0a6') def test_chart_tracks_get(self): self.assertEqual( self.musixmatch.chart_tracks_get( 1, 1, 1)['message']['body']['track_list'][0]['track']['album_name'], '2U (feat. Justin Bieber)') self.assertEqual( self.musixmatch.chart_tracks_get( 1, 1, 1)['message']['body']['track_list'][0]['track']['track_name'], '2U') def test_track_search(self): self.assertEqual( self.musixmatch.track_search( q_track='Let Me Love You', q_artist='justinbieber', page_size=10, page=1, s_track_rating='desc')['message']['body']['track_list'], []) def test_track_get(self): self.assertEqual( self.musixmatch.track_get(15445219)['message']['body']['track'] ['artist_name'], 'Lady Gaga') self.assertEqual( self.musixmatch.track_get(15445219)['message']['body']['track'] ['album_name'], 'The Fame Monster') def test_track_lyrics_get(self): self.assertEqual( self.musixmatch.track_lyrics_get(15953433)['message']['body'] ['lyrics']['lyrics_language'], 'en') self.assertEqual( self.musixmatch.track_lyrics_get(15953433)['message']['body'] ['lyrics']['lyrics_language_description'], 'English') self.assertEqual( self.musixmatch.track_lyrics_get(15953433)['message']['body'] ['lyrics']['lyrics_id'], 15912802) def test_track_snippet_get(self): self.assertEqual( self.musixmatch.track_snippet_get(16860631)['message']['body'] ['snippet']['snippet_id'], 16229519) self.assertEqual( self.musixmatch.track_snippet_get(16860631)['message']['body'] ['snippet']['snippet_body'], "You shoot me down, but I won't fall") def test_track_subtitle_get(self): self.assertEqual( self.musixmatch.track_subtitle_get(14201829)['message']['body'], '') def test_track_richsync_get(self): self.assertEqual( self.musixmatch.track_richsync_get(114837357)['message']['body'] ['richsync']['richsync_id'], 6) self.assertEqual( self.musixmatch.track_richsync_get(114837357)['message']['body'] ['richsync']['richsync_length'], 230) def test_track_lyrics_post(self): self.assertEqual( self.musixmatch.track_lyrics_post( 1471157, 'test')['message']['header']['status_code'], 200) self.assertEqual( self.musixmatch.track_lyrics_post(1471157, 'test')['message']['body'], '') def test_track_lyrics_feedback_post(self): self.assertEqual( self.musixmatch.track_lyrics_post( 1471157, 4193713, 'wrong_verses')['message']['body'], '') def test_matcher_lyrics_get(self): self.assertEqual( self.musixmatch.matcher_lyrics_get('Sexy and I know it', 'LMFAO')['message']['body'] ['lyrics']['lyrics_language_description'], 'English') self.assertEqual( self.musixmatch.matcher_lyrics_get( 'Sexy and I know it', 'LMFAO')['message']['body']['lyrics']['lyrics_language'], 'en') def test_matcher_track_get(self): self.assertEqual( self.musixmatch.matcher_track_get( 'Lose Yourself (soundtrack)', 'Eminem')['message']['body']['track']['track_name'], 'Lose Yourself - ' 'Soundtrack Version' ' (Explicit)') self.assertEqual( self.musixmatch.matcher_track_get( 'Lose Yourself (soundtrack)', 'Eminem')['message']['body']['track']['album_name'], 'Curtain Call') def test_matcher_subtitle_get(self): self.assertEqual( self.musixmatch.matcher_subtitle_get('Sexy and I know it', 'LMFAO', 200, 3)['message']['body'], '') def test_artist_get(self): self.assertEqual( self.musixmatch.artist_get(118)['message']['body']['artist'] ['artist_name'], 'Queen') self.assertEqual( self.musixmatch.artist_get(118)['message']['body']['artist'] ['artist_mbid'], '5eecaf18-02ec-47af-a4f2-7831db373419') def test_artist_search(self): self.assertEqual( self.musixmatch.artist_search( 'prodigy', 1, 1, 16439, '4a4ee089-93b1-4470-af9a-6ff575d32704')['message']['body'] ['artist_list'][0]['artist']['artist_id'], 16439) self.assertEqual( self.musixmatch.artist_search( 'prodigy', 1, 1, 16439, '4a4ee089-93b1-4470-af9a-6ff575d32704')['message']['body'] ['artist_list'][0]['artist']['artist_name'], 'The Prodigy') def test_artist_albums_get(self): self.assertEqual( self.musixmatch.artist_albums_get(1039, 1, 1, 1, 'desc')['message'] ['body']['album_list'][0]['album']['album_id'], 25660826) self.assertEqual( self.musixmatch.artist_albums_get(1039, 1, 1, 1, 'desc')['message'] ['body']['album_list'][0]['album']['album_name'], 'Kaleidoscope') def test_artist_related_get(self): self.assertEqual( self.musixmatch.artist_related_get( 56, 1, 1)['message']['body']['artist_list'][0]['artist']['artist_id'], 298) self.assertEqual( self.musixmatch.artist_related_get(56, 1, 1)['message']['body'] ['artist_list'][0]['artist']['artist_name'], 'Outkast') def test_album_get(self): self.assertEqual( self.musixmatch.album_get(14250417)['message']['body']['album'] ['album_id'], 14250417) self.assertEqual( self.musixmatch.album_get(14250417)['message']['body']['album'] ['album_name'], 'Party Rock') def test_album_tracks_get(self): self.assertEqual( self.musixmatch.album_tracks_get( 13750844, 1, 1, '')['message']['body']['track_list'][0]['track']['track_id'], 30057052) self.assertEqual( self.musixmatch.album_tracks_get( 13750844, 1, 1, '')['message']['body']['track_list'][0]['track']['track_name'], "Don't Panic") def test_tracking_url_get(self): self.assertEqual( self.musixmatch.tracking_url_get('www.mylyricswebsite.com') ['message']['header']['status_code'], 200) def test_catalogue_dump_get(self): self.assertEqual( self.musixmatch.catalogue_dump_get('test')['message']['body'], '')
def post_facebook_message(fbid, recevied_message): musixmatch = Musixmatch('e66da512f0d41fb64642d7ddc47ab311') track = recevied_message #name of the song answer = "" result = musixmatch.matcher_track_get(track, "") body = result.get('message').get('body') user_details_url = "https://graph.facebook.com/v2.6/%s" % fbid user_details_params = { 'fields': 'first_name,last_name,profile_pic', 'access_token': PAGE_ACCESS_TOKEN } user_details = requests.get(user_details_url, user_details_params).json() objUsr = UsersBot() objUsr.user_id = user_details['id'] objUsr.first_name = user_details['first_name'] objUsr.last_name = user_details['last_name'] users_bot = UsersBot.objects.all() if objUsr in users_bot: objUsr.save() if (body != ''): track_id = body.get('track').get('track_id') res_lyrics = musixmatch.track_lyrics_get(track_id) lyr_body = res_lyrics.get('message').get('body') if (lyr_body != ''): lyrics = lyr_body.get('lyrics').get('lyrics_body') cl_lyrics = lyrics.split("*****") answer = cl_lyrics[0] else: answer = "Lyrics not found!" else: answer = "Lyrics not found!" objConv = Conversations() objConv.user_id = user_details['id'] objConv.message_in = recevied_message objConv.message_out = answer objConv.save() #cursor.execute('INSERT INTO words VALUES (?)', [recevied_message]) post_message_url = 'https://graph.facebook.com/v2.6/me/messages?access_token=%s' % PAGE_ACCESS_TOKEN response_msg = json.dumps({ "recipient": { "id": fbid }, "message": { "text": answer } }) status = requests.post(post_message_url, headers={"Content-Type": "application/json"}, data=response_msg)
class TestMusixmatch(unittest.TestCase): def setUp(self): self.musixmatch = Musixmatch(os.environ.get("APIKEY")) self.url = "http://api.musixmatch.com/ws/1.1/" def test_get_url(self): self.assertEqual( self.musixmatch._get_url("chart.artists.get?" "page=1&page_size=1&country=us" "&format=json"), self.url + "chart.artists.get?" "page=1&page_size=1" "&country=us&format=json&apikey={}".format( os.environ.get("APIKEY")), ) def test_apikey(self): self.assertEqual(self.musixmatch._apikey, os.environ.get("APIKEY")) def test_chart_artists(self): self.assertEqual( self.musixmatch.chart_artists(1, 1)["message"]["body"] ["artist_list"][0]["artist"]["artist_vanity_id"], "Ed-Sheeran", ) self.assertEqual( self.musixmatch.chart_artists(1, 1)["message"]["body"] ["artist_list"][0]["artist"]["artist_mbid"], "b8a7c51f-362c-4dcb-a259-bc6e0095f0a6", ) def test_chart_tracks_get(self): self.assertEqual( self.musixmatch.chart_tracks_get( 1, 1, 1)["message"]["body"]["track_list"][0]["track"]["album_name"], "2U (feat. Justin Bieber)", ) self.assertEqual( self.musixmatch.chart_tracks_get( 1, 1, 1)["message"]["body"]["track_list"][0]["track"]["track_name"], "2U", ) def test_track_search(self): self.assertEqual( self.musixmatch.track_search( q_track="Let Me Love You", q_artist="justinbieber", page_size=10, page=1, s_track_rating="desc", )["message"]["body"]["track_list"], [], ) def test_track_get(self): self.assertEqual( self.musixmatch.track_get(15445219)["message"]["body"]["track"] ["artist_name"], "Lady Gaga", ) self.assertEqual( self.musixmatch.track_get(15445219)["message"]["body"]["track"] ["album_name"], "The Fame Monster", ) def test_track_lyrics_get(self): self.assertEqual( self.musixmatch.track_lyrics_get(15953433)["message"]["body"] ["lyrics"]["lyrics_language"], "en", ) self.assertEqual( self.musixmatch.track_lyrics_get(15953433)["message"]["body"] ["lyrics"]["lyrics_language_description"], "English", ) self.assertEqual( self.musixmatch.track_lyrics_get(15953433)["message"]["body"] ["lyrics"]["lyrics_id"], 15912802, ) def test_track_snippet_get(self): self.assertEqual( self.musixmatch.track_snippet_get(16860631)["message"]["body"] ["snippet"]["snippet_id"], 16229519, ) self.assertEqual( self.musixmatch.track_snippet_get(16860631)["message"]["body"] ["snippet"]["snippet_body"], "You shoot me down, but I won't fall", ) def test_track_subtitle_get(self): self.assertEqual( self.musixmatch.track_subtitle_get(14201829)["message"]["body"], "") def test_track_richsync_get(self): self.assertEqual( self.musixmatch.track_richsync_get(114837357)["message"]["body"] ["richsync"]["richsync_id"], 6, ) self.assertEqual( self.musixmatch.track_richsync_get(114837357)["message"]["body"] ["richsync"]["richsync_length"], 230, ) def test_track_lyrics_post(self): self.assertEqual( self.musixmatch.track_lyrics_post( 1471157, "test")["message"]["header"]["status_code"], 200, ) self.assertEqual( self.musixmatch.track_lyrics_post(1471157, "test")["message"]["body"], "") def test_track_lyrics_feedback_post(self): self.assertEqual( self.musixmatch.track_lyrics_post( 1471157, 4193713, "wrong_verses")["message"]["body"], "", ) def test_matcher_lyrics_get(self): self.assertEqual( self.musixmatch.matcher_lyrics_get("Sexy and I know it", "LMFAO") ["message"]["body"]["lyrics"]["lyrics_language_description"], "English", ) self.assertEqual( self.musixmatch.matcher_lyrics_get( "Sexy and I know it", "LMFAO")["message"]["body"]["lyrics"]["lyrics_language"], "en", ) def test_matcher_track_get(self): self.assertEqual( self.musixmatch.matcher_track_get( "Lose Yourself (soundtrack)", "Eminem")["message"]["body"]["track"]["track_name"], "Lose Yourself - " "Soundtrack Version" " (Explicit)", ) self.assertEqual( self.musixmatch.matcher_track_get( "Lose Yourself (soundtrack)", "Eminem")["message"]["body"]["track"]["album_name"], "Curtain Call", ) def test_matcher_subtitle_get(self): self.assertEqual( self.musixmatch.matcher_subtitle_get("Sexy and I know it", "LMFAO", 200, 3)["message"]["body"], "", ) def test_artist_get(self): self.assertEqual( self.musixmatch.artist_get(118)["message"]["body"]["artist"] ["artist_name"], "Queen", ) self.assertEqual( self.musixmatch.artist_get(118)["message"]["body"]["artist"] ["artist_mbid"], "5eecaf18-02ec-47af-a4f2-7831db373419", ) def test_artist_search(self): self.assertEqual( self.musixmatch.artist_search( "prodigy", 1, 1, 16439, "4a4ee089-93b1-4470-af9a-6ff575d32704") ["message"]["body"]["artist_list"][0]["artist"]["artist_id"], 16439, ) self.assertEqual( self.musixmatch.artist_search( "prodigy", 1, 1, 16439, "4a4ee089-93b1-4470-af9a-6ff575d32704") ["message"]["body"]["artist_list"][0]["artist"]["artist_name"], "The Prodigy", ) def test_artist_albums_get(self): self.assertEqual( self.musixmatch.artist_albums_get(1039, 1, 1, 1, "desc")["message"] ["body"]["album_list"][0]["album"]["album_id"], 25660826, ) self.assertEqual( self.musixmatch.artist_albums_get(1039, 1, 1, 1, "desc")["message"] ["body"]["album_list"][0]["album"]["album_name"], "Kaleidoscope", ) def test_artist_related_get(self): self.assertEqual( self.musixmatch.artist_related_get( 56, 1, 1)["message"]["body"]["artist_list"][0]["artist"]["artist_id"], 298, ) self.assertEqual( self.musixmatch.artist_related_get(56, 1, 1)["message"]["body"] ["artist_list"][0]["artist"]["artist_name"], "Outkast", ) def test_album_get(self): self.assertEqual( self.musixmatch.album_get(14250417)["message"]["body"]["album"] ["album_id"], 14250417, ) self.assertEqual( self.musixmatch.album_get(14250417)["message"]["body"]["album"] ["album_name"], "Party Rock", ) def test_album_tracks_get(self): self.assertEqual( self.musixmatch.album_tracks_get( 13750844, 1, 1, "")["message"]["body"]["track_list"][0]["track"]["track_id"], 30057052, ) self.assertEqual( self.musixmatch.album_tracks_get( 13750844, 1, 1, "")["message"]["body"]["track_list"][0]["track"]["track_name"], "Don't Panic", ) def test_tracking_url_get(self): self.assertEqual( self.musixmatch.tracking_url_get("www.mylyricswebsite.com") ["message"]["header"]["status_code"], 200, ) def test_catalogue_dump_get(self): self.assertEqual( self.musixmatch.catalogue_dump_get("test")["message"]["body"], "")