0

I have the following code:

def coming_episode ( show ): try: show = api.search ( show , 'en' ) [ 0 ] except: print "a" return announced = [ 'show title' ] for e in show [ len ( show ) -1 ]: if e.FirstAired != '' and time.time () < time.mktime ( e.FirstAired.timetuple () ): announced.append ( [ e.EpisodeName , e.id , time.mktime ( e.FirstAired.timetuple () ) ] ) return announced 

And this works fine when I look for a show which exsists in the TVDB api. However, I also want to catch the exception when I type something stupid, such as "awdawd" as a show.

I tried except: and also except TVDBIndexError: but both still give me the following error:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "init.py", line 27, in <module> series = coming_episode ( series ) File "init.py", line 19, in coming_episode for e in show [ len ( show ) -1 ]: File "/Users/Sites/Python/_envs/Series/lib/python2.7/site-packages/pytvdbapi/api.py", line 340, in __getitem__ raise error.TVDBIndexError("Season {0} not found".format(item)) pytvdbapi.error.TVDBIndexError: (u'Season 0 not found', (), {}) 

What am I doing wrong here?

Thanks in advance ;)

3
  • 2
    your trace indicates the exception is being thrown on the for loop, not on api.search. What are you trying to iterate over? Commented Oct 17, 2013 at 13:20
  • You use pytvdbapi, right? Commented Oct 17, 2013 at 13:47
  • Yeah, sorry for not specifying that... Commented Oct 17, 2013 at 14:04

2 Answers 2

4

I guess you use pytvdbapi, so I took all my informations from here.

When you use

for e in show [ len ( show ) -1 ] 

you iterate over each episode of a single season of the show.

When a show has 3 seasons, you iterate over (len(show) == 3, 3 - 1 == 2) the 2nd season.

If a show has exactly one season, the season you try to iterate is len(show) == 1 => 1 - 1 = 0 => 0, but there's no season 0, only a season 1, so an error is raised. (I don't know for sure, but maybe if a show is not found there's still a Show instance with an empty Season instance).

You probably want to use:

for s in show: # for each season in show for e in s: # for each episode in season if e.FirstAired != '' and time.time (... 
Sign up to request clarification or add additional context in comments.

2 Comments

But iterations start at 0 right? So if the api returns show = [ 'show1' ] you'll get "show1" by calling show[0]. Or am I misunderstanding you? I just want the last episode of the last season.
Yes, I think so (I didn't test this myself, I just read the source code). The Show class has an member called seasons where the seasons are stored, and it's a dict, not a list. So if you access show[i] you don't get the season at index i, but rather the value of the seasons-dictionary with the key i.
0

Never mind... It suddenly works. Copy pasted the code just now on different machine, and it works. Weird stuff...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.