Skip to content
pages.py 5.51 KiB
Newer Older
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
# -*- coding: utf-8 -*-

# Copyright(C) 2015 Guilhem Bonnefille
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
#
Roger Philibert's avatar
Roger Philibert committed
# This file is part of a woob module.
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
#
Roger Philibert's avatar
Roger Philibert committed
# This woob module is free software: you can redistribute it and/or modify
Guilhem Bonnefille's avatar
Guilhem Bonnefille 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.
#
Roger Philibert's avatar
Roger Philibert committed
# This woob module is distributed in the hope that it will be useful,
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
# 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
Roger Philibert's avatar
Roger Philibert committed
# along with this woob module. If not, see <http://www.gnu.org/licenses/>.
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed

import re
from datetime import timedelta
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed

from woob.browser.elements import ItemElement, ListElement, method
from woob.browser.filters.html import CleanHTML, Link, XPath
from woob.browser.filters.json import Dict
from woob.browser.filters.standard import CleanText, DateTime, Duration, Filter, Format, Regexp
from woob.browser.pages import HTMLPage, JsonPage
from woob.capabilities import NotLoaded
from woob.capabilities.collection import Collection
from woob.capabilities.image import Thumbnail
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed

from .video import RmllVideo

BASE_URL = 'http://video.rmll.info'

Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
class NormalizeThumbnail(Filter):
    def filter(self, thumbnail):
        if not thumbnail.startswith('http'):
            thumbnail = BASE_URL + thumbnail
        if thumbnail == "http://rmll.ubicast.tv/statics/mediaserver/images/video_icon.png":
            # This is the default: remove it as any frontend default should be better
            thumbnail = None
        return thumbnail


class RmllDuration(Duration):
    _regexp = re.compile(r'((?P<hh>\d+) h )?((?P<mm>\d+) m )?(?P<ss>\d+) s')
    kwargs = {'hours': 'hh', 'minutes': 'mm', 'seconds': 'ss'}


def create_video(metadata):
    video = RmllVideo(metadata['oid'])

    video.title = str(metadata['title'])
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
    video.date = DateTime(Dict('creation'), default=NotLoaded)(metadata)
    video.duration = RmllDuration(Dict('duration', default=''), default=NotLoaded)(metadata)
    thumbnail = NormalizeThumbnail(Dict('thumb'))(metadata)
    video.thumbnail = Thumbnail(thumbnail)
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
    video.thumbnail.url = video.thumbnail.id
    video.url = NotLoaded

    return video

Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
class RmllVideoPage(HTMLPage):
    @method
    class get_video(ItemElement):
        klass = RmllVideo

        obj_id = CleanHTML('/html/head/meta[@property="og:url"]/@content') & CleanText() & Regexp(pattern=r'.*/permalink/(.+)/$')
        obj_title = Format(u'%s', CleanHTML('/html/head/meta[@name="DC.title"]/@content') & CleanText())
        obj_description = Format(u'%s', CleanHTML('/html/head/meta[@property="og:description"]/@content') & CleanText())

        def obj_thumbnail(self):
            url = NormalizeThumbnail(CleanText('/html/head/meta[@property="og:image"]/@content'))(self)
            if url:
                thumbnail = Thumbnail(url)
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
                thumbnail.url = thumbnail.id
                return thumbnail

        def obj_url(self):
            links = XPath('//div[@id="download_links"]/div[@class="paragraph"]/div[has-class("share")]/a[@target="_blank"]/@href')(self)
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
            for link in links:
                ext = str(link).split('.')[-1]
                self.logger.debug("Link:%s Ext:%s", link, ext)
                if ext in ['mp4', 'webm']:
                    return self.page.browser.BASEURL + link


class RmllDurationPage(JsonPage):
    def get_duration(self):
        return timedelta(seconds=Dict('duration')(self.doc))

Guilhem Bonnefille's avatar
Guilhem Bonnefille committed

class RmllCollectionPage(HTMLPage):

    @method
    class iter_videos(ListElement):
        item_xpath = '//div[@class="item-entry type-video " or @class="item-entry type-vod "]'

        class item(ItemElement):
            klass = RmllVideo

            obj_id = Link('a') & Regexp(pattern=r'.*/videos/(.+)/$')
            obj_title = Format(u'%s', CleanHTML('a/span/span/span[@class="item-entry-title"]') & CleanText())
            obj_url = NotLoaded
            # obj_date = XPath('a/span/span/span[@class="item-entry-creation"]')
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed

            obj_duration = CleanText('a/span/span/span[@class="item-entry-duration"]') & RmllDuration()

            def obj_thumbnail(self):
                thumbnail = NormalizeThumbnail(CleanText('a/span[@class="item-entry-preview"]/img/@src'))(self)
                if thumbnail:
                    thumbnail = Thumbnail(thumbnail)
Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
                    thumbnail.url = thumbnail.id
                    return thumbnail

Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
class RmllChannelsPage(JsonPage):
    def iter_resources(self, split_path):
        if 'channels' in self.doc:
            for metadata in self.doc['channels']:
                collection = Collection(split_path+[metadata['oid']], metadata['title'])
                yield collection

        if 'videos' in self.doc:
            for metadata in self.doc['videos']:
                video = create_video(metadata)
                yield video

Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
class RmllLatestPage(JsonPage):
    def iter_resources(self):
        for metadata in self.doc['items']:
            if metadata['type'] == 'c':
                collection = Collection([metadata['oid']], metadata['title'])
                yield collection

            if metadata['type'] == 'v':
                video = create_video(metadata)
                yield video

Guilhem Bonnefille's avatar
Guilhem Bonnefille committed
class RmllSearchPage(JsonPage):
    def iter_resources(self):
        if 'videos' in self.doc:
            for metadata in self.doc['videos']:
                video = create_video(metadata)
                yield video