.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
6import re
7from json import loads
8from urllib.parse import quote_plus, urlencode
9from lxml import html
10from dateutil import parser
11from searx.network import get as http_get
12
13# about
14about = {
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
24categories = ['music']
25paging = True
26
27# search-url
28# missing attribute: user_id, app_version, app_locale
29url = 'https://api-v2.soundcloud.com/'
30search_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
40cid_re = re.compile(r'client_id:"([^"]*)"', re.I | re.U)
41guest_client_id = ''
42
43
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
66def 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
73def 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
81def 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 img_src = result['artwork_url'] or result['user']['avatar_url']
98 if img_src:
99 res['img_src'] = img_src
100 results.append(res)
101
102 return results
init(engine_settings=None)
Definition soundcloud.py:66
request(query, params)
Definition soundcloud.py:73