-
Notifications
You must be signed in to change notification settings - Fork 8
/
hass_radarr_search_by_voice.py
executable file
·425 lines (342 loc) · 17.4 KB
/
hass_radarr_search_by_voice.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import datetime
import requests
import json
import sys
import os
import configparser
import argparse
import logging
parser = argparse.ArgumentParser()
parser.add_argument("query", help="Term to search",
type=str)
parser.add_argument("mode", help="Running mode. 0=Add best match. 1=Full search. 2=Add numeric option. 3=Search by actor",
type=str)
parser.add_argument(
'-d', '--debug',
help="Print lots of debugging statements",
action="store_const", dest="loglevel", const=logging.DEBUG,
default=logging.WARNING,
)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
class MovieDownloader:
'''
Constrctor
:param str movie: Title of the movie or option number if mode 2 is used
:param int mode: 0 | 1 | 2 | 3
mode 0 - takes the movie string and download best guess from upcoming and recent years.
mode 1 - search movie string and offers 3 options to choose from.
mode 2 - download option given from previous search.
mode 3 - search latest movies by Actor/Actress and offers 5 options to choose from.
'''
def __init__(self, movie, mode=0):
self.loadParameters()
self.headers = {
'Content-type':'application/json',
'Accept':'application/json',
'X-Api-Key': self.RADARR_API
}
self.baseUrl = "/api/v3/movie"
year = datetime.datetime.now().year
term = movie
search_term = term.replace(" ", "%20")
current_years = [year, year+1, year+2]
for i in range(1,49):
current_years .append(year-i)
if mode == 0 or mode == 1: # we are making a search by movie title
# search
logging.debug("Searching movie: "+movie+" in the last 50 years and upcoming realeases.")
r = requests.get(self.RADARR_SERVER+self.baseUrl+"/lookup?term="+search_term, headers=self.headers)
if r.status_code == requests.codes.ok:
media_list = r.json()
if len(media_list) > 0:
if mode == 0: # download best guess
# add first occurrence to downloads
# we search for newish movies (recent and upcoming movies only)
logging.debug("Mode 0: Automatically adding best match.")
i = 0
found = False
while i < len(media_list) and found == False:
year = media_list[i]['year']
if year in current_years:
found = True
logging.debug("movie "+media_list[i]['title']+" found")
data = self.prepare_movie_json(media_list[i])
self.add_movie(data)
break;
i += 1
if found == False:
self.tts_google("No recent movie found. Try with the command Search movie.")
elif mode == 1: # search movie and give 3 options
# add to download_options file and read them out loud
logging.debug("Mode 1: Making a search and providing 3 best options found.")
i = 0
movies = []
while i < len(media_list) and i < 3:
data = self.prepare_movie_json(media_list[i])
movies.append(data)
i += 1
msg = self.save_options_found_and_compose_msg(movies)
self.tts_google(msg)
else:
logging.debug("Your radarr setup seems fine, but it didn't found a result.")
else:
logging.debug("Radarr didn't respond. Please check your conf file setup, such as server_url and api_key fo Radarr.")
elif mode == 3:
logging.debug("Mode 3: Making a search by Actor: "+term+" and providing 3 best options found.")
if self.TMDBID_API_V3 and "http" not in self.TMDBID_API_V3: # search latest movies by Actor/Actress and offers 5 options to choose from.
actor_id = self.get_actor_id(search_term)
if actor_id > 0:
movies = []
movies = self.get_actors_latest_movies(actor_id, year)
if len(movies) > 0:
msg = self.save_options_found_and_compose_msg(movies)
self.tts_google(msg)
else:
self.tts_google("No movies were found.")
else:
self.tts_google("No actor or actress was found.")
else:
logging.error("It looks like you haven't setup you api key (v3 auth) for TMDBID. To get one go to https://www.themoviedb.org/settings/api")
else:
# add to downloads from download_options file
try:
logging.debug("Mode 2: Adding numeric option from previous search in mode 1 or 3")
download_option = int(movie)-1
data = {}
with open(self.HASS_SCRIPTS_PATH+'/download_options.txt') as json_data:
movies = json.load(json_data)
if download_option > -1 and len(movies) >= download_option:
m = movies[download_option]
if m['qualityProfileId'] == -1 and m['tmdbId'] > 0:
r = requests.get(self.RADARR_SERVER+self.baseUrl+"/lookup/tmdb?tmdbId="+str(m['tmdbId']), headers=self.headers)
if r.status_code == requests.codes.ok:
media_list = r.json()
# print(media_list)
# if len(media_list) > 0:
data = self.prepare_movie_json(media_list)
else:
data = self.prepare_movie_json(m)
self.add_movie(data)
else:
self.tts_google("There's no such option.")
except ValueError:
logging.error("Sorry. That was not a valid option. It needs to be a number.")
def prepare_movie_json(self, media):
data = {}
data['title'] = media['title']
data['qualityProfileId'] = self.RADARR_QUALITY_PROFILE_ID
data['titleSlug'] = media['titleSlug']
data['images'] = media['images']
data['tmdbId'] = media['tmdbId']
data['rootFolderPath'] = self.RADARR_DOWNLOAD_PATH
data['monitored'] = True
data['minimumAvailability'] = 'released'
data['year'] = media['year']
data['cast'] = self.get_cast(data['tmdbId'])
return data
def prepare_barebone_movie_json(self, tmdbId, title):
data = {}
data['title'] = title
data['qualityProfileId'] = -1
data['titleSlug'] = 0
data['images'] = 0
data['tmdbId'] = tmdbId
data['rootFolderPath'] = 0
data['monitored'] = 0
data['minimumAvailability'] = 0
data['year'] = 0
data['cast'] = ""
return data
def add_movie(self, data):
r = requests.post(self.RADARR_SERVER+self.baseUrl,json.dumps(data), headers=self.headers)
logging.debug("Trying to add movie...")
if r.status_code == 201:
if str(data['cast']) == "":
self.tts_google("I added the movie "+str(data['title'])+" to your list.")
else:
self.tts_google("I added the movie "+str(data['title'])+" with "+str(data['cast'])+" to your list.")
movie = r.json()
with open(self.HASS_SCRIPTS_PATH+"/last_download_added.txt", "w") as myfile:
myfile.write("movie:"+str(movie['id'])+"\n")
else:
logging.debug("movie wasn't added. r.status_code:"+str(r.status_code))
logging.debug("This was the failed request. You can manually try it using curl to test it.")
logging.debug(self.curlify_request(r))
res = self.is_movie_already_added(data)
if res >= 0:
if res == 0:
logging.debug("the movie wasn't found on your Raddar list either.")
self.tts_google("I wasn't able to add the movie.")
else:
if str(data['cast']) == "":
self.tts_google("The movie "+str(data['title'])+" is already on your list.")
else:
self.tts_google("The movie "+str(data['title'])+" with "+str(data['cast'])+" is already on your list.")
else:
self.tts_google("Something went wrong when adding the movie. Please try again.")
def is_movie_already_added(self, data):
# print("http://"+self.RADARR_SERVER+"/api/movie?apikey="+self.RADARR_API)
r = requests.get(self.RADARR_SERVER+self.baseUrl, headers=self.headers)
logging.debug("Checking if movie with tmdbId: "+str(data['tmdbId'])+" already exists on Radarr...")
found = False
# print(data['tmdbId'])
# print(r.status_code)
if r.status_code == 200:
logging.debug("Radarr list retreived")
media_list = r.json()
# print(media_list)
if len(media_list) > 0:
logging.debug("Searching a match in "+str(len(media_list))+" movies already on Radarr...")
i = 0
while i < len(media_list) and found == False:
tmdbId = media_list[i]['tmdbId']
if tmdbId == data['tmdbId']:
found = True
break;
i += 1
return 1 if found == True else 0
else:
logging.error("Error while checking if movie already exists. r.status_code="+str(r.status_code))
logging.debug("This was the failed request. You can manually try it using curl to test it.")
logging.debug(self.curlify_request(r))
return -1
def get_cast(self, tmdbId):
if self.TMDBID_API_V3:
logging.debug("Getting movie cast...")
r = requests.get("https://api.themoviedb.org/3/movie/"+str(tmdbId)+"/credits?api_key="+self.TMDBID_API_V3)
if r.status_code == requests.codes.ok:
movie = r.json()
cast = movie['cast']
if len(cast) > 1:
return(cast[0]['name']+" and "+cast[1]['name'])
else:
return(cast[0]['name'])
else:
logging.debug("Getting cast request failed.")
return("")
def get_actor_id(self, actor_name):
r = requests.get("https://api.themoviedb.org/3/search/person?language=en-US&page=1&include_adult=false&api_key="+self.TMDBID_API_V3+"&query="+actor_name)
if r.status_code == requests.codes.ok:
results = r.json()
if int(results['total_results']) > 0:
return(int(results['results'][0]['id']))
else:
return(-1)
else:
return(-1)
def get_actors_latest_movies(self, actor_id, year):
latest_years = [ year+1, year, year-1, year-2]
i = 0
movies = []
while i < len(latest_years) and len(movies) < 5:
r = requests.get("https://api.themoviedb.org/3/discover/movie?language=en-US&page=1&sort_by=release_date.desc&include_adult=false&include_video=false&page=1&primary_release_year=&api_key="+self.TMDBID_API_V3+"&primary_release_year="+str(latest_years[i])+"&with_cast="+str(actor_id))
if r.status_code == requests.codes.ok:
results = r.json()
if int(results['total_results']) > 0:
for movie in results['results']:
if len(movies) < 5:
data = self.prepare_barebone_movie_json(int(movie["id"]), movie["title"])
movies.append(data)
i += 1
return(movies)
def save_options_found_and_compose_msg(self, movies):
msg=""
with open(self.HASS_SCRIPTS_PATH+"/download_options.txt", "w") as myfile:
json.dump(movies, myfile)
i = 0
if len(movies) > 1:
msg = "I found "+str(len(movies))+" options.\n"
else:
msg = "I found "+str(len(movies))+" option.\n"
while i < len(movies):
m = movies[i]
if str(m['cast']) != "":
msg = msg+"Option "+str(i+1)+", "+str(m['title'])+" with "+str(m['cast'])+".\n"
else:
msg = msg+"Option "+str(i+1)+", "+str(m['title'])+". "
i += 1
return msg
def tts_google(self, msg):
data = {"entity_id": self.HASS_SPEAKER_ENTITY, "message": msg}
if self.HASS_API == "" and self.HASS_TOKEN != "":
headers = {
'Authorization': 'Bearer '+self.HASS_TOKEN
}
r = requests.post(self.HASS_SERVER+"/api/services/tts/"+self.HASS_TTS_SERVICE,json.dumps(data), headers=headers)
else:
r = requests.post(self.HASS_SERVER+"/api/services/tts/"+self.HASS_TTS_SERVICE+"?api_password="+self.HASS_API,json.dumps(data))
# assistant-relay
# command_data = {"command": msg}
# r = requests.post("http://"+self.HASS_SERVER+"/api/services/rest_command/gh_broadcast?api_password="+self.HASS_API,json.dumps(command_data))
print(msg)
def curlify_request(self, r):
req = r.request
command = "curl --compressed -X {method} -H {headers} -d '{data}' '{uri}'"
method = req.method
uri = req.url
data = req.body
headers = ['"{0}: {1}"'.format(k, v) for k, v in req.headers.items()]
headers = " -H ".join(headers)
return command.format(method=method, headers=headers, data=data, uri=uri)
def loadParameters(self):
config=configparser.ConfigParser()
dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
configFile = os.path.join(dirname, 'ha_radarr_sonarr.conf')
config.read(configFile)
self.HASS_SERVER = config.get('HomeAssistant', 'server_url')
self.HASS_API = config.get('HomeAssistant', 'api_key')
self.HASS_TOKEN = config.get('HomeAssistant', 'token')
self.HASS_SCRIPTS_PATH = config.get('HomeAssistant', 'scripts_path')
self.HASS_SPEAKER_ENTITY = config.get('HomeAssistant', 'speaker_entity')
self.HASS_TTS_SERVICE = config.get('HomeAssistant', 'tts_service')
self.RADARR_SERVER = config.get('Radarr', 'server_url')
self.RADARR_API = config.get('Radarr', 'api_key')
self.RADARR_DOWNLOAD_PATH = config.get('Radarr', 'root_directory')
self.RADARR_QUALITY_PROFILE_ID = int(config.get('Radarr', 'profile_id'))
self.TMDBID_API_V3 = config.get('Services', 'tmdmid_api_key_v3')
self.checkConfig()
def checkConfig(self):
error_messages = []
warning_messages = []
if not self.HASS_SERVER:
error_messages.append('Home Assistant url with port (usually localhost:8123) must be defined')
if not self.HASS_API and not self.HASS_TOKEN:
error_messages.append('A Long-lived token or HA API password (legacy) must be defined')
if not self.HASS_SCRIPTS_PATH:
error_messages.append('Path were this script is located. must be defined. eg. /users/vr/.homeassistant/scripts or for container (e.g HA SUPERVISED) /config/scripts')
if not self.HASS_SPEAKER_ENTITY:
error_messages.append('Home assistant speaker entity_id must be specified. eg. media_player.family_room_speaker')
if not self.HASS_TTS_SERVICE:
error_messages.append('Home assistant text-to-speech service must be specified.')
if not self.RADARR_SERVER:
error_messages.append('Radarr url with port (usually :7878) must be defined')
if not self.RADARR_API:
error_messages.append('Radarr API Key must be defined')
if not self.RADARR_DOWNLOAD_PATH:
error_messages.append('Radarr root_directory also knwon as rootFolderPath must be defined')
if self.RADARR_QUALITY_PROFILE_ID == 0:
error_messages.append('Radarr quality profile id must be defined. Default value is 4 (1080p)')
if not self.TMDBID_API_V3:
warning_messages.append("Warning. tmdmid_api_key_v3 (optional)(recommended) is not set. You won't be able to search movies by actor or actress and your speaker's feedback will miss cast details for movies. https://www.themoviedb.org/settings/api v3 auth")
if len(error_messages) > 0:
print('Problem(s) in configuration file :')
for m in error_messages:
print(m)
if len(warning_messages) > 0:
for m in warning_messages:
print(m)
if len(error_messages) > 0:
exit(1)
# query = sys.argv[1]
# mode = sys.argv[2]
# full_search = sys.argv[2]
# downloading_from_file = sys.argv[3]
# download_option = int(sys.argv[4])-1
# print(query)
# print(mode)
# print(downloading_from_file)
# print(download_option)
downloader = MovieDownloader(args.query, int(args.mode))