-
Notifications
You must be signed in to change notification settings - Fork 16
/
show.py
73 lines (57 loc) · 2.51 KB
/
show.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from season import Season
from episode import Episode
class Show(object):
def __init__(self, element, server):
self.server = server
self.type = 'show'
try:
self.key = element.attrib['key']
self.title = element.attrib['title']
self.summary = element.attrib['summary']
except KeyError as e:
print "Missing key in element: ", e.message
self.genres = [e.attrib['tag'] for e in element.findall('.Genre')]
self.collections = [e.attrib['tag'] for e in element.findall('.Collection')]
self.seasons_ = []
def __len__(self):
""" returns the number of seasons of this show. """
return len(self.seasons)
def __iter__(self):
""" iterate over all seasons. """
for s in self.seasons:
yield s
def __str__(self):
return "<Show: %s>" % self.title
def __repr__(self):
return "<Show: %s>" % self.title
@property
def seasons(self):
""" property that returns a list to all seasons of the show. caches it's value after first call. """
if not self.seasons_:
element = self.server.query(self.key)
self.seasons_ = [Season(e, self.server) for e in element if ('type' in e.attrib) and (e.attrib['type'] == 'season')]
return self.seasons_
def getSeason(self, num):
""" returns the season with the index number `num` or None if it doesn't exist. """
return next((s for s in self.seasons if s.index == num), None)
def getAllEpisodes(self):
""" returns a list of all episodes of the show independent of seasons. """
key = '/'.join(self.key.split('/')[:-1]) + '/allLeaves'
element = self.server.query(key)
episodes = [Episode(e, self.server) for e in element if ('type' in e.attrib) and (e.attrib['type'] == 'episode')]
return episodes
def getNextUnwatchedEpisode(self):
""" returns the episode that follows the last watched episode in the show over
all seasons. if all are watched, return None.
"""
key = '/'.join(self.key.split('/')[:-1]) + '/allLeaves'
element = self.server.query(key)
prev = None
for e in reversed(element):
if ('viewCount' in e.attrib) and (e.attrib['viewCount'] == '1'):
if prev == None:
return None
else:
return Episode(prev, self.server)
prev = e
return Episode(element[0], self.server)