Last active
September 29, 2024 00:48
-
-
Save Archmonger/8575bc907595f84b344be05977968e95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""This script changes all Radarr movie folders to `{current_folder_name} {tmdb-xxx}` format | |
and removes characters that are illegal on Windows from the path.""" | |
import json | |
import pathlib | |
import re | |
import sys | |
from requests import request | |
ILLEGAL_CHARS = ':*?"<>|' | |
if __name__ == "__main__": | |
radarr_json = pathlib.Path("radarr.json") | |
if radarr_json.exists(): | |
with open(radarr_json, encoding="UTF-8") as f: | |
data = json.loads(f.read()) | |
api_url = data["api_url"].rstrip("/") | |
api_key = data["api_key"] | |
else: | |
print("Enter Radarr's URL: ", end="") | |
api_url = input().rstrip("/") | |
print("Enter Radarr's API Key: ", end="") | |
api_key = input() | |
yes_in_sys_argv: bool = len(sys.argv) > 1 and sys.argv[1] in {"-y", "--yes"} | |
response = request( | |
"GET", | |
f"{api_url}/api/v3/movie?apikey={api_key}", | |
timeout=30, | |
) | |
response.raise_for_status() | |
all_movies = response.json() | |
needs_path_update = {} | |
for movies in all_movies: | |
# Find movies that contain characters that are illegal in Windows paths | |
if any(char in movies["path"] for char in ILLEGAL_CHARS): | |
needs_path_update[movies["id"]] = movies | |
# Find movies that have a `tmdb` but the path does not contain `tmdb-` | |
elif movies["tmdbId"] and "tmdb-" not in movies["path"]: | |
needs_path_update[movies["id"]] = movies | |
if not needs_path_update: | |
print("No movies need their paths updated.") | |
sys.exit(0) | |
# Update the paths for the movies that need it using the movies editor API | |
for movies_id, movies in needs_path_update.items(): | |
# Remove trailing slashes | |
new_path: str = movies["path"].rstrip("/").rstrip("\\") | |
# Replace illegal characters | |
for char in ILLEGAL_CHARS: | |
new_path = new_path.replace(char, " ") | |
# If the path does not contain `tmdb-`, add it | |
if movies["tmdbId"] and "tmdb-" not in new_path: | |
new_path = f"{new_path} {{tmdb-{movies['tmdbId']}}}" | |
# Replace duplicate spaces with a single space | |
new_path = re.sub(r"\s+", " ", new_path) | |
# Wait for user input | |
print(f"Updating path for {movies['title']} to '{new_path}'") | |
if not yes_in_sys_argv: | |
print("Press any button to commit this change, or Ctrl+C to cancel...") | |
input() | |
# Commit the change | |
response = request( | |
"PUT", | |
f"{api_url}/api/v3/movie/{movies_id}?apikey={api_key}&moveFiles=true", | |
json={**movies, "path": new_path}, | |
timeout=30, | |
) | |
response.raise_for_status() | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment