1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
--- src/mythtv/bindings/python/tmdb3/tmdb3/request.py.orig 2015-09-29 16:11:52.436024705 +0000
+++ src/mythtv/bindings/python/tmdb3/tmdb3/request.py 2015-09-29 16:13:24.297824407 +0000
@@ -16,6 +16,7 @@
import urllib2
import json
import os
+import time
DEBUG = False
cache = Cache(filename='pytmdb3.cache')
@@ -113,21 +114,29 @@
def readJSON(self):
"""Parse result from specified URL as JSON data."""
url = self.get_full_url()
- try:
- # catch HTTP error from open()
- data = json.load(self.open())
- except TMDBHTTPError, e:
- try:
- # try to load whatever was returned
- data = json.loads(e.response)
- except:
- # cannot parse json, just raise existing error
- raise e
- else:
- # response parsed, try to raise error from TMDB
- handle_status(data, url)
- # no error from TMDB, just raise existing error
- raise e
+ while True:
+ try:
+ # catch HTTP error from open()
+ data = json.load(self.open())
+ break
+ except TMDBHTTPError, e:
+ try:
+ # try to load whatever was returned
+ data = json.loads(e.response)
+ except:
+ # cannot parse json, just raise existing error
+ raise e
+ else:
+ # Check for error code of 25 which means we are doing more than 40 requests per minute.
+ if data.get('status_code', 1) == 25:
+ # Sleep and retry query.
+ time.sleep(10)
+ continue
+ else:
+ # response parsed, try to raise error from TMDB
+ handle_status(data, url)
+ # no error from TMDB, just raise existing error
+ raise e
handle_status(data, url)
if DEBUG:
import pprint
|