logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

soundcloud.py (3160B)


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