.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
spotify.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Spotify (Music)
3
4"""
5
6from json import loads
7from urllib.parse import urlencode
8import base64
9
10from searx.network import post as http_post
11
12# about
13about = {
14 "website": 'https://www.spotify.com',
15 "wikidata_id": 'Q689141',
16 "official_api_documentation": 'https://developer.spotify.com/web-api/search-item/',
17 "use_official_api": True,
18 "require_api_key": False,
19 "results": 'JSON',
20}
21
22# engine dependent config
23categories = ['music']
24paging = True
25api_client_id = None
26api_client_secret = None
27
28# search-url
29url = 'https://api.spotify.com/'
30search_url = url + 'v1/search?{query}&type=track&offset={offset}'
31
32
33# do search-request
34def request(query, params):
35 offset = (params['pageno'] - 1) * 20
36
37 params['url'] = search_url.format(query=urlencode({'q': query}), offset=offset)
38
39 r = http_post(
40 'https://accounts.spotify.com/api/token',
41 data={'grant_type': 'client_credentials'},
42 headers={
43 'Authorization': 'Basic '
44 + base64.b64encode("{}:{}".format(api_client_id, api_client_secret).encode()).decode()
45 },
46 )
47 j = loads(r.text)
48 params['headers'] = {'Authorization': 'Bearer {}'.format(j.get('access_token'))}
49
50 return params
51
52
53# get response from search-request
54def response(resp):
55 results = []
56
57 search_res = loads(resp.text)
58
59 # parse results
60 for result in search_res.get('tracks', {}).get('items', {}):
61 if result['type'] == 'track':
62 title = result['name']
63 link = result['external_urls']['spotify']
64 content = '{} - {} - {}'.format(result['artists'][0]['name'], result['album']['name'], result['name'])
65
66 # append result
67 results.append(
68 {
69 'url': link,
70 'title': title,
71 'iframe_src': "https://embed.spotify.com/?uri=spotify:track:" + result['id'],
72 'content': content,
73 }
74 )
75
76 # return results
77 return results
request(query, params)
Definition spotify.py:34