Skip to content

Instantly share code, notes, and snippets.

@mtongnz
Last active January 5, 2025 04:04
Show Gist options
  • Select an option

  • Save mtongnz/19e9c91289e4de7f8ff8deaa73acbbb9 to your computer and use it in GitHub Desktop.

Select an option

Save mtongnz/19e9c91289e4de7f8ff8deaa73acbbb9 to your computer and use it in GitHub Desktop.
This is a User Script for unRaid to interface with various *arr APIs. It will test (and thus re-activate) indexers and download clients in Prowlarr, Sonarr, and Radarr.
#!/usr/bin/php
<?php
#description=ARR Tester
// -- CONFIG -- //
// API Key mode:
// Determines if the API key is sent as part of the URL or in the request header
// either HEADER or URL
$API_KEY_MODE = "HEADER";
// Services
// name of each service is only used for displaying results so can be whatever
// - url: full API url (with http(s):// and port, but without trailing slash)
// - apikey: this is unique to each app. Find it in Settings->General
$services = [
'Prowlarr' =>
[
'url' => 'http://172.19.0.7:9696/api/v1',
'apikey' => 'api1234',
],
'Sonarr' =>
[
'url' => 'http://172.19.0.7:8989/api/v3',
'apikey' => 'api5678',
],
'Radarr' =>
[
'url' => 'http://172.19.0.7:7878/api/v3',
'apikey' => 'api0110',
],
];
// End Points
// name of each end point is for display of results only and can be whatever
// - method: POST, GET....
// - url: added to services.url to for the complete request url
$end_points = [
'indexers' =>
[
'method' => 'POST',
'url' => '/indexer/testall',
],
'download clients' =>
[
'method' => 'POST',
'url' => '/downloadclient/testall',
],
];
// -- END CONFIG -- //
$curl = curl_init();
foreach ($end_points as $desc => $ep_config) {
echo "\n\nTesting $desc...\n";
foreach ($services as $service => $s_config) {
echo " - $service: ";
$url = $s_config['url'] . $ep_config['url'];
$get_data = callAPI($ep_config['method'], $url, $s_config['apikey'], false);
$response = json_decode($get_data, true);
if (!is_array($response))
echo "$get_data\n";
else {
$failures = getValidationFailures($response);
if (count($failures) === 0)
echo "All " . count($response) . " $desc are working\n";
else
echo count($failures) . " of " . count($response) . " $desc have failed\n";
}
}
}
curl_close($curl);
echo "\n\nAll Done";
function callAPI($method, $url, $apikey, $data)
{
global $curl, $API_KEY_MODE;
try {
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
if ($API_KEY_MODE == "HEADER") {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-Api-Key: ' . $apikey,
'Content-Type: application/json',
'Accept: application/json',
));
} else {
$url = (str_contains($url, '?')) ? "$url&apikey=$apikey" : "$url?apikey=$apikey";
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
// EXECUTE:
$result = curl_exec($curl);
if(!$result)
return "Error: Connection Failure";
return $result;
} catch (Exception $e) {
return "Error: " . $e->getMessage();
}
}
function getValidationFailures($array)
{
$failures = [];
foreach ($array as $key => $val) {
if (count($val['validationFailures']) !== 0)
$failures[] = $val['id'];
}
return $failures;
}
?>
@mtongnz
Copy link
Author

mtongnz commented Jan 5, 2025

I have changed to this method: https://gist.github.com/mtongnz/a7d88f65a78699ca6c427058d376ae02
I find it's more reliable to restart everything before the issues arise.

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