.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
soundcloud.py
Go to the documentation of this file.
1
# SPDX-License-Identifier: AGPL-3.0-or-later
2
"""
3
Soundcloud (Music)
4
"""
5
6
import
re
7
from
json
import
loads
8
from
urllib.parse
import
quote_plus, urlencode
9
from
lxml
import
html
10
from
dateutil
import
parser
11
from
searx.network
import
get
as
http_get
12
13
# about
14
about = {
15
"website"
:
'https://soundcloud.com'
,
16
"wikidata_id"
:
'Q568769'
,
17
"official_api_documentation"
:
'https://developers.soundcloud.com/'
,
18
"use_official_api"
:
True
,
19
"require_api_key"
:
False
,
20
"results"
:
'JSON'
,
21
}
22
23
# engine dependent config
24
categories = [
'music'
]
25
paging =
True
26
27
# search-url
28
# missing attribute: user_id, app_version, app_locale
29
url =
'https://api-v2.soundcloud.com/'
30
search_url = (
31
url +
'search?{query}'
32
'&variant_ids='
33
'&facet=model'
34
'&limit=20'
35
'&offset={offset}'
36
'&linked_partitioning=1'
37
'&client_id={client_id}'
38
)
# noqa
39
40
cid_re = re.compile(
r'client_id:"([^"]*)"'
, re.I | re.U)
41
guest_client_id =
''
42
43
44
def
get_client_id
():
45
resp = http_get(
"https://soundcloud.com"
)
46
47
if
resp.ok:
48
tree = html.fromstring(resp.content)
49
# script_tags has been moved from /assets/app/ to /assets/ path. I
50
# found client_id in https://a-v2.sndcdn.com/assets/49-a0c01933-3.js
51
script_tags = tree.xpath(
"//script[contains(@src, '/assets/')]"
)
52
app_js_urls = [script_tag.get(
'src'
)
for
script_tag
in
script_tags
if
script_tag
is
not
None
]
53
54
# extracts valid app_js urls from soundcloud.com content
55
for
app_js_url
in
app_js_urls[::-1]:
56
# gets app_js and searches for the clientid
57
resp = http_get(app_js_url)
58
if
resp.ok:
59
cids = cid_re.search(resp.content.decode())
60
if
cids
is
not
None
and
len(cids.groups()):
61
return
cids.groups()[0]
62
logger.warning(
"Unable to fetch guest client_id from SoundCloud, check parser!"
)
63
return
""
64
65
66
def
init
(engine_settings=None):
# pylint: disable=unused-argument
67
global
guest_client_id
# pylint: disable=global-statement
68
# api-key
69
guest_client_id =
get_client_id
()
70
71
72
# do search-request
73
def
request
(query, params):
74
offset = (params[
'pageno'
] - 1) * 20
75
76
params[
'url'
] = search_url.format(query=urlencode({
'q'
: query}), offset=offset, client_id=guest_client_id)
77
78
return
params
79
80
81
def
response
(resp):
82
results = []
83
search_res = loads(resp.text)
84
85
# parse results
86
for
result
in
search_res.get(
'collection'
, []):
87
88
if
result[
'kind'
]
in
(
'track'
,
'playlist'
):
89
uri = quote_plus(result[
'uri'
])
90
res = {
91
'url'
: result[
'permalink_url'
],
92
'title'
: result[
'title'
],
93
'content'
: result[
'description'
]
or
''
,
94
'publishedDate'
: parser.parse(result[
'last_modified'
]),
95
'iframe_src'
:
"https://w.soundcloud.com/player/?url="
+ uri,
96
}
97
thumbnail = result[
'artwork_url'
]
or
result[
'user'
][
'avatar_url'
]
98
if
thumbnail:
99
res[
'thumbnail'
] = thumbnail
100
results.append(res)
101
102
return
results
searx.engines.soundcloud.init
init(engine_settings=None)
Definition
soundcloud.py:66
searx.engines.soundcloud.request
request(query, params)
Definition
soundcloud.py:73
searx.engines.soundcloud.get_client_id
get_client_id()
Definition
soundcloud.py:44
searx.engines.soundcloud.response
response(resp)
Definition
soundcloud.py:81
searx.network
Definition
__init__.py:1
searxng
searx
engines
soundcloud.py
Generated on Sat Nov 16 2024 00:10:57 for .oO SearXNG Developer Documentation Oo. by
1.12.0