framalibre.py 2.1 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. from urlparse import urljoin
  11. from cgi import escape
  12. from urllib import urlencode
  13. from lxml import html
  14. from searx.engines.xpath import extract_text
  15. from dateutil import parser
  16. # engine dependent config
  17. categories = ['it']
  18. paging = True
  19. # search-url
  20. base_url = 'https://framalibre.org/'
  21. search_url = base_url + 'recherche-par-crit-res?{query}&page={offset}'
  22. # specific xpath variables
  23. results_xpath = '//div[@class="nodes-list-row"]/div[contains(@typeof,"sioc:Item")]'
  24. link_xpath = './/h3[@class="node-title"]/a[@href]'
  25. thumbnail_xpath = './/img[@class="media-object img-responsive"]/@src'
  26. content_xpath = './/div[@class="content"]//p'
  27. # do search-request
  28. def request(query, params):
  29. offset = (params['pageno'] - 1)
  30. params['url'] = search_url.format(query=urlencode({'keys': query}),
  31. offset=offset)
  32. return params
  33. # get response from search-request
  34. def response(resp):
  35. results = []
  36. dom = html.fromstring(resp.text)
  37. # parse results
  38. for result in dom.xpath(results_xpath):
  39. link = result.xpath(link_xpath)[0]
  40. href = urljoin(base_url, link.attrib.get('href'))
  41. # there's also a span (class="rdf-meta element-hidden" property="dc:title")'s content property for this...
  42. title = escape(extract_text(link))
  43. thumbnail_tags = result.xpath(thumbnail_xpath)
  44. thumbnail = None
  45. if len(thumbnail_tags) > 0:
  46. thumbnail = extract_text(thumbnail_tags[0])
  47. if thumbnail[0] == '/':
  48. thumbnail = base_url + thumbnail
  49. content = escape(extract_text(result.xpath(content_xpath)))
  50. # append result
  51. results.append({'url': href,
  52. 'title': title,
  53. 'thumbnail': thumbnail,
  54. 'img_src': thumbnail,
  55. 'content': content})
  56. # return results
  57. return results