.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
6
from
json
import
loads
7
from
urllib.parse
import
urlencode
8
import
base64
9
10
from
searx.network
import
post
as
http_post
11
12
# about
13
about = {
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
23
categories = [
'music'
]
24
paging =
True
25
api_client_id =
None
26
api_client_secret =
None
27
28
# search-url
29
url =
'https://api.spotify.com/'
30
search_url = url +
'v1/search?{query}&type=track&offset={offset}'
31
32
33
# do search-request
34
def
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
54
def
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
searx.engines.spotify.request
request(query, params)
Definition
spotify.py:34
searx.engines.spotify.response
response(resp)
Definition
spotify.py:54
searx.network
Definition
__init__.py:1
searx.format
format
Definition
__init__.py:94
searxng
searx
engines
spotify.py
Generated on Sat Nov 16 2024 00:10:57 for .oO SearXNG Developer Documentation Oo. by
1.12.0