-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplex_to_arr.py
167 lines (152 loc) · 6.33 KB
/
plex_to_arr.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
import requests
import xml.etree.ElementTree as ET
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
# Retrieve API keys from environment variables
PLEX_TOKEN = os.getenv("PLEX_TOKEN")
RADARR_API_KEY = os.getenv("RADARR_API_KEY")
SONARR_API_KEY = os.getenv("SONARR_API_KEY")
TMDB_API_KEY = os.getenv("TMDB_API_KEY")
RADARR_URL = "http://192.168.1.15:7878/api/v3"
SONARR_URL = "http://192.168.1.15:8989/api/v3"
RADARR_ROOT_FOLDER = "/config"
SONARR_ROOT_FOLDER = "/config"
# Language Profile ID for Sonarr
LANGUAGE_PROFILE = 1 # Adjust this value based on your Sonarr configuration
def get_quality_profile_id():
quality_profiles_url = f"{RADARR_URL}/qualityProfile?apikey={RADARR_API_KEY}"
response = requests.get(quality_profiles_url)
if response.status_code == 200:
quality_profiles = response.json()
for profile in quality_profiles:
if profile["name"] == "HD-1080p":
return profile["id"]
else:
print(f"Failed to retrieve quality profiles. Status Code: {response.status_code}")
return None
QUALITY_PROFILE = get_quality_profile_id()
def fetch_plex_watchlist():
print("Fetching Plex watchlist...")
plex_url = f"https://metadata.provider.plex.tv/library/sections/watchlist/all?X-Plex-Token={PLEX_TOKEN}"
response = requests.get(plex_url)
root = ET.fromstring(response.content)
return root.findall('Directory') + root.findall('Video')
def fetch_tmdb_id(title, media_type):
if media_type == "show":
search_url = f"https://api.themoviedb.org/3/search/tv?api_key={TMDB_API_KEY}&query={title}"
else:
search_url = f"https://api.themoviedb.org/3/search/movie?api_key={TMDB_API_KEY}&query={title}"
response = requests.get(search_url)
if response.status_code == 200:
results = response.json().get('results')
if results:
# Assuming the first result is the most relevant one
return results[0]['id']
else:
print(f"No TMDB ID found for {media_type} '{title}'")
return None
else:
print(f"Failed to retrieve TMDB ID for {media_type} '{title}'")
return None
def add_to_radarr(tmdb_id, title):
print(f"Adding movie '{title}' to Radarr...")
payload = {
"title": title,
"qualityProfileId": int(QUALITY_PROFILE),
"tmdbId": tmdb_id,
"rootFolderPath": RADARR_ROOT_FOLDER,
"monitored": True,
"addOptions": {
"searchForMovie": True
}
}
radarr_add_url = f"{RADARR_URL}/movie?apikey={RADARR_API_KEY}"
response = requests.post(radarr_add_url, json=payload)
if response.status_code == 201:
print(f"Added movie '{title}' to Radarr successfully.")
else:
try:
error_message = response.json()[0]['errorMessage']
print(f"Failed to add movie '{title}' to Radarr. Error: {error_message}")
except (KeyError, IndexError):
print(f"Failed to add movie '{title}' to Radarr. Status Code: {response.status_code}")
def add_to_sonarr(tmdb_id, title):
print(f"Adding series '{title}' to Sonarr...")
payload = {
"title": title,
"qualityProfileId": int(QUALITY_PROFILE),
"languageProfileId": int(LANGUAGE_PROFILE),
"tvdbId": tmdb_id,
"rootFolderPath": SONARR_ROOT_FOLDER,
"monitored": True,
"addOptions": {
"searchForMissingEpisodes": True
}
}
sonarr_add_url = f"{SONARR_URL}/series?apikey={SONARR_API_KEY}"
response = requests.post(sonarr_add_url, json=payload)
if response.status_code == 201:
print(f"Added series '{title}' to Sonarr successfully.")
else:
try:
error_message = response.json()[0]['errorMessage']
print(f"Failed to add series '{title}' to Sonarr. Error: {error_message}")
except (KeyError, IndexError):
print(f"Failed to add series '{title}' to Sonarr. Status Code: {response.status_code}")
def search_and_add_series(search_term):
search_url = f"{SONARR_URL}/series/lookup"
headers = {"X-Api-Key": SONARR_API_KEY}
params = {"term": search_term}
response = requests.get(search_url, headers=headers, params=params)
if response.status_code == 200:
results = response.json()
if results:
series = results[0] # Assuming the first search result is the desired series
series_id = series["tvdbId"]
add_series_url = f"{SONARR_URL}/series"
payload = {
"title": series["title"],
"qualityProfileId": int(QUALITY_PROFILE),
"languageProfileId": int(LANGUAGE_PROFILE),
"tvdbId": series_id,
"rootFolderPath": SONARR_ROOT_FOLDER,
"monitored": True,
"addOptions": {
"searchForMissingEpisodes": True
}
}
response = requests.post(add_series_url, headers=headers, json=payload)
if response.status_code == 201:
print(f"Added series '{series['title']}' to Sonarr successfully.")
else:
try:
error_message = response.json()[0]['errorMessage']
print(f"Failed to add series '{series['title']}' to Sonarr. Error: {error_message}")
except (KeyError, IndexError):
print(f"Failed to add series '{series['title']}' to Sonarr. Status Code: {response.status_code}")
else:
print("No series found for the search term.")
else:
print("Failed to perform series search.")
def main():
print("Starting script...")
watchlist = fetch_plex_watchlist()
print(f"Found {len(watchlist)} items in Plex watchlist")
print("Processing Plex watchlist...")
for item in watchlist:
title = item.get('title')
media_type = item.get('type')
if media_type == "movie":
tmdb_id = fetch_tmdb_id(title, media_type)
if tmdb_id is not None:
add_to_radarr(tmdb_id, title)
elif media_type == "show":
tmdb_id = fetch_tmdb_id(title, media_type)
if tmdb_id is not None:
search_and_add_series(title)
else:
print(f"Unknown media type found: {media_type}")
if __name__ == "__main__":
main()