.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
bandcamp.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Bandcamp (Music)
3
4@website https://bandcamp.com/
5@provide-api no
6@results HTML
7@parse url, title, content, publishedDate, iframe_src, thumbnail
8
9"""
10
11from urllib.parse import urlencode, urlparse, parse_qs
12from dateutil.parser import parse as dateparse
13from lxml import html
14
15from searx.utils import (
16 eval_xpath_getindex,
17 eval_xpath_list,
18 extract_text,
19)
20
21# about
22about = {
23 "website": 'https://bandcamp.com/',
24 "wikidata_id": 'Q545966',
25 "official_api_documentation": 'https://bandcamp.com/developer',
26 "use_official_api": False,
27 "require_api_key": False,
28 "results": 'HTML',
29}
30
31categories = ['music']
32paging = True
33
34base_url = "https://bandcamp.com/"
35search_string = 'search?{query}&page={page}'
36iframe_src = "https://bandcamp.com/EmbeddedPlayer/{type}={result_id}/size=large/bgcol=000/linkcol=fff/artwork=small"
37
38
39def request(query, params):
40
41 search_path = search_string.format(query=urlencode({'q': query}), page=params['pageno'])
42 params['url'] = base_url + search_path
43 return params
44
45
46def response(resp):
47
48 results = []
49 dom = html.fromstring(resp.text)
50
51 for result in eval_xpath_list(dom, '//li[contains(@class, "searchresult")]'):
52
53 link = eval_xpath_getindex(result, './/div[@class="itemurl"]/a', 0, default=None)
54 if link is None:
55 continue
56
57 title = result.xpath('.//div[@class="heading"]/a/text()')
58 content = result.xpath('.//div[@class="subhead"]/text()')
59 new_result = {
60 "url": extract_text(link),
61 "title": extract_text(title),
62 "content": extract_text(content),
63 }
64
65 date = eval_xpath_getindex(result, '//div[@class="released"]/text()', 0, default=None)
66 if date:
67 new_result["publishedDate"] = dateparse(date.replace("released ", ""))
68
69 thumbnail = result.xpath('.//div[@class="art"]/img/@src')
70 if thumbnail:
71 new_result['thumbnail'] = thumbnail[0]
72
73 result_id = parse_qs(urlparse(link.get('href')).query)["search_item_id"][0]
74 itemtype = extract_text(result.xpath('.//div[@class="itemtype"]')).lower()
75 if "album" == itemtype:
76 new_result["iframe_src"] = iframe_src.format(type='album', result_id=result_id)
77 elif "track" == itemtype:
78 new_result["iframe_src"] = iframe_src.format(type='track', result_id=result_id)
79
80 results.append(new_result)
81 return results
request(query, params)
Definition bandcamp.py:39