Handle failed calls to omdbapi better.

This commit is contained in:
Derek Christensen
2016-12-06 21:17:07 -08:00
parent 8a4318885a
commit 7a5cfb5306
2 changed files with 56 additions and 19 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ test.sh
archive/
config
temp/
test.py

View File

@@ -20,35 +20,71 @@ def getdvdtype():
or a tv series """
dvd_title = args.title
year = dvd_title[(dvd_title.rindex('(')):len(dvd_title)]
year = re.sub('[()]','', year)
try:
year = dvd_title[(dvd_title.rindex('(')):len(dvd_title)]
except:
year = ""
else:
year = re.sub('[()]','', year)
dvd_title = dvd_title[0:(dvd_title.rindex('('))].strip()
dvd_title = cleanupstring(dvd_title)
try:
dvd_title = dvd_title[0:(dvd_title.rindex('('))].strip()
except:
dvd_title_clean = cleanupstring(dvd_title)
else:
dvd_title_clean = cleanupstring(dvd_title)
if year is None:
year = ""
dvd_title_info_json = urllib.request.urlopen("http://www.omdbapi.com/?t={0}&y={1}&plot=short&r=json".format(dvd_title, year)).read()
doc = json.loads(dvd_title_info_json.decode())
dvd_type = callwebservice(dvd_title, year)
return doc['Type']
# handle failures
# first see if there is a hyphen and split it
if (dvd_title.find("-") > -1):
dvd_title_slice = dvd_title[:dvd_title.find("-")]
dvd_title_slice =cleanupstring(dvd_title_slice)
dvd_type = callwebservice(dvd_title_slice)
# if still fail, then try slicing off the last word in a loop
while dvd_type == "fail" and dvd_title_clean.count('+') > 0:
dvd_title_clean = dvd_title_clean.rsplit('+', 1)[0]
dvd_type = callwebservice(dvd_title_clean)
return dvd_type
def cleanupstring(string):
# clean up title string to pass to OMDbapi.org
string = string.strip()
return re.sub('[_ ]',"+",string)
def callwebservice(dvd_title, year=""):
""" Queries OMDbapi.org for title information and parses if it's a movie
or a tv series """
try:
dvd_title_info_json = urllib.request.urlopen("http://www.omdbapi.com/?t={0}&y={1}&plot=short&r=json".format(dvd_title, year)).read()
except:
return "fail"
else:
doc = json.loads(dvd_title_info_json.decode())
if doc['Response'] == "False":
return "fail"
else:
return doc['Type']
args = entry()
dvd_type = getdvdtype()
print(dvd_type)
try:
dvd_type = getdvdtype()
except:
print("fail")
else:
# we only want "movie" or "series"
if dvd_type == "movie" or dvd_type == "series":
print(dvd_type)
else:
print("other")
# try:
# dvd_type = getdvdtype()
# except:
# print("fail")
# else:
# # we only want "movie" or "series"
# if dvd_type == "movie" or dvd_type == "series":
# print(dvd_type)
# else:
# print("other")