-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
get_imdbid.py
49 lines (45 loc) · 1.86 KB
/
get_imdbid.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
import os, time, requests, json, sys, re, configparser
config = configparser.ConfigParser()
config.read('./config.ini')
omdbapi_key = config['sonarr']['omdbapi_key']
add_count = 0
failed_count = 0
res = 0
def main():
global add_count
global failed_count
global res
if len(sys.argv)<2: print ("No list Specified... Bye!!"); sys.exit(-1)
f=open(sys.argv[1], "r")
if f.mode == 'r': f1 = f.readlines()
print('\033c')
res = input("This list contains S) TV Shows or M) Movies [S/M]? : ")
if res == "S" or "s": res = 'series'
elif res == "M" or "m": res = 'movie'
else: print("Invalid Entry"); sys.exit(0)
fo = open("{}.csv".format(res), "a")
fo.write("title,year,imdbid\n")
for x in f1:
r = requests.get("https://www.omdbapi.com/?t={}&type={}&apikey={}".format(x.rstrip(),res,omdbapi_key))
if r.status_code == 200:
item = json.loads(r.text)
if item.get('Response') == "False":
failed_count +=1
ff = open("{}_failed".format(res), "a")
ff.write("{} Failed to be Matched\n".format(x.rstrip()))
ff.close()
continue
else:
year = item.get('Year')
imdbid = item.get('imdbID')
title = item.get('Title')
title = re.sub('[(]\d{4}[)]','',title)
fo.write("{},{},{}\n".format(title.rstrip(),year[:4],imdbid))
#print ("{},{},{}".format(title.rstrip(),year[:4],imdbid))
print('\033c')
print("Matching {} {}.".format(title,year[:4]))
add_count +=1
print("\u001b[32mMatched {} of {}, {} Failed to match. {} file was created.\u001b[0m".format(add_count,len(f1),failed_count,res))
fo.close()
if __name__ == "__main__":
main()