|
<?php |
|
|
|
// Customize this variables first! |
|
define('PLEX_IDENTIFIER', ''); // Your Plex server identifier. You can find this in the browser URL, it's a 40 characters alphanumeric string |
|
define('API_URL', ''); // Maintainerr API URL in format http://IP:PORT/api |
|
define('TG_BOT_TOKEN', ''); // Your bot token. Ask @BotFather on Telegram to obtain one |
|
define('TG_CHAT_ID', ''); // The Telegram chat ID where you want to send notifications to. You can find the ID adding https://t.me/myidbot to that group/supergroup/channel |
|
|
|
define('TG_URL', 'https://api.telegram.org/bot'); // Don't change this |
|
|
|
function getReq(string $url){ |
|
$curl = curl_init(); |
|
curl_setopt($curl, CURLOPT_URL, $url); |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
|
curl_setopt($curl, CURLOPT_HEADER, false); |
|
|
|
$data = curl_exec($curl); |
|
curl_close($curl); |
|
|
|
return json_decode($data); |
|
} |
|
|
|
function postReq(string $url, $postData){ |
|
$curl = curl_init(); |
|
curl_setopt($curl, CURLOPT_URL, $url); |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
|
curl_setopt($curl, CURLOPT_HEADER, false); |
|
curl_setopt($curl, CURLOPT_POST, 1); |
|
curl_setopt($curl, CURLOPT_POSTFIELDS, ($postData)); |
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
|
|
|
$data = curl_exec($curl); |
|
curl_close($curl); |
|
|
|
return json_decode($data); |
|
} |
|
|
|
function populateCollections($collectionsJson) { |
|
$collections = []; |
|
|
|
foreach ($collectionsJson as $cKey => $collection) { |
|
if (empty($collection->media) || $collection->isActive == false) |
|
continue; |
|
|
|
$collections[$cKey] = new stdClass; |
|
$collections[$cKey]->title = $collection->title; |
|
$collections[$cKey]->id = $collection->id; |
|
$collections[$cKey]->url = "https://app.plex.tv/#!/server/".PLEX_IDENTIFIER."/details?key=%2Flibrary%2Fcollections%2F$collection->plexId"; |
|
|
|
$colItemsJson = getReq(API_URL."/collections/media/$collection->id/content/1"); |
|
foreach ($colItemsJson->items as $iKey => $item) { |
|
$collections[$cKey]->items[$iKey] = new stdClass; |
|
$collections[$cKey]->items[$iKey]->title = $item->plexData->title; |
|
$plexId = $item->plexId; |
|
$collections[$cKey]->items[$iKey]->url = "https://app.plex.tv/#!/server/".PLEX_IDENTIFIER."/details?key=%2Flibrary%2Fmetadata%2F$plexId"; |
|
} |
|
} |
|
|
|
return $collections; |
|
} |
|
|
|
function renderTgMessage($collectionsData): string { |
|
if (empty($collectionsData)) |
|
return ''; |
|
|
|
$message = "⚠ <b>Contenido que nos deja pronto</b>\n\nDurante las próximas semanas, el siguiente contenido será eliminado para dejar espacio a nuevo contenido.\n<blockquote>❕ Si quieres ver algo, es el momento!</blockquote>\n\n"; |
|
foreach ($collectionsData as $colKey => $col) { |
|
$message.= "🔴 <b>$col->title</b> <a href='$col->url'>(Ver todo)</a>\n"; |
|
foreach ($col->items as $iKey => $item) { |
|
$message.= "▫ $item->title - <a href='$item->url'>Ver ahora</a>\n"; |
|
} |
|
$message.= "\n"; |
|
} |
|
return urldecode($message); |
|
} |
|
|
|
$collectionsJson = getReq(API_URL.'/collections'); |
|
$collections = populateCollections($collectionsJson); |
|
if (!empty($collections)){ |
|
$msg = renderTgMessage($collections); |
|
$tgParams = [ |
|
'chat_id' => TG_CHAT_ID, |
|
'parse_mode' => 'HTML', |
|
'text' => $msg, |
|
]; |
|
postReq(TG_URL.TG_BOT_TOKEN.'/sendMessage', $tgParams); |
|
} |