yandex_music.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Yandex Music
  3. .. _Countries where Yandex.Music is available: https://yandex.com/support/music/access.html
  4. .. hint::
  5. Access to music is limited to a few countries: `Countries where Yandex.Music
  6. is available`_
  7. """
  8. from urllib.parse import urlencode
  9. # about
  10. about = {
  11. "website": 'https://music.yandex.ru',
  12. "wikidata_id": 'Q4537983',
  13. "official_api_documentation": None,
  14. "use_official_api": False,
  15. "require_api_key": False,
  16. "results": 'JSON',
  17. }
  18. # engine dependent config
  19. categories = ['music']
  20. paging = True
  21. # search-url
  22. url = 'https://music.yandex.ru'
  23. search_url = url + '/handlers/music-search.jsx'
  24. # do search-request
  25. def request(query, params):
  26. args = {'text': query, 'page': params['pageno'] - 1}
  27. params['url'] = search_url + '?' + urlencode(args)
  28. return params
  29. # get response from search-request
  30. def response(resp):
  31. results = []
  32. search_res = resp.json()
  33. # parse results
  34. for result in search_res.get('tracks', {}).get('items', []):
  35. if result['type'] == 'music':
  36. track_id = result['id']
  37. album_id = result['albums'][0]['id']
  38. # append result
  39. results.append(
  40. {
  41. 'url': f'{url}/album/{album_id}/track/{track_id}',
  42. 'title': result['title'],
  43. 'content': f"[{result['albums'][0]['title']}] {result['artists'][0]['name']} - {result['title']}",
  44. 'iframe_src': f'{url}/iframe/track/{track_id}/{album_id}',
  45. }
  46. )
  47. return results