summaryrefslogtreecommitdiff
path: root/searx/engines/yandex_music.py
blob: 810654b6f70d8bbadda06b4b0d898606626fe7e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Yandex Music

.. _Countries where Yandex.Music is available: https://yandex.com/support/music/access.html

.. hint::

   Access to music is limited to a few countries: `Countries where Yandex.Music
   is available`_

"""

from urllib.parse import urlencode

# about
about = {
    "website": 'https://music.yandex.ru',
    "wikidata_id": 'Q4537983',
    "official_api_documentation": None,
    "use_official_api": False,
    "require_api_key": False,
    "results": 'JSON',
}

# engine dependent config
categories = ['music']
paging = True

# search-url
url = 'https://music.yandex.ru'
search_url = url + '/handlers/music-search.jsx'


# do search-request
def request(query, params):
    args = {'text': query, 'page': params['pageno'] - 1}
    params['url'] = search_url + '?' + urlencode(args)

    return params


# get response from search-request
def response(resp):
    results = []
    search_res = resp.json()

    # parse results
    for result in search_res.get('tracks', {}).get('items', []):
        if result['type'] == 'music':
            track_id = result['id']
            album_id = result['albums'][0]['id']

            # append result
            results.append(
                {
                    'url': f'{url}/album/{album_id}/track/{track_id}',
                    'title': result['title'],
                    'content': f"[{result['albums'][0]['title']}] {result['artists'][0]['name']} - {result['title']}",
                    'iframe_src': f'{url}/iframe/track/{track_id}/{album_id}',
                }
            )

    return results