1
0
postauto/autopost/analyzer.php

131 lines
3.6 KiB
PHP
Raw Normal View History

2025-02-01 22:23:51 +01:00
<?php
function formatBytesnzb($size, $precision = 2) {
$base = log($size, 1024);
$suffixes = array('', 'Ko', 'Mo', 'Go', 'To');
return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
}
// Charger le fichier NZB
$nzbFilePath = $argv[1];
if (!file_exists($nzbFilePath)) {
die('Fichier NZB non trouvé.');
}
// Charger le contenu du fichier NZB
$xmlContent = file_get_contents($nzbFilePath);
if ($xmlContent === false) {
die('Impossible de lire le fichier NZB.');
}
// Vérifier si le fichier contient <meta type="password"> pour choisir la méthode d'analyse
if (strpos($xmlContent, '<meta type="password">') !== false) {
// Analyse via Regex (approche nzbanal.php)
$rename = '/&quot;(.*?).par2/';
preg_match($rename, $xmlContent, $matchesname);
$ngname = $matchesname[1];
if (empty($ngname)) {
$rename = '/subject="(.*?).par2/';
preg_match($rename, $xmlContent, $matchesname);
$ngname = $matchesname[1];
}
// Récupération du mot de passe
$repass = '/<meta type="password">(.*?)<\/meta>/';
preg_match($repass, $xmlContent, $matchespass);
$password = $matchespass[1];
// Récupération de la date
$redate = '/date="(.*?)"/';
preg_match($redate, $xmlContent, $matchesdate);
$update = $matchesdate[1];
$update = date('d/m/Y', $update);
// Calcul de la taille des fichiers RAR/ISO/MKV/MP4
$regexsize = '/(rar|iso|mkv|mp4).*\(\d+\/\d+\) (\d+)/';
preg_match_all($regexsize, $xmlContent, $matchessize, PREG_PATTERN_ORDER, 0);
$filesize = formatBytesnzb(array_sum($matchessize[2]));
// Extraction des groupes
$regroup = '/<group>(.*?)<\/group>/';
preg_match_all($regroup, $xmlContent, $matchesgroup, PREG_PATTERN_ORDER, 0);
$resultgroup = array_unique($matchesgroup[1]);
$GROUP = '';
$i = '0';
foreach ($resultgroup as $group) {
$GROUP .= $group;
$i++;
}
// Création de l'objet JSON pour cette approche
$myObj = new stdClass();
$myObj->Name = $ngname;
$myObj->Pass = $password;
$myObj->Date = $update;
$myObj->Groupes = $GROUP;
$myObj->Taille = $filesize;
$myObj->Taillebit = array_sum($matchessize[2]);
$myJSON = json_encode($myObj);
echo $myJSON;
} else {
// Analyse via XML (approche nzbanal_obs.php)
$password = 'Aucun';
$xml = @simplexml_load_string($xmlContent, null, 0, "http://www.newzbin.com/DTD/2003/nzb");
if ($xml === false) {
die('Erreur lors de l\'analyse du fichier NZB en tant que XML.');
}
$maxSegmentsFile = null;
$maxSegmentsCount = 0;
foreach ($xml->file as $file) {
$fileName = (string)$file->attributes()->subject;
$fileDate = (string)$file->attributes()->date;
$fileGroups = [];
$totalSize = 0;
$segmentCount = 0;
foreach ($file->groups->group as $group) {
$fileGroups[] = (string)$group;
}
foreach ($file->segments->segment as $segment) {
$segmentSize = (int)$segment->attributes()->bytes;
$totalSize += $segmentSize;
$segmentCount++;
}
if ($segmentCount > $maxSegmentsCount) {
$maxSegmentsCount = $segmentCount;
$maxSegmentsFile = [
'name' => $fileName,
'date' => $fileDate,
'groups' => $fileGroups,
'total_size' => $totalSize,
'segment_count' => $segmentCount
];
}
}
if ($maxSegmentsFile) {
$myObj = new stdClass();
$myObj->Name = $maxSegmentsFile['name'];
$myObj->Pass = $password;
$myObj->Date = date('Y-m-d H:i:s', (int)$maxSegmentsFile['date']);
$myObj->Groupes = implode(', ', $maxSegmentsFile['groups']);
$myObj->Taille = formatBytesnzb($maxSegmentsFile['total_size']);
$myObj->Taillebit = $maxSegmentsFile['total_size'];
$myJSON = json_encode($myObj);
echo $myJSON;
} else {
echo "Aucun fichier trouvé dans le NZB.
";
}
}
?>