soundcloud.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. Soundcloud (Music)
  3. @website https://soundcloud.com
  4. @provide-api yes (https://developers.soundcloud.com/)
  5. @using-api yes
  6. @results JSON
  7. @stable yes
  8. @parse url, title, content, publishedDate, embedded
  9. """
  10. import re
  11. from json import loads
  12. from lxml import html
  13. from dateutil import parser
  14. from io import StringIO
  15. from urllib.parse import quote_plus, urlencode
  16. from searx import logger
  17. from searx.poolrequests import get as http_get
  18. # engine dependent config
  19. categories = ['music']
  20. paging = True
  21. # search-url
  22. # missing attribute: user_id, app_version, app_locale
  23. url = 'https://api-v2.soundcloud.com/'
  24. search_url = url + 'search?{query}'\
  25. '&variant_ids='\
  26. '&facet=model'\
  27. '&limit=20'\
  28. '&offset={offset}'\
  29. '&linked_partitioning=1'\
  30. '&client_id={client_id}' # noqa
  31. embedded_url = '<iframe width="100%" height="166" ' +\
  32. 'scrolling="no" frameborder="no" ' +\
  33. 'data-src="https://w.soundcloud.com/player/?url={uri}"></iframe>'
  34. cid_re = re.compile(r'client_id:"([^"]*)"', re.I | re.U)
  35. guest_client_id = ''
  36. def get_client_id():
  37. response = http_get("https://soundcloud.com")
  38. if response.ok:
  39. tree = html.fromstring(response.content)
  40. # script_tags has been moved from /assets/app/ to /assets/ path. I
  41. # found client_id in https://a-v2.sndcdn.com/assets/49-a0c01933-3.js
  42. script_tags = tree.xpath("//script[contains(@src, '/assets/')]")
  43. app_js_urls = [script_tag.get('src') for script_tag in script_tags if script_tag is not None]
  44. # extracts valid app_js urls from soundcloud.com content
  45. for app_js_url in app_js_urls:
  46. # gets app_js and searches for the clientid
  47. response = http_get(app_js_url)
  48. if response.ok:
  49. cids = cid_re.search(response.content.decode())
  50. if cids is not None and len(cids.groups()):
  51. return cids.groups()[0]
  52. logger.warning("Unable to fetch guest client_id from SoundCloud, check parser!")
  53. return ""
  54. def init(engine_settings=None):
  55. global guest_client_id
  56. # api-key
  57. guest_client_id = get_client_id()
  58. # do search-request
  59. def request(query, params):
  60. offset = (params['pageno'] - 1) * 20
  61. params['url'] = search_url.format(query=urlencode({'q': query}),
  62. offset=offset,
  63. client_id=guest_client_id)
  64. return params
  65. # get response from search-request
  66. def response(resp):
  67. results = []
  68. search_res = loads(resp.text)
  69. # parse results
  70. for result in search_res.get('collection', []):
  71. if result['kind'] in ('track', 'playlist'):
  72. title = result['title']
  73. content = result['description']
  74. publishedDate = parser.parse(result['last_modified'])
  75. uri = quote_plus(result['uri'])
  76. embedded = embedded_url.format(uri=uri)
  77. # append result
  78. results.append({'url': result['permalink_url'],
  79. 'title': title,
  80. 'publishedDate': publishedDate,
  81. 'embedded': embedded,
  82. 'content': content})
  83. # return results
  84. return results