discoverylidarr/lidarr_helpers.py

128 lines
4.3 KiB
Python
Raw Permalink Normal View History

2025-06-25 15:03:26 +02:00
#lidarr_helpers.py
2025-06-23 13:22:12 +00:00
from config import (
LIDARR_URL,
LIDARR_API_KEY,
ROOT_FOLDER_PATH,
QUALITY_PROFILE_ID,
MONITORED,
MONITOR_NEW_ITEMS,
SEARCH_FOR_MISSING_ALBUMS
)
2025-06-23 10:31:25 +02:00
import requests
2025-06-23 12:27:49 +00:00
import json
2025-06-23 14:28:34 +02:00
# import requests
# import json
# from config import *
2025-06-23 10:31:25 +02:00
2025-06-23 14:09:29 +02:00
# def lidarr_api_add_artist(mbid):
# lookup_url = f"{LIDARR_URL}/api/v1/artist/lookup?term=mbid:{mbid}"
# headers = {"X-Api-Key": LIDARR_API_KEY, "Content-Type": "application/json"}
2025-06-23 14:28:34 +02:00
# try:
# res = requests.get(lookup_url, headers=headers)
# res.raise_for_status()
# artists = res.json()
# if not artists:
# print(f"[LIDARR DEBUG] Ingen artist hittades för MBID {mbid}")
# return False
# artist = artists[0]
# print(f"[LIDARR DEBUG] Artist hittad: {artist['artistName']} (MBID: {mbid})")
# # Bygg payload från lookup-objektet
# payload = artist.copy()
# payload.update({
# "monitored": True,
# "qualityProfileId": QUALITY_PROFILE_ID,
# "metadataProfileId": 1,
# "rootFolderPath": ROOT_FOLDER_PATH,
# "addOptions": {
# "monitor": "all", # Bevakning av alla album
# "searchForMissingAlbums": True
# }
# })
# add_url = f"{LIDARR_URL}/api/v1/artist"
# print(f"[LIDARR DEBUG] Payload: {json.dumps(payload, indent=2)}")
# post_res = requests.post(add_url, headers=headers, json=payload)
# post_res.raise_for_status()
2025-06-23 14:09:29 +02:00
2025-06-23 14:28:34 +02:00
# print(f"[LIDARR DEBUG] Lyckades lägga till: {artist['artistName']}")
# return True
2025-06-23 14:09:29 +02:00
2025-06-23 14:28:34 +02:00
# except requests.exceptions.HTTPError as http_err:
# print(f"[LIDARR ERROR] HTTP-fel vid tillägg av MBID {mbid}: {http_err}")
# if http_err.response is not None:
# print(f"[LIDARR ERROR] Svar från servern: {http_err.response.text}")
# return False
# except Exception as e:
# print(f"[LIDARR ERROR] Annat fel vid tillägg av MBID {mbid}: {e}")
# return False
def lidarr_api_add_artist(mbid, verbose=False):
2025-06-23 10:31:25 +02:00
lookup_url = f"{LIDARR_URL}/api/v1/artist/lookup?term=mbid:{mbid}"
2025-06-23 14:28:34 +02:00
headers = {"X-Api-Key": LIDARR_API_KEY}
2025-06-23 12:27:49 +00:00
2025-06-23 14:09:29 +02:00
try:
res = requests.get(lookup_url, headers=headers)
res.raise_for_status()
2025-06-23 14:28:34 +02:00
data = res.json()
2025-06-23 10:31:25 +02:00
2025-06-23 14:28:34 +02:00
if not data:
if verbose:
print(f"[LIDARR] Ingen artist hittades för MBID {mbid}")
2025-06-23 14:09:29 +02:00
return False
2025-06-23 10:31:25 +02:00
2025-06-23 14:28:34 +02:00
artist = data[0]
2025-06-23 13:22:12 +00:00
#payload = {
# "foreignArtistId": mbid,
# "artistName": artist["artistName"],
# "monitored": True,
# "qualityProfileId": QUALITY_PROFILE_ID,
# "metadataProfileId": 1,
# "rootFolderPath": ROOT_FOLDER_PATH,
# "addOptions": {
# "monitor": "all",
# "searchForMissingAlbums": True
# }
2025-06-23 14:28:34 +02:00
payload = {
"foreignArtistId": mbid,
"artistName": artist["artistName"],
2025-06-23 13:22:12 +00:00
"monitored": MONITORED,
2025-06-23 12:27:49 +00:00
"qualityProfileId": QUALITY_PROFILE_ID,
2025-06-23 14:09:29 +02:00
"metadataProfileId": 1,
"rootFolderPath": ROOT_FOLDER_PATH,
"addOptions": {
2025-06-23 13:22:12 +00:00
"monitor": MONITOR_NEW_ITEMS,
"searchForMissingAlbums": SEARCH_FOR_MISSING_ALBUMS
2025-06-23 14:09:29 +02:00
}
2025-06-23 14:28:34 +02:00
}
if verbose:
print(f"[LIDARR] Försöker lägga till: {artist['artistName']}")
2025-06-23 10:31:25 +02:00
2025-06-23 14:09:29 +02:00
add_url = f"{LIDARR_URL}/api/v1/artist"
2025-06-23 14:28:34 +02:00
res = requests.post(add_url, headers=headers, json=payload)
if res.status_code == 400:
errors = res.json()
for err in errors:
if err.get("errorCode") == "ArtistExistsValidator":
print(f"[LIDARR] {artist['artistName']} finns redan.")
return False
if err.get("errorCode") == "RootFolderExistsValidator":
print(f"[LIDARR] Fel: Angiven rotmapp finns inte: {ROOT_FOLDER_PATH}")
return False
print(f"[LIDARR] Fel: {res.text}")
return False
2025-06-23 14:09:29 +02:00
2025-06-23 14:28:34 +02:00
res.raise_for_status()
print(f"[LIDARR] ✅ Tillagd: {artist['artistName']}")
2025-06-23 14:09:29 +02:00
return True
except Exception as e:
2025-06-23 14:28:34 +02:00
print(f"[LIDARR] ❌ Fel vid MBID {mbid}: {e}")
2025-06-23 14:09:29 +02:00
return False