tolto locale che non serve più

This commit is contained in:
Alessandro Battilani
2026-01-15 10:00:11 +01:00
parent 7282352401
commit 7f3e047aff

View File

@@ -1,36 +1,13 @@
import win32com.client import win32com.client
import os
import hashlib
from datetime import datetime, timedelta from datetime import datetime, timedelta
from tqdm import tqdm from tqdm import tqdm
import time import time
import locale
# Imposta la localizzazione in italiano
try:
locale.setlocale(locale.LC_TIME, "it_IT.UTF-8") # Per Windows/Linux moderni
except: # noqa: E722
try:
locale.setlocale(locale.LC_TIME, "ita_ita") # Specifica per Windows
except: # noqa: E722
print("Impossibile impostare il locale italiano, uso i nomi manuali.")
# --- CONFIGURAZIONE --- # --- CONFIGURAZIONE ---
ARCHIVE_NAME = "Archivio online - alessandro.battilani@intesasanpaolo.com" ARCHIVE_NAME = "Archivio online - alessandro.battilani@intesasanpaolo.com"
ONEDRIVE_PATH = r"C:\Users\U086304\OneDrive - Intesa SanPaolo\Allegati_Outlook"
if not os.path.exists(ONEDRIVE_PATH):
os.makedirs(ONEDRIVE_PATH)
print(f"Cartella creata: {ONEDRIVE_PATH}")
MONTHS_LIMIT = 3 MONTHS_LIMIT = 3
# ---------------------- # ----------------------
def get_file_hash(file_path):
hasher = hashlib.md5()
with open(file_path, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
return hasher.hexdigest()
def mostra_cartelle(folder_object, indent=""): def mostra_cartelle(folder_object, indent=""):
for folder in folder_object.Folders: for folder in folder_object.Folders:
print(f"{indent}{folder.Name}") print(f"{indent}{folder.Name}")
@@ -38,9 +15,6 @@ def mostra_cartelle(folder_object, indent=""):
mostra_cartelle(folder, indent + " ") mostra_cartelle(folder, indent + " ")
def main(): def main():
if not os.path.exists(ONEDRIVE_PATH):
os.makedirs(ONEDRIVE_PATH)
print("Connessione a Outlook in corso...") print("Connessione a Outlook in corso...")
try: try:
outlook = win32com.client.Dispatch("Outlook.Application") outlook = win32com.client.Dispatch("Outlook.Application")
@@ -49,7 +23,7 @@ def main():
# Partendo dalla radice dell'account predefinito # Partendo dalla radice dell'account predefinito
# root = namespace.GetDefaultFolder(6).Parent # root = namespace.GetDefaultFolder(6).Parent
# mostra_cartelle(root) # mostra_cartelle(root)
email_sent = namespace.GetDefaultFolder(5) email_sent = namespace.GetDefaultFolder()
inbox = namespace.GetDefaultFolder(6) inbox = namespace.GetDefaultFolder(6)
archive_root = namespace.Folders.Item(ARCHIVE_NAME) archive_root = namespace.Folders.Item(ARCHIVE_NAME)
except Exception as e: except Exception as e:
@@ -58,6 +32,9 @@ def main():
cutoff_date = datetime.now() - timedelta(days=MONTHS_LIMIT * 30) cutoff_date = datetime.now() - timedelta(days=MONTHS_LIMIT * 30)
filter_date_str = cutoff_date.strftime("%d/%m/%Y %H:%M") filter_date_str = cutoff_date.strftime("%d/%m/%Y %H:%M")
filter_str = f"[ReceivedTime] < '{filter_date_str}'" filter_str = f"[ReceivedTime] < '{filter_date_str}'"
items = inbox.Items.Restrict(filter_str) items = inbox.Items.Restrict(filter_str)
@@ -70,7 +47,6 @@ def main():
print(f"Trovate {total_items} email vecchie. Inizio archiviazione...") print(f"Trovate {total_items} email vecchie. Inizio archiviazione...")
processed_files = {}
archived_count = 0 archived_count = 0
mesi_it = {1: "Gennaio", 2: "Febbraio", 3: "Marzo", 4: "Aprile", 5: "Maggio", 6: "Giugno", mesi_it = {1: "Gennaio", 2: "Febbraio", 3: "Marzo", 4: "Aprile", 5: "Maggio", 6: "Giugno",
7: "Luglio", 8: "Agosto", 9: "Settembre", 10: "Ottobre", 11: "Novembre", 12: "Dicembre"} 7: "Luglio", 8: "Agosto", 9: "Settembre", 10: "Ottobre", 11: "Novembre", 12: "Dicembre"}
@@ -117,71 +93,6 @@ def main():
pbar.set_postfix(error="Move fallito") pbar.set_postfix(error="Move fallito")
continue continue
# --- GESTIONE ALLEGATI (SOLO FILE VERI) ---
if archived_item.Class == 43 and archived_item.Attachments.Count > 0:
has_changed = False
attachments = [archived_item.Attachments.Item(j) for j in range(1, archived_item.Attachments.Count + 1)]
for att in attachments:
try:
# 1. Filtro base: solo allegati di tipo "Valore" (file)
if att.Type != 1:
continue
# 2. FILTRO AVANZATO: Salta le immagini nelle firme (Inline Images)
# Controlliamo se l'allegato ha un "Content-ID" (tipico delle immagini incorporate)
try:
prop_accessor = att.PropertyAccessor
cid = prop_accessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E")
if cid: # Se ha un CID, è un'immagine nel testo/firma
continue
except:
# Se non riesce a leggere la proprietà, procediamo con cautela
# (spesso i file veri non hanno questa proprietà)
pass
# 3. FILTRO ESTENSIONI: Opzionale, se vuoi escludere png/jpg a prescindere
# ext = os.path.splitext(att.FileName)[1].lower()
# if ext in ['.png', '.jpg', '.jpeg', '.gif']: continue
# --- Procedura di salvataggio con DATA nel nome ---
temp_path = os.path.join(os.environ['TEMP'], att.FileName)
att.SaveAsFile(temp_path)
f_hash = get_file_hash(temp_path)
# Creiamo il prefisso con la data (es. 2025-02-09_)
date_prefix = received_time.strftime("%Y-%m-%d")
if f_hash not in processed_files:
# Il nome file sarà: DATA_HASH_NOMEORIGINALE.ext
# Usiamo l'hash corto (primi 6 caratteri) per non avere nomi infiniti
unique_name = f"{date_prefix}_{f_hash[:6]}_{att.FileName}"
dest_path = os.path.join(ONEDRIVE_PATH, unique_name)
if not os.path.exists(dest_path):
os.replace(temp_path, dest_path)
processed_files[f_hash] = dest_path
else:
dest_path = processed_files[f_hash]
os.remove(temp_path)
# Il link nella mail punterà al nuovo nome con la data
link_html = (
f"<div style='border:1px solid #ccc; padding:8px; margin:10px 0; "
f"background-color:#f3f3f3; font-family:Arial; font-size:12px;'>"
f"<b>📎 Allegato spostato su OneDrive ({date_prefix}):</b><br>"
f"<a href='file:///{dest_path}'>{att.FileName}</a></div>"
)
archived_item.HTMLBody = link_html + archived_item.HTMLBody
att.Delete()
has_changed = True
except Exception as e:
continue
if has_changed:
archived_item.Save()
archived_count += 1 archived_count += 1
pbar.set_postfix(archiviate=archived_count) pbar.set_postfix(archiviate=archived_count)