Nov-29-2017, 01:33 PM
I want to get the lyrics for each track returned from lastfm api. If i use the method song_lyric() below, that uses the musixmatch api, directy like:
But if I use "song_lyric(name,artist)" in the code below its not working, in the firsts lyrics works but then when it reaches this same song "All Over Again" and it appears:
Traceback (most recent call last):
File "/Users/ozzy/PycharmProjects/semantic/semantic.py", line 240, in <module>
tracks[ID]['Lyric'] = song_lyric(title, tracks[ID]['Artist'])
File "/Users/ozzy/PycharmProjects/semantic/semantic.py", line 29, in song_lyric
body = json_obj["message"]["body"]["lyrics"]["lyrics_body"]
TypeError: list indices must be integers or slices, not str
Do you know how to solve this issue?
Code to get the lyrics of track based on artist name and title:
song_lyric("(Another Song) All Over Again", "Justion Timberlake")It works I get the lyric of the artist and track title informed in the method parameters:
Quote:name:::::(Another Song) All Over Againartsist::::Justin Timberlake
https://tracking.musixmatch.com/t1.0/m_h...mQOTsHKL6/
But if I use "song_lyric(name,artist)" in the code below its not working, in the firsts lyrics works but then when it reaches this same song "All Over Again" and it appears:
Traceback (most recent call last):
File "/Users/ozzy/PycharmProjects/semantic/semantic.py", line 240, in <module>
tracks[ID]['Lyric'] = song_lyric(title, tracks[ID]['Artist'])
File "/Users/ozzy/PycharmProjects/semantic/semantic.py", line 29, in song_lyric
body = json_obj["message"]["body"]["lyrics"]["lyrics_body"]
TypeError: list indices must be integers or slices, not str
Do you know how to solve this issue?
Code to get the lyrics of track based on artist name and title:
tracks = {} #GET TRACKS FOR ALL THE ALBUMS for i,v in albuns.items(): artist = albuns[i]['Artist'].replace(" ","+") title = albuns[i]['Title'].replace(" ", "+") album = requests.get('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key='+api_key+'&artist='+artist+'&album='+title) tree = ET.fromstring(album.content) for child in tree: for artist in child.findall('tracks'): for track in artist.findall('track'): name = track.find('name').text tracks[ID] = {} tracks[ID]['Title'] = name tracks[ID]['Artist'] = albuns[i]['Artist'] tracks[ID]['Album'] = albuns[i]['Title'] tracks[ID]['Lyric'] = song_lyric(title, tracks[ID]['Artist']) print("name:::::"+name + "artsist::::" +tracks[ID]['Artist']) song_lyric("(Another Song) All Over Again", "Justion Timberlake") song_lyric(name,artist) ID+=1 print (ID)Full working example:
import xml.etree.ElementTree as ET import requests import musicbrainzngs import json import urllib.request, urllib.error, urllib.parse import socket apikey_musixmatch = '0b4a363bbd71974c2634837d5b5d1d9a' #generated for the example apiurl_musixmatch = 'http://api.musixmatch.com/ws/1.1/' def song_lyric(song_name,artist_name): while True: querystring = apiurl_musixmatch + "matcher.lyrics.get?q_track=" + urllib.parse.quote(song_name) + "&q_artist=" + urllib.parse.quote(artist_name) +"&apikey=" + apikey_musixmatch + "&format=json&f_has_lyrics=1" #matcher.lyrics.get?q_track=sexy%20and%20i%20know%20it&q_artist=lmfao request = urllib.request.Request(querystring) #request.add_header("Authorization", "Bearer " + client_access_token) request.add_header("User-Agent", "curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)") #Must include user agent of some sort, otherwise 403 returned while True: try: response = urllib.request.urlopen(request, timeout=4) #timeout set to 4 seconds; automatically retries if times out raw = response.read() except socket.timeout: print("Timeout raised and caught") continue break json_obj = json.loads(raw.decode('utf-8')) body = json_obj["message"]["body"]["lyrics"]["lyrics_body"] copyright = json_obj["message"]["body"]["lyrics"]["lyrics_copyright"] tracking_url = json_obj["message"]["body"]["lyrics"]["html_tracking_url"] if(tracking_url != "" and body != ""): print(tracking_url) lyrics_tracking(tracking_url) return (body + "\n\n" +copyright) else: return "None" def lyrics_tracking(tracking_url): while True: querystring = tracking_url request = urllib.request.Request(querystring) #request.add_header("Authorization", "Bearer " + client_access_token) request.add_header("User-Agent", "curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)") #Must include user agent of some sort, otherwise 403 returned try: response = urllib.request.urlopen(request, timeout=4) #timeout set to 4 seconds; automatically retries if times out raw = response.read() except socket.timeout: print("Timeout raised and caught") continue break print(raw) ID = 1 api_key = "b088cbedecd40b35dd89e90f55227ac2" #This should be you LastFM API Key bands = {} #GET TOP ARTISTS for i in range(2,5): artistslist = requests.get('http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=poland&page='+str(i)+'&api_key='+api_key) tree = ET.fromstring(artistslist.content) for child in tree: for artist in child.findall('artist'): if ID > 5: continue name = artist.find('name').text bands[ID] = {} bands[ID]['ID'] = ID bands[ID]['Name'] = name ID+=1 albuns = {} #GET 15 TOP ALBUNS FROM ARTISTS for i,v in bands.items(): chosen = bands[i]['Name'].replace(" ", "+") topalbuns = requests.get('http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist='+chosen+'&api_key='+api_key+'&limit=15') tree = ET.fromstring(topalbuns.content) for child in tree: for album in child: name = album.find('name').text albuns[ID] = {} albuns[ID]['ID'] = ID albuns[ID]['Artist'] = bands[i]['Name'] albuns[ID]['ArtistID'] = bands[i]['ID'] albuns[ID]['Title'] = name ID+=1 print(ID) print("TRACKS INFO") tracks = {} #GET TRACKS FOR ALL THE ALBUMS for i,v in albuns.items(): artist = albuns[i]['Artist'].replace(" ","+") title = albuns[i]['Title'].replace(" ", "+") album = requests.get('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key='+api_key+'&artist='+artist+'&album='+title) tree = ET.fromstring(album.content) for child in tree: for artist in child.findall('tracks'): for track in artist.findall('track'): name = track.find('name').text tracks[ID] = {} tracks[ID]['Title'] = name tracks[ID]['Artist'] = albuns[i]['Artist'] tracks[ID]['Album'] = albuns[i]['Title'] tracks[ID]['AlbumID'] = albuns[i]['ID'] tracks[ID]['Lyric'] = song_lyric(title, tracks[ID]['Artist']) print("name:::::"+name + "artsist::::" +tracks[ID]['Artist']) song_lyric("(Another Song) All Over Again", "Justion Timberlake") song_lyric(name,artist) ID+=1 print (ID)