-
Notifications
You must be signed in to change notification settings - Fork 0
/
radarr-importer.py
118 lines (97 loc) · 3.46 KB
/
radarr-importer.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
106
107
108
109
110
111
112
113
114
115
116
117
118
import json
import os
import re
import requests
import tmdbsimple as tmdb
###################
## FILL THESE IN ##
###################
tmdb.API_KEY = 'tmdb api key goes here'
radarr_host = 'localhost'
radarr_port = '7878'
radarr_api_key = 'radarr api key goes here'
radarr_base_path = 'leave blank if no base path is set'
radarr_movies_dir = '/movies'
local_movies_dir = '/mnt/movies'
max_retries = 6
##########################
## DONT TOUCH PAST HERE ##
## VERY FRAGILE ##
##########################
radarr_socket = radarr_host + ':' + radarr_port
radarr_api_path = radarr_base_path + '/api/v3/movie'
movies = os.listdir(local_movies_dir)
movies_pattern = re.compile('^(.*) \((\d+)\)$')
r = requests.get('http://{}{}?apikey={}'.format(radarr_socket, radarr_api_path,
radarr_api_key))
if not r.ok:
exit()
radarr_movies = json.loads(r.text)
count_new = 0
count_failed = 0
count_existing = 0
def radarr_add_movie(movie, year, tmdb_result):
global count_new
global count_failed
global count_existing
for existing_movie in radarr_movies:
if existing_movie['tmdbId'] == tmdb_result['id']:
print('{} already exists in radarr'.format(movie))
count_existing += 1
return
payload = {
'title': tmdb_result['title'],
'qualityProfileId': '4',
'titleSlug':
tmdb_result['title'].lower() + '-' + str(tmdb_result['id']),
'monitored': 'true',
'tmdbId': tmdb_result['id'],
'year': year,
'rootFolderPath': radarr_movies_dir,
'hasFile': 'true',
'minimumAvailability': 'announced',
'addOptions': {}
}
num_retries = 0
while num_retries < max_retries:
if num_retries > 0:
print('retrying...')
try:
r = requests.post('http://{}{}?apikey={}'.format(
radarr_socket, radarr_api_path, radarr_api_key),
headers={'Content-Type': 'application/json'},
data=json.dumps(payload),
timeout=10)
if not r.ok:
for error in json.loads(r.text):
print(error)
count_failed = count_failed + 1
return
count_new += 1
print('{} was added successfully'.format(movie))
return
except requests.exceptions.ReadTimeout:
print('timeout whilde adding {}'.format(movie))
num_retries += 1
count_failed += 1
for dir in movies:
match = movies_pattern.match(dir)
if match:
print(match.group(1), match.group(2))
resp = tmdb.Search().movie(query=match.group(1), year=match.group(2))
if resp['total_results'] == 0:
print('no matches found for {}'.format(match.group(1)))
continue
if resp['total_results'] == 1:
#print('found one match for {}'.format(match.group(1)))
radarr_add_movie(match.group(1), match.group(2),
resp['results'][0])
if resp['total_results'] > 1:
#print('found multiple matches for {}'.format(match.group(1)))
radarr_add_movie(match.group(1), match.group(2),
resp['results'][0])
print('---')
print('duplicates:', count_existing)
print('new:', count_new)
print('failed:', count_failed)
print('folders processed:' count_existing + count_new + count_failed)