framalibre.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. FramaLibre (It)
  4. """
  5. from html import escape
  6. from urllib.parse import urljoin, urlencode
  7. from lxml import html
  8. from searx.utils import extract_text
  9. # about
  10. about = {
  11. "website": 'https://framalibre.org/',
  12. "wikidata_id": 'Q30213882',
  13. "official_api_documentation": None,
  14. "use_official_api": False,
  15. "require_api_key": False,
  16. "results": 'HTML',
  17. }
  18. # engine dependent config
  19. categories = ['it']
  20. paging = True
  21. # search-url
  22. base_url = 'https://framalibre.org/'
  23. search_url = base_url + 'recherche-par-crit-res?{query}&page={offset}'
  24. # specific xpath variables
  25. results_xpath = '//div[@class="nodes-list-row"]/div[contains(@typeof,"sioc:Item")]'
  26. link_xpath = './/h3[@class="node-title"]/a[@href]'
  27. thumbnail_xpath = './/img[@class="media-object img-responsive"]/@src'
  28. content_xpath = './/div[@class="content"]//p'
  29. # do search-request
  30. def request(query, params):
  31. offset = params['pageno'] - 1
  32. params['url'] = search_url.format(query=urlencode({'keys': query}), offset=offset)
  33. return params
  34. # get response from search-request
  35. def response(resp):
  36. results = []
  37. dom = html.fromstring(resp.text)
  38. # parse results
  39. for result in dom.xpath(results_xpath):
  40. link = result.xpath(link_xpath)[0]
  41. href = urljoin(base_url, link.attrib.get('href'))
  42. # there's also a span (class="rdf-meta element-hidden" property="dc:title")'s content property for this...
  43. title = escape(extract_text(link))
  44. thumbnail_tags = result.xpath(thumbnail_xpath)
  45. thumbnail = None
  46. if len(thumbnail_tags) > 0:
  47. thumbnail = extract_text(thumbnail_tags[0])
  48. if thumbnail[0] == '/':
  49. thumbnail = base_url + thumbnail
  50. content = escape(extract_text(result.xpath(content_xpath)))
  51. # append result
  52. results.append({'url': href, 'title': title, 'img_src': thumbnail, 'content': content})
  53. # return results
  54. return results