Skip to content

Commit

Permalink
Allow locale fallthrough for movie images and alternate titles.
Browse files Browse the repository at this point in the history
When fallthrough is enabled, queries for backdrops, posters, and
alternate titles will not filter for the selected locale, but will
instead sort the results to place those matching the specified locale at
the top of the list.

Also resolves an issue where Request() was incorrectly passing 'None' as
an argument when no locale was set.
  • Loading branch information
wagnerrp committed Mar 31, 2012
1 parent 61045e4 commit ddc0cb0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
10 changes: 10 additions & 0 deletions tmdb3/locales.py
Expand Up @@ -32,6 +32,13 @@ def __delattr__(self, key):
' does not support modification.')
super(LocaleBase, self).__delattr__(key)

def __lt__(self, other):
return (id(self) != id(other)) and (str(self) > str(other))
def __gt__(self, other):
return (id(self) != id(other)) and (str(self) < str(other))
def __eq__(self, other):
return (id(self) == id(other)) or (str(self) == str(other))

@classmethod
def getstored(cls, key):
if key is None:
Expand Down Expand Up @@ -82,6 +89,9 @@ def __init__(self, language, country):
self.language = Language.getstored(language)
self.country = Country.getstored(country)

def __str__(self):
return u"{0}_{1}".format(self.language, self.country)

def __repr__(self):
return u"<Locale {0.language}_{0.country}>".format(self)

Expand Down
6 changes: 4 additions & 2 deletions tmdb3/request.py
Expand Up @@ -17,9 +17,11 @@
import os

DEBUG = False

cache = Cache(filename='pytmdb3.cache')

#DEBUG = True
#cache = Cache(engine='null')

def set_key(key):
"""
Specify the API key to use retrieving data from themoviedb.org. This
Expand Down Expand Up @@ -55,7 +57,7 @@ def __init__(self, url, **kwargs):
self._kwargs = dict([(kwa,kwv) for kwa,kwv in kwargs.items()
if kwv is not None])
url = '{0}{1}?{2}'.format(self._base_url, self._url,
urllib.urlencode(kwargs))
urllib.urlencode(self._kwargs))
urllib2.Request.__init__(self, url)
self.add_header('Accept', 'application/json')
self.lifetime = 3600 # 1hr
Expand Down
32 changes: 27 additions & 5 deletions tmdb3/tmdb_api.py
Expand Up @@ -107,6 +107,16 @@ def geturl(self, size='original'):
url = Configuration.images['base_url'].rstrip('/')
return url+'/{0}/{1}'.format(size, self.filename)

# sort preferring locale's language, but keep remaining ordering consistent
def __lt__(self, other):
return (self.language == self._locale.language) \
and (self.language != other.language)
def __gt__(self, other):
return (self.language != other.language) \
and (other.language == self._locale.language)
def __eq__(self, other):
return self.language == other.language

def __repr__(self):
return u"<{0.__class__.__name__} '{0.filename}'>".format(self)

Expand All @@ -128,6 +138,16 @@ class AlternateTitle( Element ):
country = Datapoint('iso_3166_1')
title = Datapoint('title')

# sort preferring locale's country, but keep remaining ordering consistent
def __lt__(self, other):
return (self.country == self._locale.country) \
and (self.country != other.country)
def __gt__(self, other):
return (self.country != other.country) \
and (other.country == self._locale.country)
def __eq__(self, other):
return self.country == other.country

class Person( Element ):
id = Datapoint('id', initarg=1)
name = Datapoint('name')
Expand Down Expand Up @@ -342,11 +362,13 @@ def fromIMDB(cls, imdbid, locale=None):
def _populate(self):
return Request('movie/{0}'.format(self.id), language=self._locale.language)
def _populate_titles(self):
return Request('movie/{0}/alternative_titles'.format(self.id), country=self._locale.country)
kwargs = {'country':self._locale.country} if not self._locale.fallthrough else {}
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):
return Request('movie/{0}/images'.format(self.id), language=self._locale.language)
kwargs = {'language':self._locale.language} if not self._locale.fallthrough else {}
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):
Expand All @@ -356,11 +378,11 @@ def _populate_trailers(self):
def _populate_translations(self):
return Request('movie/{0}/translations'.format(self.id))

alternate_titles = Datalist('titles', handler=AlternateTitle, poller=_populate_titles)
alternate_titles = Datalist('titles', handler=AlternateTitle, poller=_populate_titles, sort=True)
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)
posters = Datalist('posters', handler=Poster, poller=_populate_images)
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)
Expand Down

0 comments on commit ddc0cb0

Please sign in to comment.