To solve the issue Im doing like below, verifying if the body is not empty, but its not working it appears the same issue.
{'message': {'header': {'status_code': 404, 'execute_time': 0.0011160373687744}, 'body': []}}
Thanks! It worked with "if json_obj["message"]["body"] != [].
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() #print(raw) except socket.timeout: print("Timeout raised and caught") continue break json_obj = json.loads(raw.decode('utf-8')) if json_obj["message"]["body"] != "": # verifying if body is empty print(json_obj) 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!= ""): #print(body) #this print blank #print(tracking_url) lyrics_tracking(tracking_url) return (body + "\n\n" +copyright) else: return "None" else: return "None"Also when there is no track the response is:
{'message': {'header': {'status_code': 404, 'execute_time': 0.0011160373687744}, 'body': []}}
Thanks! It worked with "if json_obj["message"]["body"] != [].