@@ -168,10 +168,11 @@ def __call__(self, *args, **kwargs):
168
168
169
169
170
170
@_rate_limit
171
- def _api_request (url , params ):
171
+ def _api_request (url , params , timeout = None ):
172
172
"""Makes a POST request for the URL with the given form parameters,
173
173
which are encoded as compressed form data, and returns a parsed JSON
174
174
response. May raise a WebServiceError if the request fails.
175
+ If the specified timeout passes, then raises a TimeoutError.
175
176
"""
176
177
headers = {
177
178
'Accept-Encoding' : 'gzip' ,
@@ -181,9 +182,11 @@ def _api_request(url, params):
181
182
with requests .Session () as session :
182
183
session .mount ('http://' , CompressedHTTPAdapter ())
183
184
try :
184
- response = session .post (url , data = params , headers = headers )
185
+ response = session .post (url , data = params , headers = headers , timeout = timeout )
185
186
except requests .exceptions .RequestException as exc :
186
187
raise WebServiceError ("HTTP request failed: {0}" .format (exc ))
188
+ except requests .exceptions .ReadTimeout :
189
+ raise WebServiceError ("HTTP timed out ({0}s)" .format (timeout ))
187
190
188
191
try :
189
192
return response .json ()
@@ -218,7 +221,7 @@ def fingerprint(samplerate, channels, pcmiter, maxlength=MAX_AUDIO_LENGTH):
218
221
raise FingerprintGenerationError ("fingerprint calculation failed" )
219
222
220
223
221
- def lookup (apikey , fingerprint , duration , meta = DEFAULT_META ):
224
+ def lookup (apikey , fingerprint , duration , meta = DEFAULT_META , timeout = None ):
222
225
"""Look up a fingerprint with the Acoustid Web service. Returns the
223
226
Python object reflecting the response JSON data.
224
227
"""
@@ -229,7 +232,7 @@ def lookup(apikey, fingerprint, duration, meta=DEFAULT_META):
229
232
'fingerprint' : fingerprint ,
230
233
'meta' : meta ,
231
234
}
232
- return _api_request (_get_lookup_url (), params )
235
+ return _api_request (_get_lookup_url (), params , timeout )
233
236
234
237
235
238
def parse_lookup_result (data ):
@@ -329,7 +332,7 @@ def fingerprint_file(path, maxlength=MAX_AUDIO_LENGTH, force_fpcalc=False):
329
332
return _fingerprint_file_fpcalc (path , maxlength )
330
333
331
334
332
- def match (apikey , path , meta = DEFAULT_META , parse = True , force_fpcalc = False ):
335
+ def match (apikey , path , meta = DEFAULT_META , parse = True , force_fpcalc = False , timeout = None ):
333
336
"""Look up the metadata for an audio file. If ``parse`` is true,
334
337
then ``parse_lookup_result`` is used to return an iterator over
335
338
small tuple of relevant information; otherwise, the full parsed JSON
@@ -338,14 +341,14 @@ def match(apikey, path, meta=DEFAULT_META, parse=True, force_fpcalc=False):
338
341
true, only the latter will be used.
339
342
"""
340
343
duration , fp = fingerprint_file (path , force_fpcalc = force_fpcalc )
341
- response = lookup (apikey , fp , duration , meta )
344
+ response = lookup (apikey , fp , duration , meta , timeout )
342
345
if parse :
343
346
return parse_lookup_result (response )
344
347
else :
345
348
return response
346
349
347
350
348
- def submit (apikey , userkey , data ):
351
+ def submit (apikey , userkey , data , timeout = None ):
349
352
"""Submit a fingerprint to the acoustid server. The ``apikey`` and
350
353
``userkey`` parameters are API keys for the application and the
351
354
submitting user, respectively.
@@ -383,7 +386,7 @@ def submit(apikey, userkey, data):
383
386
for k , v in d .items ():
384
387
args ["%s.%s" % (k , i )] = v
385
388
386
- response = _api_request (_get_submit_url (), args )
389
+ response = _api_request (_get_submit_url (), args , timeout )
387
390
if response .get ('status' ) != 'ok' :
388
391
try :
389
392
code = response ['error' ]['code' ]
@@ -394,7 +397,7 @@ def submit(apikey, userkey, data):
394
397
return response
395
398
396
399
397
- def get_submission_status (apikey , submission_id ):
400
+ def get_submission_status (apikey , submission_id , timeout = None ):
398
401
"""Get the status of a submission to the acoustid server.
399
402
``submission_id`` is the id of a fingerprint submission, as returned
400
403
in the response object of a call to the ``submit`` endpoint.
@@ -404,4 +407,4 @@ def get_submission_status(apikey, submission_id):
404
407
'client' : apikey ,
405
408
'id' : submission_id ,
406
409
}
407
- return _api_request (_get_submission_status_url (), params )
410
+ return _api_request (_get_submission_status_url (), params , timeout )
0 commit comments