cleanup category handling
This commit is contained in:
49
module.nix
49
module.nix
@@ -112,7 +112,13 @@ let
|
||||
syncCategories = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.int;
|
||||
default = [ ];
|
||||
description = "List of sync category IDs for the application.";
|
||||
description = ''
|
||||
List of Newznab category IDs to sync for this application.
|
||||
When empty (default), categories are auto-detected at runtime
|
||||
by querying Prowlarr's indexer/categories API endpoint and
|
||||
collecting all IDs under the parent category matching the
|
||||
implementation type (e.g. Sonarr -> TV, Radarr -> Movies).
|
||||
'';
|
||||
example = [
|
||||
5000
|
||||
5010
|
||||
@@ -291,6 +297,44 @@ let
|
||||
};
|
||||
};
|
||||
|
||||
# Map Servarr implementation names to their Newznab parent category names.
|
||||
# Used to auto-detect syncCategories from the Prowlarr API when not explicitly set.
|
||||
implementationCategoryMap = {
|
||||
Sonarr = "TV";
|
||||
Radarr = "Movies";
|
||||
Lidarr = "Audio";
|
||||
Readarr = "Books";
|
||||
Whisparr = "XXX";
|
||||
};
|
||||
|
||||
# Emit shell code that sets SYNC_CATEGORIES to a JSON array of category IDs.
|
||||
# When the user provides explicit IDs, use those. Otherwise, query the Prowlarr
|
||||
# /indexer/categories endpoint and collect the parent + all subcategory IDs for
|
||||
# the implementation's Newznab category.
|
||||
mkResolveSyncCategories =
|
||||
app:
|
||||
let
|
||||
hasExplicit = app.syncCategories != [ ];
|
||||
categoryName = implementationCategoryMap.${app.implementation} or null;
|
||||
in
|
||||
if hasExplicit then
|
||||
"SYNC_CATEGORIES=${lib.escapeShellArg (builtins.toJSON app.syncCategories)}"
|
||||
else if categoryName != null then
|
||||
''
|
||||
echo "Auto-detecting sync categories for ${app.implementation}..."
|
||||
ALL_CATEGORIES=$(${curl} -sf "$BASE_URL/indexer/categories" -H "X-Api-Key: $API_KEY")
|
||||
SYNC_CATEGORIES=$(echo "$ALL_CATEGORIES" | ${jq} --arg name ${lib.escapeShellArg categoryName} \
|
||||
'[.[] | select(.name == $name) | .id, .subCategories[].id]')
|
||||
if [ "$SYNC_CATEGORIES" = "[]" ] || [ -z "$SYNC_CATEGORIES" ]; then
|
||||
echo "Warning: could not auto-detect categories for '${categoryName}', using empty list" >&2
|
||||
SYNC_CATEGORIES='[]'
|
||||
else
|
||||
echo "Resolved sync categories: $SYNC_CATEGORIES"
|
||||
fi
|
||||
''
|
||||
else
|
||||
"SYNC_CATEGORIES='[]' ";
|
||||
|
||||
curl = "${pkgs.curl}/bin/curl";
|
||||
jq = "${pkgs.jq}/bin/jq";
|
||||
grep = "${pkgs.gnugrep}/bin/grep";
|
||||
@@ -353,6 +397,7 @@ let
|
||||
echo "Synced app '${app.name}' already exists, skipping"
|
||||
else
|
||||
echo "Adding synced app '${app.name}'..."
|
||||
${mkResolveSyncCategories app}
|
||||
PAYLOAD=$(${jq} -n \
|
||||
--arg name ${lib.escapeShellArg app.name} \
|
||||
--arg implementation ${lib.escapeShellArg app.implementation} \
|
||||
@@ -361,7 +406,7 @@ let
|
||||
--arg prowlarrUrl ${lib.escapeShellArg app.prowlarrUrl} \
|
||||
--arg baseUrl ${lib.escapeShellArg app.baseUrl} \
|
||||
--arg apiKey "$TARGET_API_KEY" \
|
||||
--argjson syncCategories ${builtins.toJSON app.syncCategories} \
|
||||
--argjson syncCategories "$SYNC_CATEGORIES" \
|
||||
'{
|
||||
name: $name,
|
||||
implementation: $implementation,
|
||||
|
||||
Reference in New Issue
Block a user