From 47432020a366dc2f6f2f0a6782d3e3673b7ac8f1 Mon Sep 17 00:00:00 2001 From: Nicolas Duhamel Date: Tue, 9 Nov 2010 11:45:24 +0100 Subject: [PATCH] new backend CanalPlus --- weboob/backends/canalplus/__init__.py | 20 ++++++ weboob/backends/canalplus/backend.py | 55 ++++++++++++++ weboob/backends/canalplus/browser.py | 72 +++++++++++++++++++ weboob/backends/canalplus/pages/__init__.py | 23 ++++++ weboob/backends/canalplus/pages/initpage.py | 40 +++++++++++ .../canalplus/pages/searchresultpage.py | 33 +++++++++ weboob/backends/canalplus/pages/video.py | 28 ++++++++ weboob/backends/canalplus/pages/videopage.py | 42 +++++++++++ 8 files changed, 313 insertions(+) create mode 100644 weboob/backends/canalplus/__init__.py create mode 100644 weboob/backends/canalplus/backend.py create mode 100644 weboob/backends/canalplus/browser.py create mode 100644 weboob/backends/canalplus/pages/__init__.py create mode 100644 weboob/backends/canalplus/pages/initpage.py create mode 100644 weboob/backends/canalplus/pages/searchresultpage.py create mode 100644 weboob/backends/canalplus/pages/video.py create mode 100644 weboob/backends/canalplus/pages/videopage.py diff --git a/weboob/backends/canalplus/__init__.py b/weboob/backends/canalplus/__init__.py new file mode 100644 index 00000000..f0039816 --- /dev/null +++ b/weboob/backends/canalplus/__init__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Romain Bignon +# +# 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 .backend import CanalplusBackend + +__all__ = ['CanalplusBackend'] diff --git a/weboob/backends/canalplus/backend.py b/weboob/backends/canalplus/backend.py new file mode 100644 index 00000000..d97de1bf --- /dev/null +++ b/weboob/backends/canalplus/backend.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Nicolas Duhamel +# +# 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 __future__ import with_statement + +from weboob.capabilities.video import ICapVideo +from weboob.tools.backend import BaseBackend +from weboob.tools.value import ValuesDict, Value + +from .browser import CanalplusBrowser +from .pages import CanalplusVideo + +__all__ = ['CanalplusBackend'] + + +class CanalplusBackend(BaseBackend, ICapVideo): + NAME = 'canalplus' + MAINTAINER = 'Nicolas Duhamel' + EMAIL = 'nicolas@jombi.fr' + VERSION = '0.1' + DESCRIPTION = 'Canal plus french TV' + LICENSE = 'GPLv3' + CONFIG = ValuesDict(Value('quality', label='Quality of videos', choices=['hd', 'sd'], default='hd')) + BROWSER = CanalplusBrowser + + def create_default_browser(self): + return self.create_browser(quality=self.config['quality']) + + def iter_search_results(self, pattern=None, sortby=ICapVideo.SEARCH_RELEVANCE, nsfw=False, max_results=None): + with self.browser: + return self.browser.iter_search_results(pattern) + + def get_video(self, _id): + with self.browser: + return self.browser.get_video(_id) + + def fill_video(self, video, fields): + return self.browser.get_video(CanalplusVideo.id2url(video.id)) + + OBJECTS = {CanalplusVideo: fill_video} diff --git a/weboob/backends/canalplus/browser.py b/weboob/backends/canalplus/browser.py new file mode 100644 index 00000000..1d12dde2 --- /dev/null +++ b/weboob/backends/canalplus/browser.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Nicolas Duhamel +# +# 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. + +import urllib + +from weboob.tools.browser import BaseBrowser +from weboob.tools.browser.decorators import id2url + +from .pages import InitPage, SearchResultPage, CanalplusVideo, VideoPage + +import lxml.etree + +class XMLParser: + def parse(self, data, encoding=None): + if encoding is None: + parser = None + else: + parser = lxml.etree.XMLParser(encoding=encoding, strip_cdata=False) + return lxml.etree.XML(data.get_data(), parser) + + +__all__ = ['CanalplusBrowser'] + + +class CanalplusBrowser(BaseBrowser): + DOMAIN = u'service.canal-plus.com' + ENCODING = 'utf-8' + PAGES = {r"http://service.canal-plus.com/video/rest/initPlayer/cplus/": InitPage, + r"http://service.canal-plus.com/video/rest/search/cplus/.*": SearchResultPage, + r"http://service.canal-plus.com/video/rest/getVideosLiees/cplus/(?P.+)": VideoPage, + } + + #We need lxml.etree.XMLParser for read CDATA + PARSER = XMLParser() + FORMATS = { 'sd': 'BAS_DEBIT', + 'hd': 'HD' + } + + def __init__(self, quality, *args, **kwargs): + BaseBrowser.__init__(self, parser= self.PARSER, *args, **kwargs) + if quality in self.FORMATS: + self.quality = self.FORMATS[quality] + else: + self.quality = 'HD' + + def home(self): + self.location("http://service.canal-plus.com/video/rest/initPlayer/cplus/") + + def iter_search_results(self, pattern): + self.location("http://service.canal-plus.com/video/rest/search/cplus/" + urllib.quote_plus(pattern) ) + return self.page.iter_results() + + @id2url(CanalplusVideo.id2url) + def get_video(self, url, video=None): + self.location(url) + return self.page.get_video(video, self.quality) + + diff --git a/weboob/backends/canalplus/pages/__init__.py b/weboob/backends/canalplus/pages/__init__.py new file mode 100644 index 00000000..7c1ad009 --- /dev/null +++ b/weboob/backends/canalplus/pages/__init__.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Nicolas Duhamel +# +# 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 .initpage import InitPage +from .searchresultpage import SearchResultPage +from .video import CanalplusVideo +from .videopage import VideoPage + +__all__ = ['InitPage', 'SearchResultPage', 'VideoPage', 'CanalplusVideo'] diff --git a/weboob/backends/canalplus/pages/initpage.py b/weboob/backends/canalplus/pages/initpage.py new file mode 100644 index 00000000..847fb251 --- /dev/null +++ b/weboob/backends/canalplus/pages/initpage.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Nicolas Duhamel +# +# 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 + +__all__ = ['InitPage'] + +class InitPage(BasePage): + def on_loaded(self): + channels = [] + ### Parse liste des channels + for elem in self.document[2].getchildren(): + channel = {} + for e in elem.getchildren(): + subchannels = [] + if e.tag == "NOM": + channel['nom'] = e.text + elif e.tag == "SELECTIONS": + + for select in e: + subchannel = {} + subchannel['id'] = select[0].text + subchannel['nom'] = select[1].text + subchannels.append(subchannel) + channel['subchannels'] = subchannels + channels.append(channel) diff --git a/weboob/backends/canalplus/pages/searchresultpage.py b/weboob/backends/canalplus/pages/searchresultpage.py new file mode 100644 index 00000000..374ff572 --- /dev/null +++ b/weboob/backends/canalplus/pages/searchresultpage.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Nicolas Duhamel +# +# 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 .video import CanalplusVideo + + +__all__ = ['SearchResultPage'] + +class SearchResultPage(BasePage): + def iter_results(self): + for vid in self.document.getchildren(): + #id + _id = vid[0].text + #Titre + titre = vid[2][9][0].text + #Sous titre + titre = titre + " " + vid[2][9][1].text + yield CanalplusVideo(_id, title=titre) diff --git a/weboob/backends/canalplus/pages/video.py b/weboob/backends/canalplus/pages/video.py new file mode 100644 index 00000000..4f54fce2 --- /dev/null +++ b/weboob/backends/canalplus/pages/video.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Nicolas Duhamel +# +# 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.capabilities.video import BaseVideo + + +__all__ = ['CanalplusVideo'] + + +class CanalplusVideo(BaseVideo): + @classmethod + def id2url(cls, _id): + return 'http://service.canal-plus.com/video/rest/getVideosLiees/cplus/%s' % _id diff --git a/weboob/backends/canalplus/pages/videopage.py b/weboob/backends/canalplus/pages/videopage.py new file mode 100644 index 00000000..d45cec35 --- /dev/null +++ b/weboob/backends/canalplus/pages/videopage.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Nicolas Duhamel +# +# 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 .video import CanalplusVideo +__all__ = ['VideoPage'] + +class VideoPage(BasePage): + def on_loaded(self): + pass + + def get_video(self, video, quality): + if not video: + video = CanalplusVideo(self.group_dict['id']) + print quality + print video.id + for vid in self.document.getchildren(): + url = None + lastest = None + for format in vid[5][1].getchildren(): + if format.tag == quality: + url = format.text + if format.text: + lastest = format + if url == None: + url = lastest.text + video.url = url + return video