2022-02-17 22:10:34 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# lint: pylint
|
|
|
|
"""Bandcamp (Music)
|
2020-12-28 05:46:11 +01:00
|
|
|
|
|
|
|
@website https://bandcamp.com/
|
|
|
|
@provide-api no
|
|
|
|
@results HTML
|
2022-02-13 16:12:46 +01:00
|
|
|
@parse url, title, content, publishedDate, iframe_src, thumbnail
|
2022-02-17 22:10:34 +01:00
|
|
|
|
2020-12-28 05:46:11 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
from urllib.parse import urlencode, urlparse, parse_qs
|
|
|
|
from dateutil.parser import parse as dateparse
|
|
|
|
from lxml import html
|
2022-02-17 22:10:34 +01:00
|
|
|
|
|
|
|
from searx.utils import (
|
|
|
|
eval_xpath_getindex,
|
|
|
|
eval_xpath_list,
|
|
|
|
extract_text,
|
|
|
|
)
|
2020-12-28 05:46:11 +01:00
|
|
|
|
2021-09-18 11:02:39 +02:00
|
|
|
# about
|
|
|
|
about = {
|
|
|
|
"website": 'https://bandcamp.com/',
|
|
|
|
"wikidata_id": 'Q545966',
|
|
|
|
"official_api_documentation": 'https://bandcamp.com/developer',
|
|
|
|
"use_official_api": False,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'HTML',
|
|
|
|
}
|
|
|
|
|
2020-12-28 05:46:11 +01:00
|
|
|
categories = ['music']
|
|
|
|
paging = True
|
|
|
|
|
|
|
|
base_url = "https://bandcamp.com/"
|
2022-02-17 22:10:34 +01:00
|
|
|
search_string = 'search?{query}&page={page}'
|
|
|
|
iframe_src = "https://bandcamp.com/EmbeddedPlayer/{type}={result_id}/size=large/bgcol=000/linkcol=fff/artwork=small"
|
2020-12-28 05:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
def request(query, params):
|
|
|
|
'''pre-request callback
|
2022-02-17 22:10:34 +01:00
|
|
|
|
2020-12-28 05:46:11 +01:00
|
|
|
params<dict>:
|
|
|
|
method : POST/GET
|
|
|
|
headers : {}
|
|
|
|
data : {} # if method == POST
|
|
|
|
url : ''
|
|
|
|
category: 'search category'
|
|
|
|
pageno : 1 # number of the requested page
|
|
|
|
'''
|
|
|
|
|
2021-12-27 09:26:22 +01:00
|
|
|
search_path = search_string.format(query=urlencode({'q': query}), page=params['pageno'])
|
2020-12-28 05:46:11 +01:00
|
|
|
params['url'] = base_url + search_path
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def response(resp):
|
|
|
|
'''post-response callback
|
2022-02-17 22:10:34 +01:00
|
|
|
|
2020-12-28 05:46:11 +01:00
|
|
|
resp: requests response object
|
|
|
|
'''
|
|
|
|
results = []
|
2022-02-17 22:10:34 +01:00
|
|
|
dom = html.fromstring(resp.text)
|
|
|
|
|
|
|
|
for result in eval_xpath_list(dom, '//li[contains(@class, "searchresult")]'):
|
|
|
|
|
|
|
|
link = eval_xpath_getindex(result, './/div[@class="itemurl"]/a', 0, default=None)
|
|
|
|
if link is None:
|
|
|
|
continue
|
|
|
|
|
2020-12-29 15:49:41 +01:00
|
|
|
title = result.xpath('.//div[@class="heading"]/a/text()')
|
|
|
|
content = result.xpath('.//div[@class="subhead"]/text()')
|
2020-12-28 05:46:11 +01:00
|
|
|
new_result = {
|
|
|
|
"url": extract_text(link),
|
2020-12-29 15:49:41 +01:00
|
|
|
"title": extract_text(title),
|
|
|
|
"content": extract_text(content),
|
2020-12-28 05:46:11 +01:00
|
|
|
}
|
2022-02-17 22:10:34 +01:00
|
|
|
|
|
|
|
date = eval_xpath_getindex(result, '//div[@class="released"]/text()', 0, default=None)
|
|
|
|
if date:
|
|
|
|
new_result["publishedDate"] = dateparse(date.replace("released ", ""))
|
|
|
|
|
2020-12-29 15:49:41 +01:00
|
|
|
thumbnail = result.xpath('.//div[@class="art"]/img/@src')
|
|
|
|
if thumbnail:
|
|
|
|
new_result['thumbnail'] = thumbnail[0]
|
2022-02-17 22:10:34 +01:00
|
|
|
|
|
|
|
result_id = parse_qs(urlparse(link.get('href')).query)["search_item_id"][0]
|
|
|
|
itemtype = extract_text(result.xpath('.//div[@class="itemtype"]')).lower()
|
|
|
|
if "album" == itemtype:
|
2022-02-13 16:12:46 +01:00
|
|
|
new_result["iframe_src"] = iframe_src.format(type='album', result_id=result_id)
|
2022-02-17 22:10:34 +01:00
|
|
|
elif "track" == itemtype:
|
2022-02-13 16:12:46 +01:00
|
|
|
new_result["iframe_src"] = iframe_src.format(type='track', result_id=result_id)
|
2022-02-07 21:59:21 +01:00
|
|
|
|
2020-12-28 05:46:11 +01:00
|
|
|
results.append(new_result)
|
|
|
|
return results
|