framalibre.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. FramaLibre (It)
  3. @website https://framalibre.org/
  4. @provide-api no
  5. @using-api no
  6. @results HTML
  7. @stable no (HTML can change)
  8. @parse url, title, content, thumbnail, img_src
  9. """
  10. try:
  11. from cgi import escape
  12. except:
  13. from html import escape
  14. from lxml import html
  15. from searx.engines.xpath import extract_text
  16. from searx.url_utils import urljoin, urlencode
  17. # engine dependent config
  18. categories = ['it']
  19. paging = True
  20. # search-url
  21. base_url = 'https://framalibre.org/'
  22. search_url = base_url + 'recherche-par-crit-res?{query}&page={offset}'
  23. # specific xpath variables
  24. results_xpath = '//div[@class="nodes-list-row"]/div[contains(@typeof,"sioc:Item")]'
  25. link_xpath = './/h3[@class="node-title"]/a[@href]'
  26. thumbnail_xpath = './/img[@class="media-object img-responsive"]/@src'
  27. content_xpath = './/div[@class="content"]//p'
  28. # do search-request
  29. def request(query, params):
  30. offset = (params['pageno'] - 1)
  31. params['url'] = search_url.format(query=urlencode({'keys': query}),
  32. 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,
  53. 'title': title,
  54. 'img_src': thumbnail,
  55. 'content': content})
  56. # return results
  57. return results