Skip to content

Commit

Permalink
Netflix collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryszard committed Jul 7, 2009
1 parent d959277 commit 31a5464
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions netflix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def __init__(self, href=None, rel=None, title=None):

class NetflixCategory(NetflixObject, Printable):
important = 'term'
def __init__(self, label=None, scheme=None, term=None):
self.label, self.scheme, self.term = label, scheme, term
def __init__(self, label=None, scheme=None, term=None, content=None):
self.label, self.scheme, self.term, self.content = label, scheme, term, content

class FancyObject(NetflixObject):
def __init__(self, d):
Expand All @@ -79,7 +79,8 @@ def __init__(self, d):
title = d.pop('title')
self.title = title['regular']
self.title_short = title['short']
self.categories = [NetflixCategory(**di) for di in d.pop('category')]
categories = d.pop('category')
self.categories = [NetflixCategory(**di) for di in categories]
super(CatalogTitle, self).__init__(d)

class NetflixUser(FancyObject, Printable):
Expand All @@ -92,10 +93,30 @@ def __init__(self, d):
self.preferred_formats = [NetflixCategory(**dd['category']) for dd in preferred_formats]
super(NetflixUser, self).__init__(d)

class RentalHistory(FancyObject):
class NetflixCollection(FancyObject):
item_type = CatalogTitle
def __init__(self, d):
self.items = [CatalogTitle(dd) for dd in d.pop('rental_history_item')]
super(RentalHistory, self).__init__(d)
try:
items = d.pop(self._items_name)
except AttributeError:
raise NotImplemened("NetflixCollection subclasses must set _items_name")
if not isinstance(items, (list, tuple)):
items = [items]
self.items = [self.item_type(dd) for dd in items]
super(NetflixCollection, self).__init__(d)
for lab in 'number_of_results', 'results_per_page', 'start_index':
setattr(self, lab, int(getattr(self, lab)))

def __iter__(self):
for item in self.items:
yield item


class RentalHistory(NetflixCollection):
_items_name = "rental_history_item"

class NetflixQueue(NetflixCollection):
_items_name = 'queue_item'

class NetflixAvailability(NetflixObject, Printable):
important = ('category', 'available_from')
Expand Down Expand Up @@ -154,6 +175,8 @@ def isa(label):
return NetflixUser(d['user'])
elif isa('rental_history'):
return RentalHistory(d['rental_history'])
elif isa('queue'):
return NetflixQueue(d['queue'])
else:
return d

Expand Down Expand Up @@ -234,5 +257,5 @@ def request(self, url, token=None, **args):
time.sleep(1)
req = urllib2.urlopen(oa_req.to_url())
except urllib2.HTTPError, e:
raise NotFound(url, e)
raise NotFound(url, e.read())
return json.load(req, object_hook=self.object_hook)

0 comments on commit 31a5464

Please sign in to comment.