imgur.py 1.7 KB

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