on master help sign in
[fix]soundcloud.com guest client_id fetches dynamically
commitdc55d87e9206b99d3e5f951d1ce31484419e61d1 authormisnyo <misnyo@misnyo.eu> dateWed 30 Dec 2015, 01:20 (11 years ago) parent35a2bc5
1 file changed+29−3
searx/engines/soundcloud.py+29−3
searx/engines/soundcloud.py+29−3
@@ -10,17 +10,19 @@
1010 @parse url, title, content, publishedDate, embedded
1111"""
1212
13+import re
14+from StringIO import StringIO
1315from json import loads
16+from lxml import etree
1417from urllib import urlencode, quote_plus
1518from dateutil import parser
19+from searx import logger
20+from searx.poolrequests import get as http_get
1621
1722# engine dependent config
1823categories = ['music']
1924paging = True
2025
21−# api-key
22−guest_client_id = 'b45b1aa10f1ac2941910a7f0d10f8e28'
23−
2426# search-url
2527url = 'https://api.soundcloud.com/'
2628search_url = url + 'search?{query}'\
@@ -35,6 +37,30 @@ embedded_url = '<iframe width="100%" height="166" ' +\
3537 'data-src="https://w.soundcloud.com/player/?url={uri}"></iframe>'
3638
3739
40+def get_client_id():
41+ response = http_get("https://soundcloud.com")
42+ rx_namespace = {"re": "http://exslt.org/regular-expressions"}
43+
44+ if response.ok:
45+ tree = etree.parse(StringIO(response.content), etree.HTMLParser())
46+ script_tags = tree.xpath("//script[re:match(@src, '(.*app.*js)')]", namespaces=rx_namespace)
47+ app_js_urls = [script_tag.get('src') for script_tag in script_tags if script_tag is not None]
48+
49+ # extracts valid app_js urls from soundcloud.com content
50+ for app_js_url in app_js_urls:
51+ # gets app_js and searches for the clientid
52+ response = http_get(app_js_url)
53+ if response.ok:
54+ cids = re.search(r'client_id:"([^"]*)"', response.content, re.M | re.I)
55+ if cids is not None and len(cids.groups()):
56+ return cids.groups()[0]
57+ logger.warning("Unable to fetch guest client_id from SoundCloud, check parser!")
58+ return ""
59+
60+# api-key
61+guest_client_id = get_client_id()
62+
63+
3864# do search-request
3965def request(query, params):
4066 offset = (params['pageno'] - 1) * 20