From 0f7483140ae823f5696beef4325265606377a217 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Tue, 6 Aug 2019 17:54:26 +0000 Subject: [PATCH] Add bandcamp engine --- engines/bandcamp.py | 42 ++++++++++++++++++++++++++++++++++ result_templates/bandcamp.html | 13 +++++++++++ 2 files changed, 55 insertions(+) create mode 100644 engines/bandcamp.py create mode 100644 result_templates/bandcamp.html diff --git a/engines/bandcamp.py b/engines/bandcamp.py new file mode 100644 index 0000000..364fbb2 --- /dev/null +++ b/engines/bandcamp.py @@ -0,0 +1,42 @@ +from lxml import html +from searx.engines.xpath import extract_text +from searx.url_utils import quote + +categories = ['music'] + +url = ' https://bandcamp.com/search' +search_url = url + '?q={query}&page={pageno}' + +def clean_url(url): + return url.split('?')[0] + +def request(query, params): + params['url'] = search_url.format(query=quote(query), pageno=params['pageno']) + + return params + +def response(resp): + results = [] + + dom = html.fromstring(resp.text.encode('utf-8')) + + for result in dom.xpath('//li[starts-with(@class, "searchresult")]'): + result_info = result.xpath('div[@class="result-info"]')[0] + + result_type = extract_text(result_info.xpath('div[@class="itemtype"]/text()')).capitalize() + if result_type == 'Fan': + continue + + title = extract_text(result_info.xpath('div[@class="heading"]/a/text()')) + subhead = extract_text(result_info.xpath('div[@class="subhead"]/text()')) + href = clean_url(result_info.xpath('div[@class="heading"]/a/@href')[0]) + image = result.xpath('a[@class="artcont"]/div[@class="art"]/img/@src')[0] + + results.append({'url': href, + 'title': title, + 'image': image, + 'subhead': subhead, + 'type': result_type, + 'template': 'bandcamp.html'}) + + return results diff --git a/result_templates/bandcamp.html b/result_templates/bandcamp.html new file mode 100644 index 0000000..3043be4 --- /dev/null +++ b/result_templates/bandcamp.html @@ -0,0 +1,13 @@ +{% from 'oscar/macros.html' import result_link, result_footer, result_footer_rtl%} + +

+ {% if result.image %}{% endif %} + {{ result_link(result.url, result.title|safe) }} +

+{{ result.type }}; {{ result.subhead }} + +{% if rtl %} +{{ result_footer_rtl(result) }} +{% else %} +{{ result_footer(result) }} +{% endif %}