deezer.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Deezer (Music)
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. # about
  8. about = {
  9. "website": 'https://deezer.com',
  10. "wikidata_id": 'Q602243',
  11. "official_api_documentation": 'https://developers.deezer.com/',
  12. "use_official_api": True,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. # engine dependent config
  17. categories = ['music']
  18. paging = True
  19. # search-url
  20. url = 'https://api.deezer.com/'
  21. search_url = url + 'search?{query}&index={offset}'
  22. iframe_src = "https://www.deezer.com/plugins/player?type=tracks&id={audioid}"
  23. # do search-request
  24. def request(query, params):
  25. offset = (params['pageno'] - 1) * 25
  26. params['url'] = search_url.format(query=urlencode({'q': query}), offset=offset)
  27. return params
  28. # get response from search-request
  29. def response(resp):
  30. results = []
  31. search_res = loads(resp.text)
  32. # parse results
  33. for result in search_res.get('data', []):
  34. if result['type'] == 'track':
  35. title = result['title']
  36. url = result['link'] # pylint: disable=redefined-outer-name
  37. if url.startswith('http://'):
  38. url = 'https' + url[4:]
  39. content = '{} - {} - {}'.format(result['artist']['name'], result['album']['title'], result['title'])
  40. # append result
  41. results.append(
  42. {'url': url, 'title': title, 'iframe_src': iframe_src.format(audioid=result['id']), 'content': content}
  43. )
  44. # return results
  45. return results