ipernity.py 2.1 KB

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