bandcamp.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Bandcamp (Music)
  3. @website https://bandcamp.com/
  4. @provide-api no
  5. @results HTML
  6. @parse url, title, content, publishedDate, iframe_src, thumbnail
  7. """
  8. from urllib.parse import urlencode, urlparse, parse_qs
  9. from dateutil.parser import parse as dateparse
  10. from lxml import html
  11. from searx.utils import (
  12. eval_xpath_getindex,
  13. eval_xpath_list,
  14. extract_text,
  15. )
  16. # about
  17. about = {
  18. "website": 'https://bandcamp.com/',
  19. "wikidata_id": 'Q545966',
  20. "official_api_documentation": 'https://bandcamp.com/developer',
  21. "use_official_api": False,
  22. "require_api_key": False,
  23. "results": 'HTML',
  24. }
  25. categories = ['music']
  26. paging = True
  27. base_url = "https://bandcamp.com/"
  28. search_string = 'search?{query}&page={page}'
  29. iframe_src = "https://bandcamp.com/EmbeddedPlayer/{type}={result_id}/size=large/bgcol=000/linkcol=fff/artwork=small"
  30. def request(query, params):
  31. '''pre-request callback
  32. params<dict>:
  33. method : POST/GET
  34. headers : {}
  35. data : {} # if method == POST
  36. url : ''
  37. category: 'search category'
  38. pageno : 1 # number of the requested page
  39. '''
  40. search_path = search_string.format(query=urlencode({'q': query}), page=params['pageno'])
  41. params['url'] = base_url + search_path
  42. return params
  43. def response(resp):
  44. '''post-response callback
  45. resp: requests response object
  46. '''
  47. results = []
  48. dom = html.fromstring(resp.text)
  49. for result in eval_xpath_list(dom, '//li[contains(@class, "searchresult")]'):
  50. link = eval_xpath_getindex(result, './/div[@class="itemurl"]/a', 0, default=None)
  51. if link is None:
  52. continue
  53. title = result.xpath('.//div[@class="heading"]/a/text()')
  54. content = result.xpath('.//div[@class="subhead"]/text()')
  55. new_result = {
  56. "url": extract_text(link),
  57. "title": extract_text(title),
  58. "content": extract_text(content),
  59. }
  60. date = eval_xpath_getindex(result, '//div[@class="released"]/text()', 0, default=None)
  61. if date:
  62. new_result["publishedDate"] = dateparse(date.replace("released ", ""))
  63. thumbnail = result.xpath('.//div[@class="art"]/img/@src')
  64. if thumbnail:
  65. new_result['img_src'] = thumbnail[0]
  66. result_id = parse_qs(urlparse(link.get('href')).query)["search_item_id"][0]
  67. itemtype = extract_text(result.xpath('.//div[@class="itemtype"]')).lower()
  68. if "album" == itemtype:
  69. new_result["iframe_src"] = iframe_src.format(type='album', result_id=result_id)
  70. elif "track" == itemtype:
  71. new_result["iframe_src"] = iframe_src.format(type='track', result_id=result_id)
  72. results.append(new_result)
  73. return results