2025-02-02 11:49:12 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
# Vérification de la présence d'un argument
|
|
|
|
|
if [ "$#" -lt 1 ]; then
|
|
|
|
|
echo "Usage: $0 fichier.nzb" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
nzbFilePath="$1"
|
|
|
|
|
if [ ! -f "$nzbFilePath" ]; then
|
2025-05-12 20:23:41 +02:00
|
|
|
echo "Fichier NZB indisponible." >&2
|
2025-02-02 11:49:12 +01:00
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Fonction de formatage de taille en octets (avec unités)
|
|
|
|
|
format_bytes() {
|
|
|
|
|
size="$1"
|
|
|
|
|
precision="${2:-2}"
|
|
|
|
|
if [ "$size" -eq 0 ]; then
|
|
|
|
|
echo "0"
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
# Calcul avec awk pour afficher la taille en Ko, Mo, Go, etc.
|
|
|
|
|
awk -v size="$size" -v prec="$precision" 'BEGIN {
|
|
|
|
|
suffixes[0] = "";
|
|
|
|
|
suffixes[1] = "Ko";
|
|
|
|
|
suffixes[2] = "Mo";
|
|
|
|
|
suffixes[3] = "Go";
|
|
|
|
|
suffixes[4] = "To";
|
|
|
|
|
base = log(size)/log(1024);
|
|
|
|
|
i = int(base);
|
|
|
|
|
value = exp((base - i)*log(1024));
|
|
|
|
|
format = "%." prec "f %s";
|
|
|
|
|
printf format, value, suffixes[i];
|
|
|
|
|
}'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Extraction du nom (ngname)
|
|
|
|
|
# On essaie d'abord avec le motif " ... .par2, puis avec subject=" ... .par2
|
|
|
|
|
ngname=$(grep -Po '"\K.*?(?=\.par2)' "$nzbFilePath" | head -n 1)
|
|
|
|
|
if [ -z "$ngname" ]; then
|
|
|
|
|
ngname=$(grep -Po 'subject="\K.*?(?=\.par2)' "$nzbFilePath" | head -n 1)
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Extraction du mot de passe
|
|
|
|
|
password=$(grep -Po '<meta type="password">\K.*?(?=</meta>)' "$nzbFilePath" | head -n 1)
|
|
|
|
|
|
|
|
|
|
# Extraction de la date (en supposant un timestamp Unix dans l'attribut date)
|
|
|
|
|
timestamp=$(grep -Po 'date="\K\d+' "$nzbFilePath" | head -n 1)
|
|
|
|
|
if [ -n "$timestamp" ]; then
|
|
|
|
|
# Format "YYYY-MM-DD HH:MM:SS"
|
|
|
|
|
update=$(date -d @"$timestamp" +"%Y-%m-%d %H:%M:%S")
|
|
|
|
|
else
|
|
|
|
|
update=""
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Calcul de la taille totale en additionnant les valeurs des attributs "bytes" de chaque balise <segment>
|
|
|
|
|
sizes=$(grep -Po '<segment bytes="\K\d+' "$nzbFilePath")
|
|
|
|
|
totalSize=0
|
|
|
|
|
if [ -n "$sizes" ]; then
|
|
|
|
|
totalSize=$(echo "$sizes" | awk '{sum+=$1} END {printf "%.0f", sum}')
|
|
|
|
|
fi
|
|
|
|
|
filesize=$(format_bytes "$totalSize")
|
|
|
|
|
|
2025-05-12 20:23:41 +02:00
|
|
|
# Extraction et concatenation des groupes (uniques)
|
2025-02-02 11:49:12 +01:00
|
|
|
groups=$(grep -Po '<group>\K.*?(?=</group>)' "$nzbFilePath")
|
|
|
|
|
GROUP=$(echo "$groups" | sort -u | paste -sd "" -)
|
|
|
|
|
|
|
|
|
|
# Création de l'objet JSON
|
|
|
|
|
# Attention : cette méthode simple avec printf ne gère pas l'échappement complet des caractères spéciaux
|
|
|
|
|
json=$(printf '{"Name":"%s", "Pass":"%s", "Date":"%s", "Groupes":"%s", "Taille":"%s", "Taillebit":%s}' \
|
|
|
|
|
"$ngname" "$password" "$update" "$GROUP" "$filesize" "$totalSize")
|
|
|
|
|
|
|
|
|
|
echo "$json"
|