forked from xe1gyq/Home-AssistantConfig-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hass_radarr_search_by_voice.py
177 lines (140 loc) · 6.51 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
import datetime
import requests
import json
import sys
import os
# User defined variables
# Home assistant URL eg. localhost:8123 with port
HASS_SERVER="10.3.1.2:8123"
# Home assistant api password
HASS_API="KkRiUybm1U6hBW7RHMN98ZZifQrBPADF7K3A6x7V"
# Home assistant scripts path eg. /users/vr/.homeassistant/scripts
HASS_SCRIPTS_PATH="/home/homeassistant/.homeassistant/scripts"
# Home assistant google home entity_id eg. media_player.family_room_speaker
HASS_GOOGLE_HOME_ENTITY="media_player.chromecast01"
RADARR_SERVER="10.3.1.2:7878" # with port
RADARR_API="6550be0baedd45c1873ba1d94dd7bf62"
RADARR_DOWNLOAD_PATH="/mnt/sdb2/new_content/sabnzb" # aka rootFolderPath
RADARR_QUALITY_PROFILE_ID=4 # 1080p
TMDBID_API="c8f99f055fce84770779145dbe390cde"
# ------------------------------------
class MovieDownloader:
'''
Constrctor
:param str movie: Title of the movie or option number if mode 2 is used
:param int mode: 0 | 1 | 2
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.
'''
def __init__(self, movie, mode=0):
year = datetime.datetime.now().year
term = movie
search_term = term.replace(" ", "%20")
current_years = [ year-1, year, year+1, year+2]
if mode != 2: # we are making a query
# query
r = requests.get("http://"+RADARR_SERVER+"/api/movie/lookup?apikey="+RADARR_API+"&term="+search_term)
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 recent movies 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_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.")
else: # search movie and give 3 options
# add to download_options file and read them out loud
i = 0
movies = []
while i < len(media_list) and i < 3:
data = self.prepare_movie_json(media_list[i])
movies.append(data)
i += 1
with open(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
self.tts_google(msg)
else:
self.tts_google("I didn't find any movie with that title.")
else:
self.tts_google("Something went wrong with the query.")
else:
# add to downloads from download_options file
download_option = int(movie)-1
with open(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]
data = self.prepare_movie_json(m)
self.add_movie(data)
else:
self.tts_google("There's no such option.")
def prepare_movie_json(self, media):
data = {}
data['title'] = media['title']
data['qualityProfileId'] = RADARR_QUALITY_PROFILE_ID
data['titleSlug'] = media['titleSlug']
data['images'] = media['images']
data['tmdbId'] = media['tmdbId']
data['rootFolderPath'] = RADARR_DOWNLOAD_PATH
data['monitored'] = True
data['minimumAvailability'] = 'released'
data['year'] = media['year']
data['cast'] = self.get_cast(data['tmdbId'])
return data
def add_movie(self, data):
r = requests.post("http://"+RADARR_SERVER+"/api/movie?apikey="+RADARR_API,json.dumps(data))
if r.status_code == 201:
self.tts_google("I added the movie "+str(data['title'])+" with "+str(data['cast'])+" to your download list.")
movie = r.json()
with open(HASS_SCRIPTS_PATH+"/last_download_added.txt", "w") as myfile:
myfile.write("movie:"+str(movie['id'])+"\n")
else:
self.tts_google("I found a movie, but I wasn't able to add it to your download list. It's possible I already added it.")
def get_cast(self, movieId):
r = requests.get("https://api.themoviedb.org/3/movie/"+str(movieId)+"/credits?api_key="+TMDBID_API)
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:
return("")
def tts_google(self, msg):
data = {"entity_id": HASS_GOOGLE_HOME_ENTITY, "message": msg}
r = requests.post("http://"+HASS_SERVER+"/api/services/tts/google_say?api_password="+HASS_API,json.dumps(data))
# print(msg)
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(query, int(mode))