bandcamp.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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()')
  46. date = dateparse(result.xpath('//div[@class="released"]/text()')[0].replace("released ", ""))
  47. content = result.xpath('.//div[@class="subhead"]/text()')
  48. new_result = {
  49. "url": extract_text(link),
  50. "title": extract_text(title),
  51. "content": extract_text(content),
  52. "publishedDate": date,
  53. }
  54. thumbnail = result.xpath('.//div[@class="art"]/img/@src')
  55. if thumbnail:
  56. new_result['thumbnail'] = thumbnail[0]
  57. if "album" in result.classes:
  58. new_result["embedded"] = embedded_url.format(type='album', result_id=result_id)
  59. elif "track" in result.classes:
  60. new_result["embedded"] = embedded_url.format(type='track', result_id=result_id)
  61. results.append(new_result)
  62. return results