forked from ASK-ME-ABOUT-LOOM/purgeomatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.movie.whitelist.py
137 lines (110 loc) · 4.27 KB
/
create.movie.whitelist.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
import config
import sys
import requests
import jq
# Define the command-line arguments
c = config.Config()
if not c.check("tautulliAPIkey", "radarrAPIkey"):
print("ERROR: Required Tautulli/Radarr API key not set. Cannot continue.")
sys.exit(1)
# check if the API keys are valid
c.apicheck(c.radarrHost, c.radarrAPIkey)
# function to get users from tautulli
def get_users():
users = requests.get(
f"{c.tautulliHost}/api/v2/?apikey={c.tautulliAPIkey}&cmd=get_users"
)
return (users)
# function to get user_ids from users
def get_user_id(users):
# Define the jq filter to extract user_id
filter_expr = jq.compile('.response.data[].user_id')
# Apply filter to data
user_ids = [user_id for user_id in filter_expr.input(users.json()).all()]
return (user_ids)
# function to get playlists from tautulli
def get_playlists(user_id):
playlists = requests.get(
f"{c.tautulliHost}/api/v2?apikey={c.tautulliAPIkey}&cmd=get_playlists_table§ion_id=1&user_id={user_id}"
)
return (playlists)
# function to get playlist rating_key from playlists
def get_playlist_rating_key(data):
# Extract the 'data' field from the response
playlist_data = data['response']['data']['data']
# Search for the playlist with the specified title
for playlist in playlist_data:
if playlist['title'] == c.protectedPlaylistName:
return playlist['ratingKey']
# Return None if the playlist is not found
return(None)
# function to get playlist content from tautulli
def get_playlist_content(rkey):
movielist = requests.get(
f"{c.tautulliHost}/api/v2?apikey={c.tautulliAPIkey}&cmd=get_children_metadata&rating_key={rkey}&media_type=playlist"
)
return(movielist)
# function to pick out the rating_keys from the movielist and return them
def get_rating_keys(movielist):
rating_keys = [movie['rating_key']
for movie in movielist['response']['data']['children_list']]
return(rating_keys)
# fucntion to get movie info from tautulli
def get_movie_info(rkey):
movie = requests.get(
f"{c.tautulliHost}/api/v2?apikey={c.tautulliAPIkey}&cmd=get_metadata&rating_key={rkey}"
)
return(movie)
# function to get guids from movie info
def get_movie_guids(movie):
guids = jq.compile(".[].data.guids").input(movie.json()).first()
return(guids)
# function to pick out the tmdbid from the guids
def get_tmdbid(guids):
tmdbid = None
try:
if guids:
tmdbid = [i for i in guids if i.startswith("tmdb://")][0].split(
"tmdb://", 1
)[1]
else:
raise ValueError("Empty guids")
except Exception as e:
print(
f"WARNING: Unexpected GUID metadata from Tautulli. Please refresh your library's metadata in Plex. Using less-accurate 'search mode' for this title. Error message: "
+ str(e)
)
tmdbid = None
return(tmdbid)
# function to append tmdbid to a file
def write_tmdbid(tmdbid):
with open("/app/protected", "a") as file:
file.write(tmdbid + "\n")
# function to call all the above functions
def main():
users = get_users()
user_ids = get_user_id(users)
# Loop for each user
for user_id in user_ids:
try:
user_playlists = get_playlists(user_id)
if user_playlists.status_code != 200:
raise ValueError(
f"Failed to get playlists for user {user_id}. Status code: {user_playlists.status_code}")
elif user_playlists.json()['response']['result'] != 'success':
raise ValueError(
f"Failed to get playlists for user {user_id}. Response: {user_playlists.json()['response']['message']}")
rkey = get_playlist_rating_key(user_playlists.json())
movielist = get_playlist_content(rkey)
rating_keys = get_rating_keys(movielist.json())
write_tmdbid("########### " + str(user_id) + " ###########")
for rating_key in rating_keys:
movie = get_movie_info(rating_key)
guids = get_movie_guids(movie)
tmdbid = get_tmdbid(guids)
write_tmdbid(tmdbid)
except Exception as e:
print(e)
# Call the main function
if __name__ == "__main__":
main()