deezer.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. Deezer (Music)
  3. @website https://deezer.com
  4. @provide-api yes (http://developers.deezer.com/api/)
  5. @using-api yes
  6. @results JSON
  7. @stable yes
  8. @parse url, title, content, embedded
  9. """
  10. from json import loads
  11. from urllib import urlencode
  12. # engine dependent config
  13. categories = ['music']
  14. paging = True
  15. # search-url
  16. url = 'https://api.deezer.com/'
  17. search_url = url + 'search?{query}&index={offset}'
  18. embedded_url = '<iframe scrolling="no" frameborder="0" allowTransparency="true" ' +\
  19. 'data-src="https://www.deezer.com/plugins/player?type=tracks&id={audioid}" ' +\
  20. 'width="540" height="80"></iframe>'
  21. # do search-request
  22. def request(query, params):
  23. offset = (params['pageno'] - 1) * 25
  24. params['url'] = search_url.format(query=urlencode({'q': query}),
  25. offset=offset)
  26. return params
  27. # get response from search-request
  28. def response(resp):
  29. results = []
  30. search_res = loads(resp.text)
  31. # parse results
  32. for result in search_res.get('data', []):
  33. if result['type'] == 'track':
  34. title = result['title']
  35. url = result['link']
  36. if url.startswith('http://'):
  37. url = 'https' + url[4:]
  38. content = result['artist']['name'] +\
  39. " &bull; " +\
  40. result['album']['title'] +\
  41. " &bull; " + result['title']
  42. embedded = embedded_url.format(audioid=result['id'])
  43. # append result
  44. results.append({'url': url,
  45. 'title': title,
  46. 'embedded': embedded,
  47. 'content': content})
  48. # return results
  49. return results