2025-10-27 13:17:09 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Script de test standalone pour vérifier la correction d'encodage.
|
|
|
|
|
Utilisez ce script pour tester la correction AVANT de l'intégrer à NZBGet.
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
python3 test_fix_encoding.py /chemin/vers/dossier
|
|
|
|
|
python3 test_fix_encoding.py /chemin/vers/dossier --fix (pour vraiment renommer)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_encoding_issue(filename):
|
|
|
|
|
"""Détecte si un nom de fichier contient des problèmes d'encodage."""
|
|
|
|
|
patterns = [
|
2025-10-27 13:26:43 +00:00
|
|
|
'é', 'è', 'ê', 'ë', 'à ', 'Ã\xa0', 'â', 'ä', 'ç',
|
2025-10-27 13:17:09 +00:00
|
|
|
'ô', 'ö', 'ù', 'û', 'ü', 'î', 'ï', 'Å"',
|
2025-10-27 13:26:43 +00:00
|
|
|
'É', 'À', 'Â', 'È', 'Ê', 'ÃŽ', 'Ã"', 'Ù', 'Û', 'Ç'
|
2025-10-27 13:17:09 +00:00
|
|
|
]
|
2025-10-27 13:26:43 +00:00
|
|
|
|
|
|
|
|
if not any(pattern in filename for pattern in patterns):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# Double vérification : essaye de convertir
|
|
|
|
|
try:
|
|
|
|
|
fixed = filename.encode('iso-8859-1').decode('utf-8')
|
|
|
|
|
return fixed != filename
|
|
|
|
|
except (UnicodeDecodeError, UnicodeEncodeError):
|
|
|
|
|
return False
|
2025-10-27 13:17:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def fix_encoding(filename):
|
|
|
|
|
"""Corrige le nom de fichier."""
|
|
|
|
|
try:
|
|
|
|
|
fixed = filename.encode('iso-8859-1').decode('utf-8')
|
|
|
|
|
return fixed
|
|
|
|
|
except (UnicodeDecodeError, UnicodeEncodeError):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
|
print("Usage: python3 test_fix_encoding.py /chemin/vers/dossier [--fix]")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
directory = sys.argv[1]
|
|
|
|
|
do_fix = '--fix' in sys.argv
|
|
|
|
|
|
|
|
|
|
if not os.path.isdir(directory):
|
|
|
|
|
print(f"Erreur: Le répertoire '{directory}' n'existe pas")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
print(f"\n{'='*70}")
|
|
|
|
|
print(f"TEST DE CORRECTION D'ENCODAGE")
|
|
|
|
|
print(f"{'='*70}")
|
|
|
|
|
print(f"Répertoire: {directory}")
|
|
|
|
|
print(f"Mode: {'CORRECTION' if do_fix else 'SIMULATION'}")
|
|
|
|
|
print(f"{'='*70}\n")
|
|
|
|
|
|
|
|
|
|
files_with_issues = 0
|
|
|
|
|
files_corrected = 0
|
|
|
|
|
|
|
|
|
|
for dirpath, dirnames, filenames in os.walk(directory):
|
|
|
|
|
for filename in filenames:
|
|
|
|
|
if is_encoding_issue(filename):
|
|
|
|
|
files_with_issues += 1
|
|
|
|
|
fixed = fix_encoding(filename)
|
|
|
|
|
|
|
|
|
|
if fixed and fixed != filename:
|
|
|
|
|
rel_path = os.path.relpath(dirpath, directory)
|
|
|
|
|
if rel_path == '.':
|
|
|
|
|
rel_path = ''
|
|
|
|
|
|
|
|
|
|
print(f"\n📁 {rel_path if rel_path else '(racine)'}")
|
|
|
|
|
print(f" ❌ Avant : {filename}")
|
|
|
|
|
print(f" ✅ Après : {fixed}")
|
|
|
|
|
|
|
|
|
|
if do_fix:
|
|
|
|
|
old_path = os.path.join(dirpath, filename)
|
|
|
|
|
new_path = os.path.join(dirpath, fixed)
|
|
|
|
|
|
|
|
|
|
if os.path.exists(new_path):
|
|
|
|
|
print(f" ⚠️ ERREUR: Le fichier de destination existe déjà!")
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
os.rename(old_path, new_path)
|
|
|
|
|
print(f" ✔️ Renommé avec succès")
|
|
|
|
|
files_corrected += 1
|
|
|
|
|
except OSError as e:
|
|
|
|
|
print(f" ❌ ERREUR: {e}")
|
|
|
|
|
else:
|
|
|
|
|
files_corrected += 1
|
|
|
|
|
|
|
|
|
|
print(f"\n{'='*70}")
|
|
|
|
|
print(f"RÉSUMÉ")
|
|
|
|
|
print(f"{'='*70}")
|
|
|
|
|
print(f"Fichiers avec problèmes d'encodage: {files_with_issues}")
|
|
|
|
|
print(f"Fichiers {'corrigés' if do_fix else 'à corriger'}: {files_corrected}")
|
|
|
|
|
|
|
|
|
|
if not do_fix and files_corrected > 0:
|
|
|
|
|
print(f"\n💡 Pour appliquer les corrections, relancez avec: --fix")
|
|
|
|
|
elif do_fix and files_corrected > 0:
|
|
|
|
|
print(f"\n✅ Les fichiers ont été corrigés avec succès!")
|
|
|
|
|
elif files_corrected == 0:
|
|
|
|
|
print(f"\n✅ Aucun problème d'encodage détecté!")
|
|
|
|
|
|
|
|
|
|
print(f"{'='*70}\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|