4
4
import requests
5
5
6
6
from .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
8
8
9
9
10
10
class OMDB (object ):
@@ -13,15 +13,24 @@ class OMDB(object):
13
13
Args:
14
14
api_key (str): The API Key to use for the requests
15
15
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
16
18
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 '''
18
22
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 ):
20
26
''' the init object '''
21
27
self ._api_url = 'https://www.omdbapi.com/'
22
- self ._timeout = timeout
28
+ self ._timeout = None
29
+ self .timeout = timeout
23
30
self ._api_key = None
24
31
self .api_key = api_key
32
+ self ._strict = None
33
+ self .strict = strict
25
34
self ._session = requests .Session ()
26
35
27
36
def close (self ):
@@ -43,6 +52,30 @@ def api_key(self, val):
43
52
else :
44
53
raise OMDBInvalidAPIKey (val )
45
54
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
+
46
79
def search (self , title , pull_all_results = True , page = 1 , ** kwargs ):
47
80
''' Perform a search based on title
48
81
@@ -101,7 +134,7 @@ def get(self, title=None, imdbid=None, **kwargs):
101
134
}
102
135
if imdbid :
103
136
params ['i' ] = imdbid
104
- if title :
137
+ elif title :
105
138
params ['t' ] = title
106
139
else :
107
140
raise OMDBException ("Either title or imdbid is required!" )
@@ -153,20 +186,32 @@ def get_movie(self, title=None, imdbid=None, **kwargs):
153
186
params .update (kwargs )
154
187
return self .get (title , imdbid , ** params )
155
188
156
- def get_series (self , title = None , imdbid = None , ** kwargs ):
189
+ def get_series (self , title = None , imdbid = None , pull_episodes = False , ** kwargs ):
157
190
''' Retrieve a TV series information by title or IMDB id
158
191
159
192
Args:
160
193
title (str): The name of the movie to retrieve
161
194
imdbid (str): The IMDB id of the movie to retrieve
195
+ pull_episodes (bool): `True` to pull the episodes
162
196
kwargs (dict): the kwargs to add additional parameters to the API request
163
197
Returns:
164
198
dict: A dictionary of all the results
165
199
Note:
166
200
Either `title` or `imdbid` is required '''
167
201
params = {'type' : 'series' }
168
202
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
170
215
171
216
def get_episode (self , title = None , imdbid = None , season = 1 , episode = 1 , ** kwargs ):
172
217
''' 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):
187
232
if episode :
188
233
params ['Episode' ] = episode
189
234
params .update (kwargs )
190
- return self .get (title , imdbid , ** params )
235
+ return self .get (title = title , imdbid = imdbid , ** params )
191
236
192
237
def get_episodes (self , title = None , imdbid = None , season = 1 , ** kwargs ):
193
238
''' 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):
201
246
dict: A dictionary of all the results
202
247
Note:
203
248
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 )
205
250
206
251
def _get_response (self , kwargs ):
207
252
''' wrapper for the `requests` library call '''
@@ -231,7 +276,7 @@ def __format_results(self, res, params):
231
276
res [camelcase_to_snake_case (key )] = val
232
277
233
278
# 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' :
235
280
err = res .get ('error' , '' ).lower ()
236
281
if err == 'too many results.' :
237
282
raise OMDBTooManyResults (res ['error' ], params )
0 commit comments