-
Notifications
You must be signed in to change notification settings - Fork 0
/
housekeeper.py
executable file
·87 lines (66 loc) · 2.59 KB
/
housekeeper.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
#!/usr/bin/env python3
import configparser
from lib.sonarr import Sonarr
from lib.radarr import Radarr
from lib.tautulli import Tautulli
from lib.plex import Plex
def read_config():
config = configparser.ConfigParser()
config.read("config.ini")
return config
def main():
# Read config
config = read_config()
# Initialize Housekeeper
dry_run = config.getboolean("Housekeeper", "dry_run", fallback=True)
inactive_days = int(config.get("Housekeeper", "inactive_days"))
exclude_list = config.get("Housekeeper", "exclude_list", fallback="")
# Initialize Sonarr
sonarr_api_key = config.get("Sonarr", "api_key")
sonarr_base_url = config.get("Sonarr", "base_url")
sonarr = Sonarr(sonarr_api_key, sonarr_base_url)
# Initialize Radarr
radarr_api_key = config.get("Radarr", "api_key")
radarr_base_url = config.get("Radarr", "base_url")
radarr = Radarr(radarr_api_key, radarr_base_url)
# Initialize Tautulli
tautulli_api_key = config.get("Tautulli", "api_key")
tautulli_base_url = config.get("Tautulli", "base_url")
tautulli = Tautulli(tautulli_api_key, tautulli_base_url)
# Initialize Plex
plex_api_key = config.get("Plex", "token")
plex_base_url = config.get("Plex", "base_url")
plex = Plex(plex_api_key, plex_base_url)
# Get section IDs
section_ids = config.get("Plex", "section_ids").split(",")
# Generate an empty list to sort movies and show titles.
titles_full_list = []
for section_id in section_ids:
# Get library type
library_type = plex.get_library_type(section_id)
# Get IDs and titles for section.
titles = tautulli.get_titles(section_id, inactive_days, exclude_list)
# Add titles to full list.
for title in titles:
titles_full_list.append(title)
# If dry run, continue
if dry_run:
continue
if library_type == "movie":
# Get Radarr IDs for titles.
radarr_ids = radarr.get_ids(titles)
# Delete movies from Radarr.
radarr.delete_movies(radarr_ids)
elif library_type == "show":
# Get Sonarr IDs for titles.
sonarr_ids = sonarr.get_ids(titles)
# Delete shows from Sonarr.
sonarr.delete_shows(sonarr_ids)
# Sort and print full list if this is a dry_run
if dry_run:
titles_full_list.sort()
print(f"{len(titles_full_list)} items would be deleted due to {inactive_days} days of inactivity:")
for title in titles_full_list:
print(title)
if __name__ == "__main__":
main()