-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_check.py
287 lines (220 loc) · 8.86 KB
/
stream_check.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
import os
from requests import get, put, delete
from tinydb import TinyDB, Query
import logging
logging.basicConfig(
filename="Stream_check.log", format="%(asctime)s %(message)s", filemode="w"
)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# Init Database document
db = TinyDB("streaming.json")
class Arr:
def __init__(self, apikey, host, media_type, remove=False):
self.apikey = apikey
self.host = host
self.media_type = media_type
self.remove = remove
self.url = f"http://{self.host}/api/v3/"
def get_media(self):
"""Get media from *arr service
Returns:
dict: returns dictionary from url request
"""
return get(url=f"{self.url}{self.media_type}?apikey={self.apikey}")
def start_monitor(self, ids):
"""Sets the monitor status to True
Args:
ids (list): Receives a list of media IDs from the *arr service
Returns:
tuple: Currently returns the tuple from the requests module
"""
data = {
"movieIds": ids,
"monitored": "true",
}
return put(url=f"{self.url}movie/editor?apikey={self.apikey}", data=data)
def stop_monitor(self, ids):
"""Sets the monitor status to False
Args:
data (list): Receives a list of media IDs from the *arr service
Returns:
tuple: Currently returns the tuple from the requests module
"""
data = {
"movieIds": ids,
"monitored": "false",
}
return put(url=f"{self.url}movie/editor?apikey={self.apikey}", data=data)
def remove_media(self, ids, media_type):
"""Removes media files and folder when config remove is True
Args:
ids (list): Receives a list of media IDs from the *arr service
"""
if self.remove:
if media_type == "movie":
for i in ids:
delete(url=f"{self.url}/moviefile/{i}?apikey={self.apikey}")
elif media_type == "series":
for i in ids:
episodes = get(
url=f"{self.url}episodeFile?seriesId={i}&apikey={self.apikey}"
)
for e in episodes.get("id"):
delete(url=f"{self.url}/episodeFile/{e}?apikey={self.apikey}")
return
class TMDB:
def __init__(self, apikey, country):
self.apikey = apikey
self.country = country
def get_services(self, media_type, media_id):
"""Gets rent,buy, and flatrate streaming providers for the media
Args:
media_type (str): Receives a string of movie or series
media_id (int): Receives an int of the media id for The Movie Database
Returns:
dict: Returns dictionary of the streaming options
"""
url = f"https://api.themoviedb.org/3/{media_type}/{media_id}/watch/providers?api_key={self.apikey}"
return self._parse_country(get(url=url))
def _parse_country(self, services):
"""Parses the services of the users country
Args:
services (dict): Receives a dict of the services for a media
Returns:
dict: Returns a dict of the users country streaming options
"""
if "results" in services:
if self.country in services.get("results").keys():
# return contry results if the country is found
return services.get("results").get(self.country)
else:
# return results if the country isn't found
# will not have key value to indicate it is
# on a streaming service
# TODO: find a better way to resolve issue
return services.get("results")
else:
# if media is not found return error
# TODO: find a better way to resolve issue
return services
class Config:
def __init__(self):
self.config = self._get_config()
self.user_services = self._get_user_services()
self.country = self._get_country()
# TODO: look at changing to .conf
def _get_config(self):
"""Opens and reads the config file
Returns:
dict: Returns a dict of all the key = value pairs
"""
config = {}
# read .conf in and create a dict based on key = value
with open("config.conf") as f:
for line in f.readlines():
a = line.strip().split("=")
config[a[0].strip()] = a[1].strip()
return config
def _get_user_services(self):
"""Get user's streaming subscriptions in config dict
Returns:
list: Returns a list of the users streaming subscriptions
"""
return [s.strip() for s in self.config.get("MY_SERVICES").split(",")]
def _get_country(self):
"""Get user's country in two letter format (US, UK, DE, etc)
Returns:
str: Returns string of country
"""
return self.config.get("COUNTRY")
def apikey(self, service):
"""Gets the API key for the service from the config
Args:
service (str): Receives the string of sonarr or radarr
Returns:
str: Returns string of API key
"""
return self.config.get(f"{service.upper()}_APIKEY")
def host(self, service):
"""Gets the host:port of the *arr service
Args:
service (str): Receives the string of sonarr or radarr
Returns:
str: Returns string of host:port for *arr service
"""
return self.config.get(f"{service.upper()}_HOST")
def parse_streaming(streaming_services, user_services):
"""Parse to find flatrate in streaming services and match on users subscriptions
Args:
streaming_services (dict): Dict of the media streaming options
user_services (list): List of user's streaming subscriptions
Returns:
list: Returns a list of all the streaming service matches for the users subscriptions
"""
streaming_on = []
# parse streaming providers excluding rent and buy for services
# Flatrate means its on a subscription service (Netflix, Hulu)
if "flatrate" in streaming_services.keys():
for serv in streaming_services.get("flatrate"):
if serv.get("provider_name") in user_services:
streaming_on.append(serv.get("provider_name"))
return streaming_on
def compare_streaming(service, subscriptions, media_type):
"""Sort through all the media and the streaming options matched with the users
streaming subscriptions and puts them into a list of IDs to set the monitor
status in the *arr service.
Args:
service (str): String of sonarr or radarr
subscriptions (list): List of user's streaming subscriptions
media_type (str): String of movie or series
Returns:
tuple: Returns a tuple of lists, one of IDs for matched streams and one without
"""
# List for IDs for *arr service monitor status
stop_monitor = []
start_monitor = []
# loop through media in *arr service and get streaming providers
# and update *arr monitor status and DB
for x in service.get_media():
# get streaming providers
streaming = parse_streaming(
tmdb.get_services(media_type, x.get("tmdbId")), subscriptions
)
# if provider is found update media dict
if streaming:
stop_monitor.append(x.get("id"))
x["streaming_on"] = streaming
else:
start_monitor.append(x.get("id"))
# insert/update entry into database
insert(x)
return stop_monitor, start_monitor
# Main function to init config and *arr services
def main():
# TODO: Might change this to use args when running
# init the config to get api keys
c = Config()
tmdb = TMDB(c.apikey("tmdb"), c.country)
sonarr = Arr(c.apikey("sonarr"), c.host("sonarr"), "series")
radarr = Arr(c.apikey("radarr"), c.host("radarr"), "movie")
# parse stream providers vs user services to find if on streaming service
stop_radarr, start_radarr = compare_streaming(radarr, c.user_services, "movie")
stop_sonarr, start_sonarr = compare_streaming(sonarr, c.user_services, "series")
# set Movies in Radarr monitor status
radarr.stop_monitor(ids=stop_radarr)
radarr.start_monitor(ids=start_radarr)
# set Series in Sonarr monitor status
sonarr.stop_monitor(ids=stop_sonarr)
sonarr.start_monitor(ids=start_sonarr)
# remove media file from radarr
radarr.remove_media(ids=stop_radarr)
# remove media file from sonarr
sonarr.remove_media(ids=stop_sonarr)
return
### Database functions ###
def insert(media):
Media = Query()
return db.upsert(media, Media.tmdbId == media.get("tmdbId"))
if __name__ == "__main__":
main()