Skip to content
gallery.py 4.24 KiB
Newer Older
Noé Rubinstein's avatar
Noé Rubinstein committed
# -*- coding: utf-8 -*-

# Copyright(C) 2010-2011 Romain Bignon, Christophe Benz, Noé Rubinstein
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
Noé Rubinstein's avatar
Noé Rubinstein committed
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob 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 Lesser General Public License for more details.
Noé Rubinstein's avatar
Noé Rubinstein committed
#
# You should have received a copy of the GNU Lesser General Public License
Noé Rubinstein's avatar
Noé Rubinstein committed
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from weboob.capabilities.image import BaseImage as CIBaseImage, Thumbnail
from weboob.tools.compat import unicode
Florent's avatar
Florent committed
from .base import Capability, BaseObject, NotLoaded, Field, StringField, \
                  IntField, FloatField, Enum
from .date import DateField

Noé Rubinstein's avatar
Noé Rubinstein committed

__all__ = ['BaseGallery', 'BaseImage', 'CapGallery']
class BaseGallery(BaseObject):
Noé Rubinstein's avatar
Noé Rubinstein committed
    """
    Represents a gallery.
Noé Rubinstein's avatar
Noé Rubinstein committed
    This object has to be inherited to specify how to calculate the URL of the gallery from its ID.
    """
    title =         StringField('Title of gallery')
    description =   StringField('Description of gallery')
    cardinality =   IntField('Cardinality of gallery')
    date =          DateField('Date of gallery')
    rating =        FloatField('Rating of this gallery')
    rating_max =    FloatField('Max rating available')
    thumbnail =     Field('Thumbnail', Thumbnail)

Noé Rubinstein's avatar
Noé Rubinstein committed
    def __init__(self, _id, title=NotLoaded, url=NotLoaded, cardinality=NotLoaded, date=NotLoaded,
                 rating=NotLoaded, rating_max=NotLoaded, thumbnail=NotLoaded, thumbnail_url=None, nsfw=False):
        super(BaseGallery, self).__init__(unicode(_id), url)
Noé Rubinstein's avatar
Noé Rubinstein committed

        self.title = title
        self.date = date
        self.rating = rating
        self.rating_max = rating_max
        self.thumbnail = thumbnail
Noé Rubinstein's avatar
Noé Rubinstein committed

    @classmethod
    def id2url(cls, _id):
        """Overloaded in child classes provided by backends."""
        raise NotImplementedError()

    @property
    def page_url(self):
Noé Rubinstein's avatar
Noé Rubinstein committed
        return self.id2url(self.id)

    def iter_image(self):
Noé Rubinstein's avatar
Noé Rubinstein committed
        raise NotImplementedError()

class BaseImage(CIBaseImage):
    """
    Base class for images.
    """
    index =     IntField('Usually page number')
    gallery =   Field('Reference to the Gallery object', BaseGallery)

    def __init__(self, _id=u'', index=None, thumbnail=NotLoaded, url=NotLoaded,
            ext=NotLoaded, gallery=None):

        super(BaseImage, self).__init__(unicode(_id), url)
        self.index = index
        self.thumbnail = thumbnail
        self.ext = ext
        self.gallery = gallery
    def __unicode__(self):
Roger Philibert's avatar
Roger Philibert committed
        return self.url

    def __repr__(self):
        return '<Image url="%s">' % self.url

    def __iscomplete__(self):
        return self.data is not NotLoaded
Noé Rubinstein's avatar
Noé Rubinstein committed

Florent's avatar
Florent committed
class CapGallery(Capability):
Noé Rubinstein's avatar
Noé Rubinstein committed
    """
    This capability represents the ability for a website backend to provide videos.
    """
    SEARCH_RELEVANCE = SearchSort.RELEVANCE
    SEARCH_RATING = SearchSort.RATING
    SEARCH_VIEWS = SearchSort.VIEWS
    SEARCH_DATE = SearchSort.DATE
Noé Rubinstein's avatar
Noé Rubinstein committed

    def search_galleries(self, pattern, sortby=SEARCH_RELEVANCE):
Noé Rubinstein's avatar
Noé Rubinstein committed
        """
        Iter results of a search on a pattern.
Noé Rubinstein's avatar
Noé Rubinstein committed

        :param pattern: pattern to search on
        :type pattern: str
        :param sortby: sort by...
        :type sortby: SEARCH_*
        :rtype: :class:`BaseGallery`
Noé Rubinstein's avatar
Noé Rubinstein committed
        """
        raise NotImplementedError()

    def get_gallery(self, _id):
        """
        Get gallery from an ID.

        :param _id: the gallery id. It can be a numeric ID, or a page url, or so.
        :type _id: str
        :rtype: :class:`Gallery`
Noé Rubinstein's avatar
Noé Rubinstein committed
        """
        raise NotImplementedError()

    def iter_gallery_images(self, gallery):
        """
        Iterate images from a Gallery.

        :type gallery: BaseGallery
        :rtype: iter(BaseImage)
        """
        raise NotImplementedError()