python encode string to html unicode for deezer api

174 views Asked by At

I'm struggling with the deezer api. I want to perform a request which contains umlaute. While the usual python requests library converts the following:
'album:"Ich schwanke noch" artist:"Ikke Hüftgold"'

To this api search request:
https://api.deezer.com/search/album?q=album%3A%22Ich+schwanke+noch%22+artist%3A%22Ikke+H%C3%BCftgold%22

Which is from utf-8 standards, totally fine as long as I understood.
But unfortunately, deezer don't accept them or just delivers an empty result.

This would be the code to reproduce:

import requests
search_url = 'https://api.deezer.com/search/'
query_type = 'album'

query = 'album:"Ich schwanke noch" artist:"Ikke Hüftgold"'
response = requests.get(
    search_url + query_type, params={'q': query}
)
print(response.url)

After a deep dive I found out, deezer accepts only the following character set. Only the two Unicode (HTML) from this article: https://de.wikipedia.org/wiki/Hilfe:Sonderzeichenreferenz

So manually the request look like this:
https://api.deezer.com/search/album?q=album:%22Ich%20schwanke%20noch%22%20artist:%22Ikke%20Hüftgold%22

But how on earth could I tell python to encode using this set. I didn't find anything till now.

I would love to found help on this way.

1

There are 1 answers

4
Maurice Meyer On

This has nothing to do with the encoding, it's how you are using the the search API:

import requests
search_url = 'https://api.deezer.com/search/'

query = 'Ikke Hüftgold Ich schwanke noch'
response = requests.get(
    search_url, params={'q': query}
)

resp = response.json()

for item in resp["data"]:
    print(item['title'], item['artist']['name'], item['album']['title'], item['album']['id'])

Out:

Ich schwanke noch Ikke Hüftgold Ich schwanke noch 226034622
Ich schwanke noch Ikke Hüftgold Ballermann Playa Hits 2021 235679732
Ich schwanke noch Ikke Hüftgold Après Ski Hütten Hits 2022 271034782
Ich schwanke noch Ikke Hüftgold Oktoberfest Megamix 2021 248418852
Ich schwanke noch Ikke Hüftgold Ballermann Charts 2021 - Der Sommer unseres Lebens 239857652
Ich schwanke noch Ikke Hüftgold Ballermann Après Ski Mega Hits 2022 271529842
Ich schwanke noch Ikke Hüftgold Ballermann Après Ski Party Hits 2022 270321652
Ich schwanke noch Ikke Hüftgold Ballermann Après-Ski Party 2022 269952722
Ich schwanke noch Ikke Hüftgold Wiesn Megamix 2021 : Die Oktoberfest Party Hits 247638622
Ich schwanke noch Ikke Hüftgold Karneval Megamix 2022 282534992
Ich schwanke noch Ikke Hüftgold Ballermann Summer Megahits 2021 - Die Party Songs Von Der Playa 241106462
Ich schwanke noch Ikke Hüftgold Oktoberfest Partyhits 2021: Die Kracher aus dem Bierzelt 255573382
Ich schwanke noch Ikke Hüftgold Ballermann Partyalarm 2021 246561782
Ich schwanke noch Ikke Hüftgold Ballermann Oktoberfest Hits 2021 246828062
Ich schwanke noch Ikke Hüftgold Ballermann Ski Hits Top 100 2022: Der ultimative Party Megamix 263551832
Ich schwanke noch Ikke Hüftgold Ballermann Hüttengaudi 2022 270698482
Ich schwanke noch Ikke Hüftgold Hütten Charts Megamix 2022: Die ultimative Après Ski Party 270840962
Ich schwanke noch Ikke Hüftgold Ballermann Party Knüller 2021 237380692
Ich schwanke noch Ikke Hüftgold Ballermann - We Are Back - Die Party Hits Des Sommers 2021 239281792
Ich schwanke noch Ikke Hüftgold Après Ski Megahits 2022 258969192
Ich schwanke noch Ikke Hüftgold Ballermann Party Classics: Die besten Hits von damals bis heute 242757312
Ich schwanke noch Ikke Hüftgold Ballermann Schlager Hitmix 2021.2 249705922
Ich schwanke noch Ikke Hüftgold Oktoberfest CLASSICS XXL : Die besten Wiesn Party Hits von damals bis heute 257765392
Ich schwanke noch Ikke Hüftgold Ballermann Oktoberfest 2021 249970202
Ich schwanke noch Ikke Hüftgold Oktoberfest Party Classics 2021 249529552

Edit:

You need to convert non-ASCII characters to HTML Unicode entities:

#coding: utf-8
import html
import requests

response = requests.get("https://api.deezer.com/search/album?q=album:%22Ich%20schwanke%20noch%22%20artist:%22Ikke%20H&#252ftgold%22")
expectedResponse = response.json()

query = 'album:"Ich schwanke noch" artist="Ikke Hüftgold"'

q = html.escape(query, quote=False).encode('ascii', 'xmlcharrefreplace').decode()
print(q)

response = requests.get(
    "https://api.deezer.com/search/album", params={'q': q}
)

resp = response.json()
print(response.url)
print(expectedResponse == resp)

Out:

album:"Ich schwanke noch" artist="Ikke Hüftgold"
https://api.deezer.com/search/album?q=album%3A%22Ich+schwanke+noch%22+artist%3D%22Ikke+H%26%23252%3Bftgold%22
True