.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
imgur.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Imgur (images)
3"""
4
5from urllib.parse import urlencode
6from lxml import html
7from searx.utils import extract_text, eval_xpath, eval_xpath_list
8
9about = {
10 "website": 'https://imgur.com/',
11 "wikidata_id": 'Q355022',
12 "official_api_documentation": 'https://api.imgur.com/',
13 "use_official_api": False,
14 "require_api_key": False,
15 "results": 'HTML',
16}
17
18categories = ['images']
19paging = True
20time_range_support = True
21
22base_url = "https://imgur.com"
23
24results_xpath = "//div[contains(@class, 'cards')]/div[contains(@class, 'post')]"
25url_xpath = "./a/@href"
26title_xpath = "./a/img/@alt"
27thumbnail_xpath = "./a/img/@src"
28
29
30def request(query, params):
31 time_range = params['time_range'] or 'all'
32 args = {
33 'q': query,
34 'qs': 'thumbs',
35 'p': params['pageno'] - 1,
36 }
37 params['url'] = f"{base_url}/search/score/{time_range}?{urlencode(args)}"
38 return params
39
40
41def response(resp):
42 results = []
43
44 dom = html.fromstring(resp.text)
45
46 for result in eval_xpath_list(dom, results_xpath):
47 thumbnail_src = extract_text(eval_xpath(result, thumbnail_xpath))
48 img_src = thumbnail_src.replace("b.", ".")
49
50 # that's a bug at imgur's side:
51 # sometimes there's just no preview image, hence we skip the image
52 if len(thumbnail_src) < 25:
53 continue
54
55 results.append(
56 {
57 'template': 'images.html',
58 'url': base_url + extract_text(eval_xpath(result, url_xpath)),
59 'title': extract_text(eval_xpath(result, title_xpath)),
60 'img_src': img_src,
61 'thumbnail_src': thumbnail_src,
62 }
63 )
64
65 return results
request(query, params)
Definition imgur.py:30