xpath.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. from lxml import html
  3. from urllib.parse import urlencode
  4. from searx.utils import extract_text, extract_url, eval_xpath, eval_xpath_list
  5. search_url = None
  6. url_xpath = None
  7. content_xpath = None
  8. title_xpath = None
  9. thumbnail_xpath = False
  10. paging = False
  11. suggestion_xpath = ''
  12. results_xpath = ''
  13. cached_xpath = ''
  14. cached_url = ''
  15. # parameters for engines with paging support
  16. #
  17. # number of results on each page
  18. # (only needed if the site requires not a page number, but an offset)
  19. page_size = 1
  20. # number of the first page (usually 0 or 1)
  21. first_page_num = 1
  22. def request(query, params):
  23. query = urlencode({'q': query})[2:]
  24. fp = {'query': query}
  25. if paging and search_url.find('{pageno}') >= 0:
  26. fp['pageno'] = (params['pageno'] - 1) * page_size + first_page_num
  27. params['url'] = search_url.format(**fp)
  28. params['query'] = query
  29. return params
  30. def response(resp):
  31. results = []
  32. dom = html.fromstring(resp.text)
  33. is_onion = True if 'onions' in categories else False # pylint: disable=undefined-variable
  34. if results_xpath:
  35. for result in eval_xpath_list(dom, results_xpath):
  36. url = extract_url(eval_xpath_list(result, url_xpath, min_len=1), search_url)
  37. title = extract_text(eval_xpath_list(result, title_xpath, min_len=1))
  38. content = extract_text(eval_xpath_list(result, content_xpath, min_len=1))
  39. tmp_result = {'url': url, 'title': title, 'content': content}
  40. # add thumbnail if available
  41. if thumbnail_xpath:
  42. thumbnail_xpath_result = eval_xpath_list(result, thumbnail_xpath)
  43. if len(thumbnail_xpath_result) > 0:
  44. tmp_result['img_src'] = extract_url(thumbnail_xpath_result, search_url)
  45. # add alternative cached url if available
  46. if cached_xpath:
  47. tmp_result['cached_url'] = cached_url\
  48. + extract_text(eval_xpath_list(result, cached_xpath, min_len=1))
  49. if is_onion:
  50. tmp_result['is_onion'] = True
  51. results.append(tmp_result)
  52. else:
  53. if cached_xpath:
  54. for url, title, content, cached in zip(
  55. (extract_url(x, search_url) for
  56. x in eval_xpath_list(dom, url_xpath)),
  57. map(extract_text, eval_xpath_list(dom, title_xpath)),
  58. map(extract_text, eval_xpath_list(dom, content_xpath)),
  59. map(extract_text, eval_xpath_list(dom, cached_xpath))
  60. ):
  61. results.append({'url': url, 'title': title, 'content': content,
  62. 'cached_url': cached_url + cached, 'is_onion': is_onion})
  63. else:
  64. for url, title, content in zip(
  65. (extract_url(x, search_url) for
  66. x in eval_xpath_list(dom, url_xpath)),
  67. map(extract_text, eval_xpath_list(dom, title_xpath)),
  68. map(extract_text, eval_xpath_list(dom, content_xpath))
  69. ):
  70. results.append({'url': url, 'title': title, 'content': content, 'is_onion': is_onion})
  71. if not suggestion_xpath:
  72. return results
  73. for suggestion in eval_xpath(dom, suggestion_xpath):
  74. results.append({'suggestion': extract_text(suggestion)})
  75. return results