.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
ipernity.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Ipernity (images)"""
3
4from datetime import datetime
5from json import loads, JSONDecodeError
6
7from urllib.parse import quote_plus
8from lxml import html
9
10from searx.utils import extr, extract_text, eval_xpath, eval_xpath_list
11
12about = {
13 'website': 'https://www.ipernity.com',
14 'official_api_documentation': 'https://www.ipernity.com/help/api',
15 'use_official_api': False,
16 'require_api_key': False,
17 'results': 'HTML',
18}
19
20paging = True
21categories = ['images']
22
23
24base_url = 'https://www.ipernity.com'
25page_size = 10
26
27
28def request(query, params):
29 params['url'] = f"{base_url}/search/photo/@/page:{params['pageno']}:{page_size}?q={quote_plus(query)}"
30 return params
31
32
33def response(resp):
34 results = []
35
36 doc = html.fromstring(resp.text)
37
38 images = eval_xpath_list(doc, '//a[starts-with(@href, "/doc")]//img')
39
40 result_index = 0
41 for result in eval_xpath_list(doc, '//script[@type="text/javascript"]'):
42 info_js = extr(extract_text(result), '] = ', '};') + '}'
43
44 if not info_js:
45 continue
46
47 try:
48 info_item = loads(info_js)
49
50 if not info_item.get('mediakey'):
51 continue
52
53 thumbnail_src = extract_text(eval_xpath(images[result_index], './@src'))
54 img_src = thumbnail_src.replace('240.jpg', '640.jpg')
55
56 resolution = None
57 if info_item.get("width") and info_item.get("height"):
58 resolution = f'{info_item["width"]}x{info_item["height"]}'
59
60 item = {
61 'template': 'images.html',
62 'url': f"{base_url}/doc/{info_item['user_id']}/{info_item['doc_id']}",
63 'title': info_item.get('title'),
64 'content': info_item.get('content', ''),
65 'resolution': resolution,
66 'publishedDate': datetime.fromtimestamp(int(info_item['posted_at'])),
67 'thumbnail_src': thumbnail_src,
68 'img_src': img_src,
69 }
70 results.append(item)
71
72 result_index += 1
73 except JSONDecodeError:
74 continue
75
76 return results
request(query, params)
Definition ipernity.py:28