-
Notifications
You must be signed in to change notification settings - Fork 8
/
hass_sonarr_search_by_voice.py
403 lines (328 loc) · 15.5 KB
/
hass_sonarr_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
import datetime
import requests
import json
import sys
import os
import configparser
# ------------------------------------
class ShowDownloader:
'''
Constrctor
:param str show: Title of the tv show or option number if mode 2 is used
:param int mode: 0 | 1 | 2
mode 0 - takes the tv show string and download best guess from upcoming and recent years.
mode 1 - search tv show string and offers 3 options to choose from.
mode 2 - download option given from previous search.
:param str monitor: missing | future
missing - Monitors episodes that do not have files or have not aired yet.
future - Monitors episodes that have not aired yet.
'''
def __init__(self, show, mode=0, monitor='future'):
self.monitor = monitor
self.loadParameters()
year = datetime.datetime.now().year
term = show
search_term = term.replace(" ", "%20")
current_years = [year, year+1, year+2]
for i in range(1,69):
current_years .append(year-i)
if mode == 0 or mode == 1: # we are making a search by series title
# search
r = requests.get(self.SONARR_SERVER+"/api/series/lookup?term="+search_term+"&apikey="+self.SONARR_API)
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 show (recent and upcoming show only)
i = 0
found = False
while i < len(media_list) and found == False:
year = media_list[i]['year']
if year in current_years:
found = True
data = self.prepare_show_json(media_list[i])
self.add_show(data)
break;
i += 1
if found == False:
self.tts_google("I didn't find the tv show. Try again with the search option.")
elif mode == 1: # search tv show and give 3 options
# add to download_options file and read them out loud
i = 0
show = []
while i < len(media_list) and i < 3:
data = self.prepare_show_json(media_list[i])
show.append(data)
i += 1
msg = self.save_options_found_and_compose_msg(show)
self.tts_google(msg)
# elif mode == 3: # search latest show by Actor/Actress and offers 5 options to choose from.
# actor_id = self.get_actor_id(search_term)
# if actor_id > 0:
# show = []
# show = self.get_actors_latest_show(actor_id, year)
# if len(show) > 0:
# msg = self.save_options_found_and_compose_msg(show)
# self.tts_google(msg)
# else:
# self.tts_google("Your tv show was not found.")
# else:
# self.tts_google("The actor was not found.")
else:
# add to downloads from download_options file
download_option = int(show)-1
data = {}
with open(self.HASS_SCRIPTS_PATH+'/download_tvshow_options.txt') as json_data:
show = json.load(json_data)
if download_option > -1 and len(show) >= download_option:
m = show[download_option]
if m['profileId'] == -1 and m['tvdbId'] > 0:
r = requests.get(self.SONARR_SERVER+"/api/series/lookup?term=tvdb:"+str(m['tvdbId'])+"&apikey="+self.SONARR_API)
if r.status_code == requests.codes.ok:
media_list = r.json()
# print(media_list)
# if len(media_list) > 0:
data = self.prepare_show_json(media_list)
else:
data = self.prepare_show_json(m)
self.add_show(data)
else:
self.tts_google("There are no options.")
def prepare_show_json(self, media):
data = {}
addOptions = {}
if self.monitor == "missing":
addOptions['ignoreEpisodesWithFiles'] = True
addOptions['ignoreEpisodesWithoutFiles'] = False
addOptions['searchForMissingEpisodes'] = True
elif self.monitor == "future":
addOptions['ignoreEpisodesWithFiles'] = True
addOptions['ignoreEpisodesWithoutFiles'] = True
addOptions['searchForMissingEpisodes'] = False
data['title'] = media['title']
data['profileId'] = self.SONARR_QUALITY_PROFILE_ID
data['titleSlug'] = media['titleSlug']
data['images'] = media['images']
data['seasons'] = media['seasons']
data['imdbId'] = media['imdbId']
data['tvdbId'] = media['tvdbId']
data['seasonFolder'] = True
data['rootFolderPath'] = self.SONARR_DOWNLOAD_PATH
data['addOptions'] = addOptions
data['year'] = media['year']
data['cast'] = self.get_cast(data['imdbId'])
return data
def prepare_barebone_show_json(self, tvdbId, title):
data = {}
data['title'] = title
data['profileId'] = -1
data['titleSlug'] = 0
data['images'] = 0
data['seasons'] = 0
data['imdbId'] = ""
data['tvdbId'] = tvdbId
data['seasonFolder'] = 0
data['rootFolderPath'] = 0
data['year'] = 0
data['cast'] = ""
return data
def add_show(self, data):
r = requests.post(self.SONARR_SERVER+"/api/series?apikey="+self.SONARR_API,json.dumps(data))
if r.status_code == 201:
if str(data['cast']) == "":
self.tts_google("I added the tv show "+str(data['title'])+" "+str(data['year'])+" to your list.")
else:
self.tts_google("I added the tv show "+str(data['title'])+" "+str(data['year'])+" with, "+str(data['cast'])+" to your list.")
show = r.json()
with open(self.HASS_SCRIPTS_PATH+"/last_tvshow_download_added.txt", "w") as myfile:
myfile.write("show:"+str(show['id'])+"\n")
else:
res = self.is_show_already_added(data)
if res >= 0:
if res == 0:
self.tts_google("I found your tv show but I was not able to add it to your list.")
else:
if str(data['cast']) == "":
self.tts_google("The tv show, "+str(data['title'])+" "+str(data['year'])+" is already in your list.")
else:
self.tts_google("The tv show, "+str(data['title'])+" "+str(data['year'])+" with, "+str(data['cast'])+" is already in your list.")
else:
self.tts_google("Something wrong occured when trying to add the tv show to your list.")
def is_show_already_added(self, data):
# print("http://"+self.SONARR_SERVER+"/api/movie?apikey="+self.SONARR_API)
r = requests.get(self.SONARR_SERVER+"/api/series?apikey="+self.SONARR_API)
found = False
# print(data['tvdbId'])
# print(r.status_code)
if r.status_code == 200:
media_list = r.json()
# print(media_list)
if len(media_list) > 0:
i = 0
while i < len(media_list) and found == False:
tvdbId = media_list[i]['tvdbId']
if tvdbId == data['tvdbId']:
found = True
break;
i += 1
return 1 if found == True else 0
else:
return -1
def get_cast(self, imdbId):
if self.OMDB_API:
r = requests.get("http://www.omdbapi.com/?i="+str(imdbId)+"&apikey="+self.OMDB_API)
if r.status_code == requests.codes.ok:
movie = r.json()
cast = movie['Actors'].split(',')
if len(cast) > 0:
if len(cast) > 1:
return(cast[0]+" and"+cast[1])
else:
if cast[0] == "N/A":
return("")
else:
return(cast[0])
else:
return("")
else:
return("")
else:
return("")
# TVDB_API
# r = requests.get("https://api.thetvdb.com/series/"+str(tvdbId)+"/actors")
# if r.status_code == requests.codes.ok:
# series = r.json()
# data = series['data']
# if len(data) > 1:
# return(data[0]['name']+" et "+data[1]['name'])
# else:
# return(data[0]['name'])
# else:
# return("")
# POSSIBLE FUTURE FEATURE IF TVDB_API BECOMES FREE
# def get_actor_id(self, actor_name):
# r = requests.get("https://api.thetvdb.com/3/search/person?language=en-US&page=1&include_adult=false&api_key="+TVDB_API+"&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_show(self, actor_id, year):
# latest_years = [ year+1, year, year-1, year-2]
# i = 0
# shows = []
# while i < len(latest_years) and len(show) < 5:
# r = requests.get("https://api.thetvdb.com/3/discover/series?language=en-US&page=1&sort_by=release_date.desc&include_adult=false&include_video=false&page=1&primary_release_year=&api_key="+TVDB_API+"&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 show in results['results']:
# if len(show) < 5:
# data = self.prepare_barebone_show_json(int(show["id"]), show["title"])
# show.append(data)
# i += 1
# return(show)
def save_options_found_and_compose_msg(self, show):
msg=""
with open(self.HASS_SCRIPTS_PATH+"/download_tvshow_options.txt", "w") as myfile:
json.dump(show, myfile)
i = 0
if len(show) > 1:
msg = "I found, "+str(len(show))+" options.\n"
else:
msg = "I found, "+str(len(show))+" option.\n"
while i < len(show):
m = show[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 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.SONARR_SERVER = config.get('Sonarr', 'server_url')
self.SONARR_API = config.get('Sonarr', 'api_key')
self.SONARR_DOWNLOAD_PATH = config.get('Sonarr', 'root_directory')
self.SONARR_QUALITY_PROFILE_ID = int(config.get('Sonarr', 'profile_id'))
self.OMDB_API = config.get('Services', 'omdb_api_key')
# print(self.HASS_SERVER)
# print(self.HASS_API)
# print(self.HASS_TOKEN)
# print(self.HASS_SCRIPTS_PATH)
# print(self.HASS_SPEAKER_ENTITY)
# print(self.HASS_TTS_SERVICE)
#
# print(self.SONARR_SERVER)
# print(self.SONARR_API)
# print(self.SONARR_DOWNLOAD_PATH)
# print(self.SONARR_QUALITY_PROFILE_ID)
#
# print(self.OMDB_API)
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.SONARR_SERVER:
error_messages.append('Sonarr url with port (usually :8989) must be defined')
if not self.SONARR_API:
error_messages.append('Sonarr API Key must be defined')
if not self.SONARR_DOWNLOAD_PATH:
error_messages.append('Sonarr root_directory also knwon as rootFolderPath must be defined')
if self.SONARR_QUALITY_PROFILE_ID == 0:
error_messages.append('Sonarr quality profile id must be defined. Default value is 4 (1080p)')
if not self.OMDB_API:
warning_messages.append("Warning. omdb_api_key (optional)(recommended) is not set. Your speaker's feedback will miss cast details for tvshows. http://www.omdbapi.com/apikey.aspx'")
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]
monitor = sys.argv[3]
print(query)
print(mode)
print(monitor)
downloader = ShowDownloader(query, int(mode), monitor)