Skip to content

Instantly share code, notes, and snippets.

@Javiink
Last active June 29, 2024 15:33
Show Gist options
  • Save Javiink/87ac3c07d157b9fd0969e271e04f312b to your computer and use it in GitHub Desktop.
Save Javiink/87ac3c07d157b9fd0969e271e04f312b to your computer and use it in GitHub Desktop.
A script that sends notifications to a Telegram chat containing media that Maintainerr will remove soon from your Plex

This script will send a message to a Telegram channel/group/supergroup containing the Plex media that will be removed soon by Maintainerr.

image

❗️ Be sure to customize the variables at the start of the file.

This script will connect to the Maintainerr API and retrieve the Collections created. If a Collection have items, the script will render a section on the Telegram message with the Collection title and a link to that Collection on your Plex server, followed by the titles of the contents that are included in that Collection with a link to watch them on Plex. If the Collection is empty or not active, it will be ignored.

You probably want to run this periodically through cron.

<?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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment