move genericArticle to tools

This commit is contained in:
Juke 2011-02-21 23:31:38 +01:00
commit 5a7ae0a5bd
10 changed files with 24 additions and 206 deletions

View file

@ -17,13 +17,11 @@
from .pages.article import ArticlePage
from weboob.tools.browser import BaseBrowser
from .tools import id2url
class NewspaperInrocksBrowser(BaseBrowser):
PAGES = {
'http://www.lesinrocks.com/actualite/actu-article/t/60121/date/2011-02-15/article/accuse-davoir-participe-a-une-mutinerie-un-detenu-porte-plainte/': ArticlePage,
'.*': ArticlePage,
}

View file

@ -16,22 +16,16 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.parsers.lxmlparser import select, SelectElementException
from .genericArticle import GenericNewsPage
def try_remove(base_element, selector):
try :
base_element.remove(select(base_element, selector, 1 ))
except (SelectElementException, ValueError):
pass
from weboob.tools.parsers.lxmlparser import select
from weboob.tools.genericArticle import GenericNewsPage, try_remove
class ArticlePage(GenericNewsPage):
"ArticlePage object for inrocks"
def on_loaded(self):
self.main_div = self.document.getroot()
self.element_author_selector = "div.name>span"
self.element_body_selector = "div.maincol"
self.element_title_selector = "h1"
self.element_body_selector = "div.maincol"
def get_body(self):
element_body = self.get_element_body()

View file

@ -16,31 +16,25 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.parsers.lxmlparser import select, SelectElementException
from .genericArticle import GenericNewsPage
def try_remove(base_element, selector):
try :
base_element.remove(select(base_element, selector, 1 ))
except (SelectElementException, ValueError):
pass
from weboob.tools.parsers.lxmlparser import select
from weboob.tools.genericArticle import GenericNewsPage, try_remove
class ArticlePage(GenericNewsPage):
"ArticlePage object for inrocks"
def on_loaded(self):
self.main_div = self.document.getroot()
self.element_author_selector = "div.name>span"
self.element_body_selector = "#article"
self.element_title_selector = "h1"
self.element_body_selector = "#article"
def get_body(self):
element_body = self.get_element_body()
h1 = select(element_body, self.element_title_selector, 1)
div_infos = select(element_body, "div.infos", 1)
toolsbar = select(element_body, "#toolsbar", 1)
el_script = select(element_body, "script", 1)
element_body = self.get_element_body()
h1_title = select(element_body, self.element_title_selector, 1)
div_infos = select(element_body, "div.infos", 1)
toolsbar = select(element_body, "#toolsbar", 1)
el_script = select(element_body, "script", 1)
element_body.remove(h1)
element_body.remove(h1_title)
element_body.remove(div_infos)
element_body.remove(toolsbar)

View file

@ -16,14 +16,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.parsers.lxmlparser import select, SelectElementException
from .genericArticle import GenericNewsPage
def try_remove(base_element, selector):
try :
base_element.remove(select(base_element, selector, 1 ))
except (SelectElementException, ValueError):
pass
from weboob.tools.genericArticle import GenericNewsPage
class FlashActuPage(GenericNewsPage):
"ArticlePage object for inrocks"

View file

@ -1,81 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Julien Hebert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.browser import BasePage
from weboob.tools.parsers.lxmlparser import select, SelectElementException
class NoAuthorElement(SelectElementException):
pass
class NoneMainDiv(AttributeError):
pass
class Article(object):
author = u''
def __init__(self, browser, _id):
self.browser = browser
self.id = _id
self.title = u''
self.body = u''
self.url = u''
self.date = None
class GenericNewsPage(BasePage):
__element_body = NotImplementedError
__article = Article
element_title_selector = NotImplementedError
main_div = NotImplementedError
element_body_selector = NotImplementedError
element_author_selector = NotImplementedError
def get_body(self):
return self.browser.parser.tostring(self.get_element_body())
def get_author(self):
try:
return self.get_element_author().text_content().strip()
except NoAuthorElement:
return self.__article.author
def get_title(self):
return select(
self.main_div,
self.element_title_selector,
1).text_content().strip()
def get_element_body(self):
return select(self.main_div, self.element_body_selector, 1)
def get_element_author(self):
try:
return select(self.main_div, self.element_author_selector, 1)
except SelectElementException:
raise NoAuthorElement()
except AttributeError:
if self.main_div == None:
raise NoneMainDiv("main_div is none on %s" % (self.browser))
else:
raise
def get_article(self, _id):
__article = Article(self.browser, _id)
__article.author = self.get_author()
__article.title = self.get_title()
__article.url = self.url
__article.body = self.get_body()
return __article

View file

@ -16,7 +16,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from .genericArticle import GenericNewsPage
from weboob.tools.genericArticle import GenericNewsPage
class SimplePage(GenericNewsPage):
"ArticlePage object for minutes20"

View file

@ -16,16 +16,9 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.parsers.lxmlparser import select, SelectElementException
from .genericArticle import NoAuthorElement
from weboob.tools.genericArticle import NoAuthorElement, try_remove
from .simple import SimplePage
def try_remove(base_element, selector):
try :
base_element.remove(select(base_element, selector, 1 ))
except (SelectElementException, ValueError):
pass
class ArticlePage(SimplePage):
"ArticlePage object for minutes20"

View file

@ -1,81 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Julien Hebert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.browser import BasePage
from weboob.tools.parsers.lxmlparser import select, SelectElementException
class NoAuthorElement(SelectElementException):
pass
class NoneMainDiv(AttributeError):
pass
class Article(object):
author = u''
def __init__(self, browser, _id):
self.browser = browser
self.id = _id
self.title = u''
self.body = u''
self.url = u''
self.date = None
class GenericNewsPage(BasePage):
__element_body = NotImplementedError
__article = Article
element_title_selector = NotImplementedError
main_div = NotImplementedError
element_body_selector = NotImplementedError
element_author_selector = NotImplementedError
def get_body(self):
return self.browser.parser.tostring(self.get_element_body())
def get_author(self):
try:
return self.get_element_author().text_content().strip()
except NoAuthorElement:
return self.__article.author
def get_title(self):
return select(
self.main_div,
self.element_title_selector,
1).text_content().strip()
def get_element_body(self):
return select(self.main_div, self.element_body_selector, 1)
def get_element_author(self):
try:
return select(self.main_div, self.element_author_selector, 1)
except SelectElementException:
raise NoAuthorElement()
except AttributeError:
if self.main_div == None:
raise NoneMainDiv("main_div is none on %s" % (self.browser))
else:
raise
def get_article(self, _id):
__article = Article(self.browser, _id)
__article.author = self.get_author()
__article.title = self.get_title()
__article.url = self.url
__article.body = self.get_body()
return __article

View file

@ -16,7 +16,8 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from .genericArticle import GenericNewsPage
from weboob.tools.genericArticle import GenericNewsPage
class SimplePage(GenericNewsPage):
"ArticlePage object for minutes20"

View file

@ -17,6 +17,13 @@
from weboob.tools.browser import BasePage
from weboob.tools.parsers.lxmlparser import select, SelectElementException
def try_remove(base_element, selector):
try :
base_element.remove(select(base_element, selector, 1 ))
except (SelectElementException, ValueError):
pass
class NoAuthorElement(SelectElementException):
pass