Created
March 11, 2024 22:24
-
-
Save mackhankins/d4b1081e7bb92d51c6decaf1a43c6a97 to your computer and use it in GitHub Desktop.
Temporary Media Radarr
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
from pyarr.radarr import RadarrAPI | |
from datetime import datetime, timedelta | |
# Radarr API URL and API key | |
RADARR_URL = "http://radarr_url:port" | |
API_KEY = "your_api_key" | |
# Number of days to keep items before deletion | |
DAYS_TO_KEEP = 30 # Change this as needed | |
# Initialize Radarr API | |
radarr = RadarrAPI(RADARR_URL, API_KEY) | |
# Function to get the ID of the "temporary" tag | |
def get_temporary_tag_id(): | |
try: | |
# Get all tags from Radarr | |
all_tags = radarr.get_tag() | |
# Loop through tags to find the ID of the "temporary" tag | |
for tag in all_tags: | |
if tag.get("label", "").lower() == "temporary": | |
return tag.get("id") | |
# Return None if "temporary" tag is not found | |
return None | |
except Exception as e: | |
print("Error:", e) | |
return None | |
# Function to get titles, IDs, date added, and days until deletion for movies with the "temporary" tag | |
def get_temporary_movies_info(): | |
try: | |
# Get tag ID for "temporary" tag | |
temporary_tag_id = get_temporary_tag_id() | |
if temporary_tag_id is None: | |
print("Error: 'temporary' tag not found.") | |
return [] | |
# Get all movies from Radarr | |
all_movies = radarr.get_movie() | |
# Extract titles, IDs, date added, and days until deletion of movies with the "temporary" tag | |
temporary_movies_info = [] | |
for movie in all_movies: | |
title = movie.get("title") | |
tags = movie.get("tags", []) | |
if isinstance(tags, list) and temporary_tag_id in tags: | |
movie_id = movie.get("id") | |
# Convert 'added' field to datetime object | |
date_added = datetime.strptime(movie.get("added"), "%Y-%m-%dT%H:%M:%SZ").date() | |
days_until_deletion = (date_added + timedelta(days=DAYS_TO_KEEP) - datetime.now().date()).days | |
temporary_movies_info.append({"title": title, "movie_id": movie_id, "date_added": date_added, "days_until_deletion": days_until_deletion}) | |
return temporary_movies_info | |
except Exception as e: | |
print("Error:", e) | |
return [] | |
# Function to delete movies older than X days | |
def delete_old_movies(): | |
try: | |
# Get movies with the "temporary" tag | |
temporary_movies_info = get_temporary_movies_info() | |
# Delete movies older than X days | |
for movie_info in temporary_movies_info: | |
if movie_info["days_until_deletion"] <= 0: | |
# Delete movie | |
radarr.del_movie(movie_info["movie_id"]) | |
print(f"Deleted movie '{movie_info['title']}'") | |
except Exception as e: | |
print("Error:", e) | |
# Example usage | |
if __name__ == "__main__": | |
# Delete old movies | |
delete_old_movies() | |
# Print titles and days until deletion for movies with the "temporary" tag | |
temporary_movies_info = get_temporary_movies_info() | |
print("Movies with 'temporary' tag:") | |
for movie_info in temporary_movies_info: | |
print(f"Title: {movie_info['title']}, Date Added: {movie_info['date_added']}, Days Until Deletion: {movie_info['days_until_deletion']}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/totaldebug/pyarr