module.py 3.45 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-

# Copyright(C) 2015      Vincent A
#
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 9 10 11
# 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.
#
12
# This weboob module is distributed in the hope that it will be useful,
13 14 15 16 17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# 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 20 21 22


from weboob.tools.backend import Module, BackendConfig
from weboob.tools.value import Value, ValueBackendPassword
ntome's avatar
ntome committed
23
from weboob.capabilities.collection import CapCollection, CollectionNotFound
24 25 26 27 28 29 30 31 32 33 34 35 36
from weboob.capabilities.video import CapVideo, BaseVideo

from .browser import FunmoocBrowser

__all__ = ['FunmoocModule']


class FunmoocModule(Module, CapVideo, CapCollection):
    NAME = 'funmooc'
    DESCRIPTION = u'France-Université-Numérique MOOC website'
    MAINTAINER = u'Vincent A'
    EMAIL = 'dev@indigo.re'
    LICENSE = 'AGPLv3+'
Romain Bignon's avatar
Romain Bignon committed
37
    VERSION = '1.6'
38

ntome's avatar
ntome committed
39 40
    CONFIG = BackendConfig(Value('email', label='Email'),
                           ValueBackendPassword('password', label='Password'),
41 42
                           Value('quality', label='Quality', default='HD',
                                 choices=['HD', 'SD', 'LD']))
43 44 45 46

    BROWSER = FunmoocBrowser

    def create_default_browser(self):
47
        quality = self.config['quality'].get().upper()
48 49
        return self.create_browser(self.config['email'].get(),
                                   self.config['password'].get(),
50
                                   quality=quality)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    def get_video(self, _id):
        return self.browser.get_video(_id)

    def iter_resources(self, objs, split_path):
        if len(split_path) == 0:
            return self.browser.iter_courses()
        elif len(split_path) == 1:
            return self.browser.iter_chapters(*split_path)
        elif len(split_path) == 2:
            return self.browser.iter_sections(*split_path)
        elif len(split_path) == 3:
            return self.browser.iter_videos(*split_path)

    def _matches(self, title, pattern):
        title = title.lower()
        words = pattern.lower().split()
        return all(word in title for word in words)

    def search_videos(self, pattern, sortby=0, nsfw=False):
        queue = [[]]
        while len(queue):
            path = queue.pop()
            for item in self.iter_resources(BaseVideo, path):
                if isinstance(item, BaseVideo):
                    if self._matches(item.title, pattern):
                        yield item
                else: # collection
                    newpath = item.split_path
                    if self._matches(item.title, pattern):
                        self.logger.debug('%s matches, returning content',
                                          item.title)
                        for item in self.iter_resources_flat(BaseVideo, newpath):
                            yield item
                        return
                    queue.append(newpath)
ntome's avatar
ntome committed
87 88 89 90

    def validate_collection(self, objs, collection):
        if not self.browser.check_collection(collection.split_path):
            raise CollectionNotFound()