-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetvideotype.py
executable file
·105 lines (86 loc) · 3.15 KB
/
getvideotype.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python3
import sys
import argparse
import urllib
import os
import xmltodict
import json
import re
def entry():
""" Entry to program, parses arguments"""
parser = argparse.ArgumentParser(description='Get type of dvd--movie or tv series')
parser.add_argument('-t', '--title', help='Title', required=True)
parser.add_argument('-k', '--key', help='API_Key', dest='omdb_api_key', required=True)
return parser.parse_args()
def getdvdtype():
""" Queries OMDbapi.org for title information and parses if it's a movie
or a tv series """
dvd_title = args.title
needs_new_year = "false"
omdb_api_key = args.omdb_api_key
try:
year = dvd_title[(dvd_title.rindex('(')):len(dvd_title)]
except:
year = ""
else:
year = re.sub('[()]','', year)
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_type = callwebservice(omdb_api_key, dvd_title_clean, year)
# print (dvd_type)
# handle failures
# this is kind of kludgy, but it kind of work...
if (dvd_type == "fail"):
# first try submitting without the year
dvd_type = callwebservice(omdb_api_key, dvd_title_clean, "")
# print (dvd_type)
if (dvd_type != "fail"):
#that means the year is wrong.
needs_new_year = "true"
if (dvd_type == "fail"):
# second 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(omdb_api_key, 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(omdb_api_key, dvd_title_clean)
if needs_new_year == "true":
#pass the new year back to bash to handle
global new_year
return dvd_type + "#" + new_year
else:
return dvd_type
def cleanupstring(string):
# clean up title string to pass to OMDbapi.org
string = string.strip()
return re.sub('[_ ]',"+",string)
def callwebservice(omdb_api_key, dvd_title, year=""):
""" Queries OMDbapi.org for title information and parses if it's a movie
or a tv series """
# print (dvd_title)
# print (year)
# print (omdb_api_key)
try:
dvd_title_info_json = urllib.request.urlopen("http://www.omdbapi.com/?t={1}&y={2}&plot=short&r=json&apikey={0}".format(omdb_api_key, dvd_title, year)).read()
except:
return "fail"
else:
doc = json.loads(dvd_title_info_json.decode())
if doc['Response'] == "False":
return "fail"
else:
global new_year
new_year = doc['Year']
return doc['Type']
args = entry()
dvd_type = getdvdtype()
print(dvd_type)