bandcamp.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. search_path = search_string.format(query=urlencode({'q': query}), page=params['pageno'])
  32. params['url'] = base_url + search_path
  33. return params
  34. def response(resp):
  35. results = []
  36. dom = html.fromstring(resp.text)
  37. for result in eval_xpath_list(dom, '//li[contains(@class, "searchresult")]'):
  38. link = eval_xpath_getindex(result, './/div[@class="itemurl"]/a', 0, default=None)
  39. if link is None:
  40. continue
  41. title = result.xpath('.//div[@class="heading"]/a/text()')
  42. content = result.xpath('.//div[@class="subhead"]/text()')
  43. new_result = {
  44. "url": extract_text(link),
  45. "title": extract_text(title),
  46. "content": extract_text(content),
  47. }
  48. date = eval_xpath_getindex(result, '//div[@class="released"]/text()', 0, default=None)
  49. if date:
  50. new_result["publishedDate"] = dateparse(date.replace("released ", ""))
  51. thumbnail = result.xpath('.//div[@class="art"]/img/@src')
  52. if thumbnail:
  53. new_result['thumbnail'] = thumbnail[0]
  54. result_id = parse_qs(urlparse(link.get('href')).query)["search_item_id"][0]
  55. itemtype = extract_text(result.xpath('.//div[@class="itemtype"]')).lower()
  56. if "album" == itemtype:
  57. new_result["iframe_src"] = iframe_src.format(type='album', result_id=result_id)
  58. elif "track" == itemtype:
  59. new_result["iframe_src"] = iframe_src.format(type='track', result_id=result_id)
  60. results.append(new_result)
  61. return results