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

# Copyright(C) 2011  Romain Bignon
#
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
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# 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

20 21
from collections import OrderedDict

22 23
from weboob.capabilities.video import CapVideo, BaseVideo
from weboob.capabilities.collection import CapCollection, CollectionNotFound
24 25
from weboob.tools.backend import Module, BackendConfig
from weboob.tools.value import Value
26 27
from .browser import DailymotionBrowser

28
import re
29

Florent Fourcot's avatar
Florent Fourcot committed
30
__all__ = ['DailymotionModule']
31 32


33
class DailymotionModule(Module, CapVideo, CapCollection):
34
    NAME = 'dailymotion'
35
    MAINTAINER = u'Romain Bignon'
36
    EMAIL = 'romain@weboob.org'
Romain Bignon's avatar
Romain Bignon committed
37
    VERSION = '1.5'
38
    DESCRIPTION = 'Dailymotion video streaming website'
39
    LICENSE = 'AGPLv3+'
40 41
    BROWSER = DailymotionBrowser

42 43 44 45 46 47 48 49 50 51 52 53
    resolution_choice = OrderedDict([(k, u'%s (%s)' % (v, k)) for k, v in sorted({
        u'480': u'480p',
        u'240': u'240p',
        u'380': u'380p',
        u'720': u'720p',
        u'1080': u'1080p'
    }.iteritems())])

    format_choice = [u'm3u8', u'mp4']

    CONFIG = BackendConfig(Value('resolution', label=u'Resolution', choices=resolution_choice),
                           Value('format', label=u'Format', choices=format_choice))
54 55

    SORTBY = ['relevance', 'rated', 'visited', None]
56

57 58 59 60 61 62
    def create_default_browser(self):
        resolution = self.config['resolution'].get()
        format = self.config['format'].get()
        return self.create_browser(resolution=resolution, format=format)

    def get_video(self, _id):
63 64 65 66 67 68
        m = re.match('http://[w\.]*dailymotion\.com/video/(.*)', _id)
        if m:
            _id = m.group(1)

        if not _id.startswith('http'):
            return self.browser.get_video(_id)
69

70
    def search_videos(self, pattern, sortby=CapVideo.SEARCH_RELEVANCE, nsfw=False):
71
        return self.browser.search_videos(pattern, self.SORTBY[sortby])
72 73 74 75

    def fill_video(self, video, fields):
        if fields != ['thumbnail']:
            # if we don't want only the thumbnail, we probably want also every fields
76
            video = self.browser.get_video(video.id, video)
77
        if 'thumbnail' in fields and video.thumbnail:
78
            video.thumbnail.data = self.browser.open(video.thumbnail.url).content
79 80
        return video

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    def iter_resources(self, objs, split_path):
        if BaseVideo in objs:
            collection = self.get_collection(objs, split_path)
            if collection.path_level == 0:
                yield self.get_collection(objs, [u'latest'])
            if collection.split_path == [u'latest']:
                for video in self.browser.latest_videos():
                    yield video

    def validate_collection(self, objs, collection):
        if collection.path_level == 0:
            return
        if BaseVideo in objs and collection.split_path == [u'latest']:
            collection.title = u'Latest Dailymotion videos'
            return
        raise CollectionNotFound(collection.split_path)

98
    OBJECTS = {BaseVideo: fill_video}