/modules/inrocks/pages.py

https://github.com/laurentb/weboob · Python · 65 lines · 38 code · 10 blank · 17 comment · 9 complexity · 270b790d3091f5591056b67461f2493b MD5 · raw file

  1. "ArticlePage object for inrocks"
  2. # -*- coding: utf-8 -*-
  3. # Copyright(C) 2011 Julien Hebert
  4. #
  5. # This file is part of a weboob module.
  6. #
  7. # This weboob module is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This weboob module is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
  19. from weboob.browser.pages import AbstractPage
  20. from weboob.browser.filters.html import XPathNotFound, CSS, CleanHTML
  21. class ArticlePage(AbstractPage):
  22. "ArticlePage object for inrocks"
  23. _selector = CSS
  24. PARENT = 'genericnewspaper'
  25. PARENT_URL = 'generic_news_page'
  26. def on_loaded(self):
  27. main = self._selector("div#block-article")(self.doc.getroot())
  28. self.main_div = main[0] if len(main) else None
  29. self.element_title_selector = "div.header>h1"
  30. self.element_author_selector = "div.name"
  31. self.element_body_selector = "div.maincol"
  32. def get_title(self):
  33. try:
  34. return super(self.__class__, self).get_title()
  35. except(XPathNotFound):
  36. if self.main_div is None:
  37. return u""
  38. else:
  39. raise
  40. def get_body(self):
  41. try:
  42. element_body = self.get_element_body()
  43. except XPathNotFound:
  44. return u'Ceci est un article payant'
  45. else:
  46. self.drop_comments(element_body)
  47. div_header_element = self._selector("div.header")(element_body)[0]
  48. div_content_element = self._selector("div#the-content")(element_body)[0]
  49. self.try_remove_from_selector_list(div_header_element,
  50. ["h1", "div.picture", "div.date",
  51. "div.news-single-img", "div.article-top",
  52. "div.metas_img", "strong"])
  53. self.try_remove_from_selector_list(div_content_element, ["div.tw_button", "div.wpfblike",
  54. "blockquote", "p.wp-caption-text", "img"])
  55. return '%s\n%s' % (CleanHTML('.')(div_header_element), CleanHTML('.')(div_content_element))