Update arrhist script
All checks were successful
Run nix flake check / flake-check (push) Successful in 1m44s
Periodic flake update / flake-update (push) Successful in 1m20s

This commit is contained in:
Torjus Håkestad 2024-11-20 21:09:57 +01:00
parent 9b497a5174
commit 4431e54740
Signed by: torjus
SSH Key Fingerprint: SHA256:KjAds8wHfD2mBYK2H815s/+ABcSdcIHUndwHEdSxml4

View File

@ -6,6 +6,7 @@ from datetime import datetime, date, timedelta
SECRET_DIR = "/home/torjus/.config/sops-nix/secrets"
def sonarr_url():
xdg_dir = os.environ["XDG_RUNTIME_DIR"]
if not xdg_dir:
@ -13,26 +14,31 @@ def sonarr_url():
with open(f"{SECRET_DIR}/sonarr_base_url") as f:
return f.read().strip()
def radarr_url():
with open(f"{SECRET_DIR}/radarr_base_url") as f:
return f.read().strip()
def make_header(api_key: str):
return {"X-Api-Key": api_key, "Accept": "application/json"}
def get_sonarr_key():
with open(f"{SECRET_DIR}/sonarr_api_key") as f:
return f.read().strip()
def get_radarr_key():
with open(f"{SECRET_DIR}/radarr_api_key") as f:
return f.read().strip()
def get_sonarr_history(since: datetime|None=None):
def get_sonarr_history(since: datetime | None = None):
api_key = get_sonarr_key()
if not since:
since = datetime.combine(date.today()-timedelta(days=1),datetime.min.time())
url = f"{sonarr_url()}/api/history/since"
since = datetime.combine(date.today() - timedelta(days=1), datetime.min.time())
url = f"{sonarr_url()}/api/v3/history/since"
url += f"?date={since.isoformat()}"
response = requests.get(url, headers=make_header(api_key))
response.raise_for_status()
@ -44,10 +50,11 @@ def get_sonarr_history(since: datetime|None=None):
items.append(item["sourceTitle"])
return items
def get_radarr_history(since: datetime|None=None):
def get_radarr_history(since: datetime | None = None):
api_key = get_radarr_key()
if not since:
since = datetime.combine(date.today()-timedelta(days=7),datetime.min.time())
since = datetime.combine(date.today() - timedelta(days=7), datetime.min.time())
url = f"{radarr_url()}/api/v3/history/since"
url += f"?date={since.isoformat()}"
response = requests.get(url, headers=make_header(api_key))
@ -60,12 +67,17 @@ def get_radarr_history(since: datetime|None=None):
items.append(item["sourceTitle"])
return items
if __name__ == "__main__":
sonarr_items = get_sonarr_history()
radarr_items = get_radarr_history()
output = {
"text": f"Son: {len(sonarr_items)}|Rad: {len(radarr_items)}",
"tooltip": "Radarr: \n" + "\n".join(radarr_items) + "\n" + "Sonarr: \n" + "\n".join(sonarr_items)
"tooltip": "Radarr: \n"
+ "\n".join(radarr_items)
+ "\n"
+ "Sonarr: \n"
+ "\n".join(sonarr_items),
}
print(json.dumps(output))