bandcamp.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. Bandcamp (Music)
  3. @website https://bandcamp.com/
  4. @provide-api no
  5. @results HTML
  6. @parse url, title, content, publishedDate, embedded, 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 extract_text
  12. categories = ['music']
  13. paging = True
  14. base_url = "https://bandcamp.com/"
  15. search_string = search_string = 'search?{query}&page={page}'
  16. embedded_url = '''<iframe width="100%" height="166"
  17. scrolling="no" frameborder="no"
  18. data-src="https://bandcamp.com/EmbeddedPlayer/{type}={result_id}/size=large/bgcol=ffffff/linkcol=0687f5/tracklist=false/artwork=small/transparent=true/"
  19. ></iframe>'''
  20. def request(query, params):
  21. '''pre-request callback
  22. params<dict>:
  23. method : POST/GET
  24. headers : {}
  25. data : {} # if method == POST
  26. url : ''
  27. category: 'search category'
  28. pageno : 1 # number of the requested page
  29. '''
  30. search_path = search_string.format(
  31. query=urlencode({'q': query}),
  32. page=params['pageno'])
  33. params['url'] = base_url + search_path
  34. return params
  35. def response(resp):
  36. '''post-response callback
  37. resp: requests response object
  38. '''
  39. results = []
  40. tree = html.fromstring(resp.text)
  41. search_results = tree.xpath('//li[contains(@class, "searchresult")]')
  42. for result in search_results:
  43. link = result.xpath('//div[@class="itemurl"]/a')[0]
  44. result_id = parse_qs(urlparse(link.get('href')).query)["search_item_id"][0]
  45. title = result.xpath('//div[@class="heading"]/a/text()')[0]
  46. date = dateparse(result.xpath('//div[@class="released"]/text()')[0].replace("released ", ""))
  47. content = result.xpath('//div[@class="subhead"]/text()')[0]
  48. thumbnail = result.xpath('//div[@class="art"]/img/@src')[0]
  49. new_result = {
  50. "url": extract_text(link),
  51. "title": title,
  52. "content": content,
  53. "publishedDate": date,
  54. "thumbnail": thumbnail,
  55. }
  56. if "album" in result.classes:
  57. new_result["embedded"] = embedded_url.format(type='album', result_id=result_id)
  58. elif "track" in result.classes:
  59. new_result["embedded"] = embedded_url.format(type='track', result_id=result_id)
  60. results.append(new_result)
  61. return results