Skip to content

Instantly share code, notes, and snippets.

@mackhankins
Created March 11, 2024 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mackhankins/55f1d34352032d8929e4f72b84981f89 to your computer and use it in GitHub Desktop.
Save mackhankins/55f1d34352032d8929e4f72b84981f89 to your computer and use it in GitHub Desktop.
Temporary Media Sonarr
from pyarr.sonarr import SonarrAPI
from datetime import datetime, timedelta
# Sonarr API URL and API key
SONARR_URL = "http://sonarr_url:port"
API_KEY = "your_api_key"
# Number of days to keep items before deletion
DAYS_TO_KEEP = 30 # Change this as needed
# Initialize Sonarr API
sonarr = SonarrAPI(SONARR_URL, API_KEY)
# Function to get titles, IDs, date added, and days until deletion for series with the "temporary" tag
def get_temporary_series_info():
try:
# Get all series from Sonarr
all_series = sonarr.get_series()
# Extract titles, IDs, date added, and days until deletion of series with the "temporary" tag
temporary_series_info = []
for series in all_series:
title = series.get("title")
tags = series.get("tags", [])
for tag_id in tags:
tag_info = sonarr.get_tag(tag_id)
if tag_info.get("label") == "temporary":
series_id = series.get("id")
date_added = datetime.strptime(series.get("added"), "%Y-%m-%dT%H:%M:%SZ").date()
days_until_deletion = (date_added + timedelta(days=DAYS_TO_KEEP) - datetime.now().date()).days
temporary_series_info.append({"title": title, "series_id": series_id, "date_added": date_added, "days_until_deletion": days_until_deletion})
break # Once found, no need to continue checking other tags
return temporary_series_info
except Exception as e:
print("Error:", e)
return []
# Function to delete series older than X days
def delete_old_series():
try:
# Get series with the "temporary" tag
temporary_series_info = get_temporary_series_info()
# Delete series older than X days
for series_info in temporary_series_info:
if series_info["days_until_deletion"] <= 0:
# Delete series
sonarr.del_series(series_info["series_id"])
print(f"Deleted series '{series_info['title']}'")
except Exception as e:
print("Error:", e)
# Example usage
if __name__ == "__main__":
# Delete old series
delete_old_series()
# Print titles and days until deletion for series with the "temporary" tag
temporary_series_info = get_temporary_series_info()
print("Series with 'temporary' tag:")
for series_info in temporary_series_info:
print(f"Title: {series_info['title']}, Date Added: {series_info['date_added']}, Days Until Deletion: {series_info['days_until_deletion']}")
@mackhankins
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment