From 6e7725de7d8507c3675aa89b17a49da06abc7cc2 Mon Sep 17 00:00:00 2001 From: Raymond Wagner Date: Thu, 21 Jun 2012 01:39:44 -0400 Subject: [PATCH] Add Genre list and associated Movie search --- README | 10 +++++++++- setup.py | 2 +- tmdb3/__init__.py | 2 +- tmdb3/tmdb_api.py | 25 ++++++++++++++++++++++++- tmdb3/util.py | 2 +- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README b/README index 71dcd8f..f0682c8 100644 --- a/README +++ b/README @@ -129,6 +129,10 @@ on-demand as used, rather than when the object is created. >>> Studio(1) +The Genre class cannot be called by id directly, however it does have a getAll +classmethod, capable of returning all available genres for a specified +language. + ## Image Behavior TheMovieDb currently offers three types of artwork: backdrops, posters, and @@ -220,7 +224,7 @@ Movie: list(Translation) translations list(Movie) getSimilar() -Movie classmethods +Movie classmethods: Movie fromIMDB(imdbid) special constructor for use with IMDb codes Movie latest() gets latest movie added to database list(Movie) nowplaying() content currently in theater @@ -287,6 +291,10 @@ Translation: Genre: integer id string name + list(Movie) movies + +Genre classmethods: + list(Genre) getAll(language) returns list of all genres Studio: integer id diff --git a/setup.py b/setup.py index c81a674..b254f78 100755 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='tmdb3', - version='0.6.3', + version='0.6.4', description='TheMovieDB.org APIv3 interface', long_description="Object-oriented interface to TheMovieDB.org's v3 API.", packages=['tmdb3'] diff --git a/tmdb3/__init__.py b/tmdb3/__init__.py index 97e18ba..b0799ee 100644 --- a/tmdb3/__init__.py +++ b/tmdb3/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/env python from tmdb_api import Configuration, searchMovie, searchPerson, searchStudio, \ - Studio, Person, Movie, Collection, __version__ + Studio, Person, Movie, Collection, Genre, __version__ from request import set_key, set_cache from locales import get_locale, set_locale from tmdb_auth import get_session, set_session diff --git a/tmdb3/tmdb_api.py b/tmdb3/tmdb_api.py index 655d26c..6492cd1 100644 --- a/tmdb3/tmdb_api.py +++ b/tmdb3/tmdb_api.py @@ -22,7 +22,7 @@ Preliminary API specifications can be found at http://help.themoviedb.org/kb/api/about-3""" -__version__="v0.6.2" +__version__="v0.6.4" # 0.1.0 Initial development # 0.2.0 Add caching mechanism for API queries # 0.2.1 Temporary work around for broken search paging @@ -46,6 +46,7 @@ # 0.6.1 Add adult filtering for people searches # 0.6.2 Add similar movie search for Movie objects # 0.6.3 Add Studio search +# 0.6.4 Add Genre list and associated Movie search from request import set_key, Request from util import Datapoint, Datalist, Datadict, Element, NameRepr, SearchRepr @@ -286,6 +287,28 @@ class Genre( NameRepr, Element ): id = Datapoint('id') name = Datapoint('name') + def _populate_movies(self): + return Request('genre/{0}/movies'.format(self.id), \ + language=self._locale.language) + + @property + def movies(self): + if 'movies' not in self._data: + search = MovieSearchResult(self._populate_movies(), \ + locale=self._locale) + search._name = "{0.name} Movies".format(self) + self._data['movies'] = search + return self._data['movies'] + + @classmethod + def getAll(cls, locale=None): + class GenreList( Element ): + genres = Datalist('genres', handler=Genre) + def _populate(self): + return Request('genre/list', language=self._locale.language) + return GenreList(locale=locale).genres + + class Studio( NameRepr, Element ): id = Datapoint('id', initarg=1) name = Datapoint('name') diff --git a/tmdb3/util.py b/tmdb3/util.py index 1c3209e..1ac5c29 100644 --- a/tmdb3/util.py +++ b/tmdb3/util.py @@ -328,7 +328,7 @@ def __new__(mcs, name, bases, attrs): def __call__(cls, *args, **kwargs): obj = cls.__new__(cls) - if 'locale' in kwargs: + if ('locale' in kwargs) and (kwargs['locale'] is not None): obj._locale = kwargs['locale'] else: obj._locale = get_locale()