Skip to content

Commit

Permalink
robustified exception handling in xmg and renamer...possible fix to r…
Browse files Browse the repository at this point in the history
…enamer crashes?
  • Loading branch information
dmwyatt committed Dec 16, 2010
1 parent 71f01f4 commit 16bf872
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions library/xmg/xmg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import os
import urllib2
import json
import os
import urllib2

__author__ = 'Therms'
__tmdb_apikey__ = '6d96a9efb4752ed0d126d94e12e52036'
Expand Down Expand Up @@ -53,27 +53,27 @@ def __init__(self, imdbid, imdbpy = None):

if imdbid[:2].lower() == 'tt':
self.imdbid = imdbid[2:]
else:
self.imdbid = imdbid

self.nfo_string = 'http://www.imdb.com/title/' + imdbid + '/'
self.tmdb_data = self._get_tmdb_imdb()
self._validate_tmdb_json()

#quick fix till I figure out what the actual problem is...try 3 times to
#get valid data...
count = 0
while 1:
count += 1
try:
_ = self._get_fanart(0,0)
break
except TypeError:
self.tmdb_data = self._get_tmdb_imdb()
if count > 3:
raise ApiError("Can't get valid tmdb data, received: %s" % self.tmdb_data)

#TODO: Search by movie name
#TODO: Search by tmdb_id
#TODO: Search by movie hash



def _validate_tmdb_json(self):
try:
_ = self._get_fanart(0,0)
except:
try:
_ = self._get_poster(0,0)
except:
raise ApiError("Unknown TMDB data format: %s" % self.tmdb_data)

def write_nfo(self, path):
try:
f = open(path, 'w')
Expand Down Expand Up @@ -148,9 +148,23 @@ def write_poster(self, filename_root, path, min_height, min_width):

def _get_tmdb_imdb(self):
url = "http://api.themoviedb.org/2.1/Movie.imdbLookup/en/json/%s/%s" % (__tmdb_apikey__, "tt" + self.imdbid)
response = urllib2.urlopen(url)
tmdb_data = json.loads(response.read())[0]
return tmdb_data

count = 0
while 1:
count += 1
response = urllib2.urlopen(url)
json_string = response.read()
try:
tmdb_data = json.loads(json_string)[0]
return tmdb_data
except ValueError, e:
if count < 3:
continue
else:
raise ApiError("Invalid JSON: %s: %s" % (e, json_string))
except:
ApiError("JSON error with: %s" % json_string)


def _get_image(self, image_list, min_height, min_width):
#Select image
Expand Down Expand Up @@ -185,7 +199,7 @@ def _get_image(self, image_list, min_height, min_width):
id = sys.argv[1]
except:
id = 'tt0111161'

x = MetaGen(id)
x.write_nfo(".\movie.nfo")
x.write_fanart("fanart", ".", 0, 0)
Expand Down

0 comments on commit 16bf872

Please sign in to comment.