dbr / tvdb_api

Simple to use interface to TheTVDB.com API in Python
The Unlicense
339 stars 63 forks source link

How to search for an episode by Absolute Number? #69

Closed WirlyWirly closed 6 years ago

WirlyWirly commented 6 years ago

I was wondering how I can search for an an episode via its absolute number instead of the usual SxxExx method. Something like...

One Piece Episode 547 t['One Piece']['absolute_number'][547]

I looked at the ReadMe and even tried using dir() to figure this out, but I couldn't find an answer. I use other programs that take advantage of the Absolute Number offered by the API so I know it is available. Thanks in advance!

WirlyWirly commented 6 years ago

I was looking through tvdb_api.py and found a round-about way to do this using the search option. I'm still interested for a more straight-forward method, but for those who may be interested here is one solution...

# One Piece Episode 547
>>> t = tvdb_api.Tvdb()
>>> results = t['One Piece'].search(547, key='absoluteNumber')
>>> # We can then list all available keys if we wanted....
>>> results[0].keys()
dict_keys(['absoluteNumber', 'airedEpisodeNumber', 'airedSeason', 'airedSeasonID', 'dvdEpisodeNumber', 'dvdSeason', 'episodeName', 'firstAired', 'id', 'language', 'lastUpdated', 'overview'])

From there we can use any of those keys to get the appropriate value. So if we wanted the season number we would do...

>>> season_number = results[0]['airedSeason']
>>> print(season_number)
15

This method works fine but like I said if there exists a more straight-forward approach I'd be very interested in knowing.

dbr commented 6 years ago

Yep that's the way - search for the episode, ensure there is a single item, and then you have your episode object. Same as used in tvnamer when trying to find via absolute episode number:

https://github.com/dbr/tvnamer/blob/d4f52624353a9050708ae2b7c3cbd3cb16bb570d/tvnamer/utils.py#L698-L723

(I'm not quite sure why it handles the if len(sr) > 1: case like it does - seems a little strange)