soundcloud.py 3.2 KB

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