예제 #1
0
class Series(NameRepr, Element):
    id = Datapoint('id', initarg=1)
    backdrop = Datapoint('backdrop_path',
                         handler=Backdrop,
                         raw=False,
                         default=None)
    authors = Datalist('created_by', handler=Person)
    episode_run_times = Datalist('episode_run_time')
    first_air_date = Datapoint('first_air_date', handler=process_date)
    last_air_date = Datapoint('last_air_date', handler=process_date)
    genres = Datalist('genres', handler=Genre)
    homepage = Datapoint('homepage')
    in_production = Datapoint('in_production')
    languages = Datalist('languages')
    origin_countries = Datalist('origin_country')
    name = Datapoint('name')
    original_name = Datapoint('original_name')
    number_of_episodes = Datapoint('number_of_episodes')
    number_of_seasons = Datapoint('number_of_seasons')
    overview = Datapoint('overview')
    popularity = Datapoint('popularity')
    status = Datapoint('status')
    userrating = Datapoint('vote_average')
    votes = Datapoint('vote_count')
    poster = Datapoint('poster_path', handler=Poster, raw=False, default=None)
    networks = Datalist('networks', handler=Network)
    seasons = Datadict('seasons',
                       attr='season_number',
                       handler=Season,
                       passthrough={'id': 'series_id'})

    def _populate(self):
        return Request('tv/{0}'.format(self.id),
                       language=self._locale.language)

    def _populate_cast(self):
        return Request('tv/{0}/credits'.format(self.id))

    def _populate_images(self):
        kwargs = {}
        if not self._locale.fallthrough:
            kwargs['language'] = self._locale.language
        return Request('tv/{0}/images'.format(self.id), **kwargs)

    def _populate_external_ids(self):
        return Request('tv/{0}/external_ids'.format(self.id))

    cast = Datalist('cast', handler=Cast, poller=_populate_cast, sort='order')
    crew = Datalist('crew', handler=Crew, poller=_populate_cast)
    backdrops = Datalist('backdrops',
                         handler=Backdrop,
                         poller=_populate_images,
                         sort=True)
    posters = Datalist('posters',
                       handler=Poster,
                       poller=_populate_images,
                       sort=True)

    imdb_id = Datapoint('imdb_id', poller=_populate_external_ids)
    freebase_id = Datapoint('freebase_id', poller=_populate_external_ids)
    freebase_mid = Datapoint('freebase_mid', poller=_populate_external_ids)
    tvdb_id = Datapoint('tvdb_id', poller=_populate_external_ids)
    tvrage_id = Datapoint('tvrage_id', poller=_populate_external_ids)
예제 #2
0
class Movie(Element):
    @classmethod
    def latest(cls):
        req = Request('latest/movie')
        req.lifetime = 600
        return cls(raw=req.readJSON())

    @classmethod
    def nowplaying(cls, locale=None):
        res = MovieSearchResult(Request('movie/now-playing'), locale=locale)
        res._name = 'Now Playing'
        return res

    @classmethod
    def mostpopular(cls, locale=None):
        res = MovieSearchResult(Request('movie/popular'), locale=locale)
        res._name = 'Popular'
        return res

    @classmethod
    def toprated(cls, locale=None):
        res = MovieSearchResult(Request('movie/top_rated'), locale=locale)
        res._name = 'Top Rated'
        return res

    @classmethod
    def upcoming(cls, locale=None):
        res = MovieSearchResult(Request('movie/upcoming'), locale=locale)
        res._name = 'Upcoming'
        return res

    @classmethod
    def favorites(cls, session=None):
        if session is None:
            session = get_session()
        account = Account(session=session)
        res = MovieSearchResult(
            Request('account/{0}/favorite_movies'.format(account.id),
                    session_id=session.sessionid))
        res._name = "Favorites"
        return res

    @classmethod
    def ratedmovies(cls, session=None):
        if session is None:
            session = get_session()
        account = Account(session=session)
        res = MovieSearchResult(
            Request('account/{0}/rated_movies'.format(account.id),
                    session_id=session.sessionid))
        res._name = "Movies You Rated"
        return res

    @classmethod
    def watchlist(cls, session=None):
        if session is None:
            session = get_session()
        account = Account(session=session)
        res = MovieSearchResult(
            Request('account/{0}/movie_watchlist'.format(account.id),
                    session_id=session.sessionid))
        res._name = "Movies You're Watching"
        return res

    @classmethod
    def fromIMDB(cls, imdbid, locale=None):
        try:
            # assume string
            if not imdbid.startswith('tt'):
                imdbid = "tt{0:0>7}".format(imdbid)
        except AttributeError:
            # assume integer
            imdbid = "tt{0:0>7}".format(imdbid)
        if locale is None:
            locale = get_locale()
        movie = cls(imdbid, locale=locale)
        movie._populate()
        return movie

    id = Datapoint('id', initarg=1)
    title = Datapoint('title')
    originaltitle = Datapoint('original_title')
    tagline = Datapoint('tagline')
    overview = Datapoint('overview')
    runtime = Datapoint('runtime')
    budget = Datapoint('budget')
    revenue = Datapoint('revenue')
    releasedate = Datapoint('release_date', handler=process_date)
    homepage = Datapoint('homepage')
    imdb = Datapoint('imdb_id')

    backdrop = Datapoint('backdrop_path',
                         handler=Backdrop,
                         raw=False,
                         default=None)
    poster = Datapoint('poster_path', handler=Poster, raw=False, default=None)

    popularity = Datapoint('popularity')
    userrating = Datapoint('vote_average')
    votes = Datapoint('vote_count')

    adult = Datapoint('adult')
    collection = Datapoint('belongs_to_collection', handler=lambda x: \
                                                        Collection(raw=x))
    genres = Datalist('genres', handler=Genre)
    studios = Datalist('production_companies', handler=Studio)
    countries = Datalist('production_countries', handler=Country)
    languages = Datalist('spoken_languages', handler=Language)

    def _populate(self):
        return Request('movie/{0}'.format(self.id), \
                       language=self._locale.language)

    def _populate_titles(self):
        kwargs = {}
        if not self._locale.fallthrough:
            kwargs['country'] = self._locale.country
        return Request('movie/{0}/alternative_titles'.format(self.id),
                       **kwargs)

    def _populate_cast(self):
        return Request('movie/{0}/casts'.format(self.id))

    def _populate_images(self):
        kwargs = {}
        if not self._locale.fallthrough:
            kwargs['language'] = self._locale.language
        return Request('movie/{0}/images'.format(self.id), **kwargs)

    def _populate_keywords(self):
        return Request('movie/{0}/keywords'.format(self.id))

    def _populate_releases(self):
        return Request('movie/{0}/releases'.format(self.id))

    def _populate_trailers(self):
        return Request('movie/{0}/trailers'.format(self.id),
                       language=self._locale.language)

    def _populate_translations(self):
        return Request('movie/{0}/translations'.format(self.id))

    alternate_titles = Datalist('titles', handler=AlternateTitle, \
                                poller=_populate_titles, sort=True)

    # FIXME: this data point will need to be changed to 'credits' at some point
    cast = Datalist('cast', handler=Cast, poller=_populate_cast, sort='order')

    crew = Datalist('crew', handler=Crew, poller=_populate_cast)
    backdrops = Datalist('backdrops',
                         handler=Backdrop,
                         poller=_populate_images,
                         sort=True)
    posters = Datalist('posters',
                       handler=Poster,
                       poller=_populate_images,
                       sort=True)
    keywords = Datalist('keywords', handler=Keyword, poller=_populate_keywords)
    releases = Datadict('countries',
                        handler=Release,
                        poller=_populate_releases,
                        attr='country')
    youtube_trailers = Datalist('youtube',
                                handler=YoutubeTrailer,
                                poller=_populate_trailers)
    apple_trailers = Datalist('quicktime',
                              handler=AppleTrailer,
                              poller=_populate_trailers)
    translations = Datalist('translations',
                            handler=Translation,
                            poller=_populate_translations)

    def setFavorite(self, value):
        req = Request('account/{0}/favorite'.format(
            Account(session=self._session).id),
                      session_id=self._session.sessionid)
        req.add_data({
            'movie_id': self.id,
            'favorite': str(bool(value)).lower()
        })
        req.lifetime = 0
        req.readJSON()

    def setRating(self, value):
        if not (0 <= value <= 10):
            raise TMDBError("Ratings must be between '0' and '10'.")
        req = Request('movie/{0}/rating'.format(self.id),
                      session_id=self._session.sessionid)
        req.lifetime = 0
        req.add_data({'value': value})
        req.readJSON()

    def setWatchlist(self, value):
        req = Request('account/{0}/movie_watchlist'.format(
            Account(session=self._session).id),
                      session_id=self._session.sessionid)
        req.lifetime = 0
        req.add_data({
            'movie_id': self.id,
            'movie_watchlist': str(bool(value)).lower()
        })
        req.readJSON()

    def getSimilar(self):
        return self.similar

    @property
    def similar(self):
        res = MovieSearchResult(Request('movie/{0}/similar_movies'.format(
            self.id)),
                                locale=self._locale)
        res._name = 'Similar to {0}'.format(self._printable_name())
        return res

    @property
    def lists(self):
        res = ListSearchResult(Request('movie/{0}/lists'.format(self.id)))
        res._name = "Lists containing {0}".format(self._printable_name())
        return res

    def _printable_name(self):
        if self.title is not None:
            s = u"'{0}'".format(self.title)
        elif self.originaltitle is not None:
            s = u"'{0}'".format(self.originaltitle)
        else:
            s = u"'No Title'"
        if self.releasedate:
            s = u"{0} ({1})".format(s, self.releasedate.year)
        return s

    def __repr__(self):
        return u"<{0} {1}>".format(self.__class__.__name__,
                                   self._printable_name()).encode('utf-8')