.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
unsplash.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Unsplash
3
4"""
5
6from urllib.parse import urlencode, urlparse, urlunparse, parse_qsl
7from json import loads
8
9# about
10about = {
11 "website": 'https://unsplash.com',
12 "wikidata_id": 'Q28233552',
13 "official_api_documentation": 'https://unsplash.com/developers',
14 "use_official_api": False,
15 "require_api_key": False,
16 "results": 'JSON',
17}
18
19base_url = 'https://unsplash.com/'
20search_url = base_url + 'napi/search/photos?'
21categories = ['images']
22page_size = 20
23paging = True
24
25
26def clean_url(url):
27 parsed = urlparse(url)
28 query = [(k, v) for (k, v) in parse_qsl(parsed.query) if k not in ['ixid', 's']]
29
30 return urlunparse((parsed.scheme, parsed.netloc, parsed.path, parsed.params, urlencode(query), parsed.fragment))
31
32
33def request(query, params):
34 params['url'] = search_url + urlencode({'query': query, 'page': params['pageno'], 'per_page': page_size})
35 logger.debug("query_url --> %s", params['url'])
36 return params
37
38
39def response(resp):
40 results = []
41 json_data = loads(resp.text)
42
43 if 'results' in json_data:
44 for result in json_data['results']:
45 results.append(
46 {
47 'template': 'images.html',
48 'url': clean_url(result['links']['html']),
49 'thumbnail_src': clean_url(result['urls']['thumb']),
50 'img_src': clean_url(result['urls']['raw']),
51 'title': result.get('alt_description') or 'unknown',
52 'content': result.get('description') or '',
53 }
54 )
55
56 return results
request(query, params)
Definition unsplash.py:33