44import requests
55
66from .exceptions import OMDBException , OMDBNoResults , OMDBLimitReached , OMDBTooManyResults , OMDBInvalidAPIKey
7- from .utilities import camelcase_to_snake_case , range_inclusive
7+ from .utilities import camelcase_to_snake_case , range_inclusive , to_int
88
99
1010class OMDB (object ):
@@ -13,15 +13,24 @@ class OMDB(object):
1313 Args:
1414 api_key (str): The API Key to use for the requests
1515 timeout (float): The timeout, in seconds
16+ strict (bool): To use strict error checking or not; strict (True) \
17+ will throw errors if the API returns an error code, non-strict will not
1618 Returns:
17- OMDB: An OMDB API wrapper connection object '''
19+ OMDB: An OMDB API wrapper connection object
20+ Note:
21+ With `strict` disabled, it is up to the user to check for and handle errors '''
1822
19- def __init__ (self , api_key , timeout = 5 ):
23+ __slots__ = ['_api_url' , '_timeout' , '_api_key' , '_session' , '_strict' ]
24+
25+ def __init__ (self , api_key , timeout = 5 , strict = True ):
2026 ''' the init object '''
2127 self ._api_url = 'https://www.omdbapi.com/'
22- self ._timeout = timeout
28+ self ._timeout = None
29+ self .timeout = timeout
2330 self ._api_key = None
2431 self .api_key = api_key
32+ self ._strict = None
33+ self .strict = strict
2534 self ._session = requests .Session ()
2635
2736 def close (self ):
@@ -43,6 +52,30 @@ def api_key(self, val):
4352 else :
4453 raise OMDBInvalidAPIKey (val )
4554
55+ @property
56+ def timeout (self ):
57+ ''' float: The timeout parameter to pass to requests for how long to wait '''
58+ return self ._timeout
59+
60+ @timeout .setter
61+ def timeout (self , val ):
62+ ''' set the timeout property '''
63+ try :
64+ self ._timeout = float (val )
65+ except ValueError :
66+ msg = "OMDB Timeout must be a float or convertable to float! {} provided" .format (val )
67+ raise ValueError (msg )
68+
69+ @property
70+ def strict (self ):
71+ ''' bool: Whether to throw or swallow errors; True will throw exceptions '''
72+ return self ._strict
73+
74+ @strict .setter
75+ def strict (self , val ):
76+ ''' set the strict property '''
77+ self ._strict = bool (val )
78+
4679 def search (self , title , pull_all_results = True , page = 1 , ** kwargs ):
4780 ''' Perform a search based on title
4881
@@ -101,7 +134,7 @@ def get(self, title=None, imdbid=None, **kwargs):
101134 }
102135 if imdbid :
103136 params ['i' ] = imdbid
104- if title :
137+ elif title :
105138 params ['t' ] = title
106139 else :
107140 raise OMDBException ("Either title or imdbid is required!" )
@@ -153,20 +186,32 @@ def get_movie(self, title=None, imdbid=None, **kwargs):
153186 params .update (kwargs )
154187 return self .get (title , imdbid , ** params )
155188
156- def get_series (self , title = None , imdbid = None , ** kwargs ):
189+ def get_series (self , title = None , imdbid = None , pull_episodes = False , ** kwargs ):
157190 ''' Retrieve a TV series information by title or IMDB id
158191
159192 Args:
160193 title (str): The name of the movie to retrieve
161194 imdbid (str): The IMDB id of the movie to retrieve
195+ pull_episodes (bool): `True` to pull the episodes
162196 kwargs (dict): the kwargs to add additional parameters to the API request
163197 Returns:
164198 dict: A dictionary of all the results
165199 Note:
166200 Either `title` or `imdbid` is required '''
167201 params = {'type' : 'series' }
168202 params .update (kwargs )
169- return self .get (title , imdbid , ** params )
203+ res = self .get (title , imdbid , ** params )
204+ num_seasons = 0
205+ if pull_episodes :
206+ num_seasons = to_int (res .get ('total_seasons' , 0 ))
207+ res ['seasons' ] = dict ()
208+
209+ for i in range (num_seasons ):
210+ season_num = i + 1
211+ season = self .get_episodes (title , imdbid , season = season_num )
212+ res ['seasons' ][season_num ] = season
213+
214+ return res
170215
171216 def get_episode (self , title = None , imdbid = None , season = 1 , episode = 1 , ** kwargs ):
172217 ''' Retrieve a TV series episode by title or IMDB id and season and episode number
@@ -187,7 +232,7 @@ def get_episode(self, title=None, imdbid=None, season=1, episode=1, **kwargs):
187232 if episode :
188233 params ['Episode' ] = episode
189234 params .update (kwargs )
190- return self .get (title , imdbid , ** params )
235+ return self .get (title = title , imdbid = imdbid , ** params )
191236
192237 def get_episodes (self , title = None , imdbid = None , season = 1 , ** kwargs ):
193238 ''' Retrieve all episodes of a TV series by season number
@@ -201,7 +246,7 @@ def get_episodes(self, title=None, imdbid=None, season=1, **kwargs):
201246 dict: A dictionary of all the results
202247 Note:
203248 Either `title` or `imdbid` is required '''
204- return self .get_episode (title , imdbid , season , None , ** kwargs )
249+ return self .get_episode (title = title , imdbid = imdbid , season = season , episode = None , ** kwargs )
205250
206251 def _get_response (self , kwargs ):
207252 ''' wrapper for the `requests` library call '''
@@ -231,7 +276,7 @@ def __format_results(self, res, params):
231276 res [camelcase_to_snake_case (key )] = val
232277
233278 # NOTE: I dislike having to use string comparisons to check for specific error conditions
234- if 'response' in res and res ['response' ] == 'False' :
279+ if self . strict and 'response' in res and res ['response' ] == 'False' :
235280 err = res .get ('error' , '' ).lower ()
236281 if err == 'too many results.' :
237282 raise OMDBTooManyResults (res ['error' ], params )
0 commit comments