Skip to content
module.py 5.58 KiB
Newer Older
Romain Bignon's avatar
Romain Bignon committed
# -*- coding: utf-8 -*-

Romain Bignon's avatar
Romain Bignon committed
# Copyright(C) 2010-2011 Romain Bignon
Romain Bignon's avatar
Romain Bignon committed
#
# This file is part of a weboob module.
Romain Bignon's avatar
Romain Bignon committed
#
# This weboob module is free software: you can redistribute it and/or modify
Romain Bignon's avatar
Romain Bignon committed
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This weboob module is distributed in the hope that it will be useful,
Romain Bignon's avatar
Romain Bignon committed
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Romain Bignon's avatar
Romain Bignon committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
Romain Bignon's avatar
Romain Bignon committed
#
Romain Bignon's avatar
Romain Bignon committed
# You should have received a copy of the GNU Affero General Public License
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
import re
from collections import OrderedDict

from weboob.capabilities.video import CapVideo, BaseVideo
from weboob.capabilities.collection import CapCollection, CollectionNotFound, Collection
from weboob.tools.backend import Module, BackendConfig
from weboob.tools.value import Value
Romain Bignon's avatar
Romain Bignon committed

from .browser import ArteBrowser
from .video import VERSION_VIDEO, FORMATS, LANG, QUALITY, SITE, get_site_enum_by_id
Romain Bignon's avatar
Romain Bignon committed

Florent's avatar
Florent committed
__all__ = ['ArteModule']
Romain Bignon's avatar
Romain Bignon committed

class ArteModule(Module, CapVideo, CapCollection):
Romain Bignon's avatar
Romain Bignon committed
    NAME = 'arte'
Florent's avatar
Florent committed
    MAINTAINER = u'Bezleputh'
    EMAIL = 'carton_ben@yahoo.fr'
Romain Bignon's avatar
Romain Bignon committed
    VERSION = '1.6'
Laurent Bachelier's avatar
Laurent Bachelier committed
    DESCRIPTION = 'Arte French and German TV'
Romain Bignon's avatar
Romain Bignon committed
    LICENSE = 'AGPLv3+'

    order = {'AIRDATE_DESC': 'Date',
             'VIEWS': 'Views',
             'ALPHA': 'Alphabetic',
             'LAST_CHANCE': 'Last chance'
             }

Bezleputh's avatar
Bezleputh committed
    versions_choice = OrderedDict([(k, u'%s' % (v.get('label'))) for k, v in VERSION_VIDEO.items])
    format_choice = OrderedDict([(k, u'%s' % (v)) for k, v in FORMATS.items])
    lang_choice = OrderedDict([(k, u'%s' % (v.get('label'))) for k, v in LANG.items])
    quality_choice = [u'%s' % (k) for k, v in QUALITY.items]
    CONFIG = BackendConfig(Value('lang', label='Lang of videos', choices=lang_choice, default='FRENCH'),
Bezleputh's avatar
Bezleputh committed
                           Value('order', label='Sort order', choices=order, default='AIRDATE_DESC'),
                           Value('quality', label='Quality of videos', choices=quality_choice, default='HD'),
Bezleputh's avatar
Bezleputh committed
                           Value('format', label='Format of videos', choices=format_choice, default=FORMATS.HTTP_MP4),
                           Value('version', label='Version of videos', choices=versions_choice))
Romain Bignon's avatar
Romain Bignon committed
    BROWSER = ArteBrowser

Romain Bignon's avatar
Romain Bignon committed
    def create_default_browser(self):
Bezleputh's avatar
Bezleputh committed
        return self.create_browser(lang=self.config['lang'].get(),
                                   quality=self.config['quality'].get(),
                                   order=self.config['order'].get(),
                                   format=self.config['format'].get(),
                                   version=self.config['version'].get())
    def parse_id(self, _id):
        m = re.match('https?://www.arte.tv/\w+/videos/(?P<id>.+)/(.*)', _id)
            return m.group(1)
Romain Bignon's avatar
Romain Bignon committed

Romain Bignon's avatar
Romain Bignon committed

    def get_video(self, _id, video=None):
        _id = self.parse_id(_id)
Romain Bignon's avatar
Romain Bignon committed

        if _id is None:
        return self.browser.get_video(_id, video)
Romain Bignon's avatar
Romain Bignon committed

    def search_videos(self, pattern, sortby=CapVideo.SEARCH_RELEVANCE, nsfw=False):
Bezleputh's avatar
Bezleputh committed
        return self.browser.search_videos(pattern)
Romain Bignon's avatar
Romain Bignon committed

Bezleputh's avatar
Bezleputh committed
    def fill_arte_video(self, video, fields):
Romain Bignon's avatar
Romain Bignon committed
        if fields != ['thumbnail']:
Bezleputh's avatar
Bezleputh committed
            video = self.browser.get_video(video.id, video)

        if 'thumbnail' in fields and video and video.thumbnail:
            video.thumbnail.data = self.browser.open(video.thumbnail.url).content
Romain Bignon's avatar
Romain Bignon committed
        return video

    def iter_resources(self, objs, split_path):
        collection = self.get_collection(objs, split_path)

Bezleputh's avatar
Bezleputh committed
        if BaseVideo in objs:
            if collection.path_level == 0:
                return [Collection([site.get('id')], site.get('label')) for site in SITE.values]

            elif len(split_path) == 1:
                site = get_site_enum_by_id(split_path[0])
                subsite = site.get('enum', None)

                if site == SITE.PROGRAM:
                    return self.browser.get_arte_programs(split_path)
                elif site == SITE.GUIDE:
                    return self.browser.get_arte_guide_days(split_path)
                elif site == SITE.CREATIVE:
                    return self.browser.get_arte_creative_videos()
                elif subsite:
                    return self.browser.get_arte_generic_subsites(split_path, subsite)

            elif collection.path_level > 1:
                site = get_site_enum_by_id(split_path[0])

                if site == SITE.GUIDE:
                    return self.browser.get_arte_guide_videos(split_path)
Bezleputh's avatar
Bezleputh committed
                else:
                    subsite = site.get('enum', {})
                    if subsite:
                        subsite = dict(subsite.items)
                    return self.browser.get_arte_navigation(split_path,
                                                            subsite.get('COLLECTION', {}).get('id', r''),
                                                            subsite.get('PLAYLIST', {}).get('id', r''),
                                                            subsite.get('MAGAZINE', {}).get('id', r''))

    def validate_collection(self, objs, collection):
        if collection.path_level == 0:
            return
        if BaseVideo in objs and (collection.split_path[0] in [value.get('id') for value in SITE.values]):
            return
        raise CollectionNotFound(collection.split_path)

    OBJECTS = {BaseVideo: fill_arte_video}