Handle failed calls to omdbapi better.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ test.sh
|
||||
archive/
|
||||
config
|
||||
temp/
|
||||
test.py
|
||||
|
||||
@@ -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())
|
||||
|
||||
return doc['Type']
|
||||
dvd_type = callwebservice(dvd_title, year)
|
||||
|
||||
# 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()
|
||||
|
||||
|
||||
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")
|
||||
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")
|
||||
Reference in New Issue
Block a user