Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| from collections import defaultdict | |
| import os | |
| # --- Le nostre funzioni di logica (non cambiano!) --- | |
| def parse_all_rules(lines): | |
| structured_rules = [] | |
| current_conditions = [] | |
| for line in lines: | |
| line = line.strip() | |
| if not line: continue | |
| if line.startswith('RewriteCond'): | |
| current_conditions.append(line) | |
| elif line.startswith('RewriteRule'): | |
| parts = line.split() | |
| source_url = parts[1] if len(parts) > 1 else "" | |
| destination_url = parts[2] if len(parts) > 2 else "" | |
| full_block_text = "\n".join(current_conditions + [line]) | |
| structured_rules.append({ | |
| "full_block": full_block_text, | |
| "source": source_url, | |
| "destination": destination_url | |
| }) | |
| current_conditions = [] | |
| return structured_rules | |
| def find_duplicate_sources(structured_rules): | |
| source_to_destinations = defaultdict(set) | |
| for rule in structured_rules: | |
| if rule["source"]: | |
| source_to_destinations[rule["source"]].add(rule["destination"]) | |
| duplicate_sources = { | |
| source for source, destinations in source_to_destinations.items() | |
| if len(destinations) > 1 | |
| } | |
| return duplicate_sources | |
| # --- La funzione principale per l'interfaccia Gradio --- | |
| def analyze_and_create_report(uploaded_file): | |
| if uploaded_file is None: | |
| return ( | |
| pd.DataFrame(), # Tabella anteprima vuota | |
| None, # Nessun file da scaricare | |
| "Nessun file caricato. Per favore, carica un file .txt" # Messaggio di stato | |
| ) | |
| # Leggi le righe dal file temporaneo caricato da Gradio | |
| with open(uploaded_file.name, 'r', encoding='utf-8') as f: | |
| lines = f.readlines() | |
| # 1. Analizza e struttura TUTTE le regole | |
| all_rules = parse_all_rules(lines) | |
| # 2. Identifica gli URL di partenza duplicati | |
| duplicate_sources = find_duplicate_sources(all_rules) | |
| # 3. Prepara i dati per l'output | |
| output_data = [] | |
| for rule in all_rules: | |
| if rule["source"] in duplicate_sources: | |
| output_data.append({ | |
| "Rewrite Rule Originale": rule["full_block"], | |
| "URL di Partenza (Isolato)": rule["source"], | |
| "URL di Destinazione (Isolato)": rule["destination"] | |
| }) | |
| else: | |
| output_data.append({ | |
| "Rewrite Rule Originale": rule["full_block"], | |
| "URL di Partenza (Isolato)": "", | |
| "URL di Destinazione (Isolato)": "" | |
| }) | |
| if not output_data: | |
| return pd.DataFrame(), None, "Il file sembra vuoto o non contiene regole valide." | |
| # 4. Crea un DataFrame pandas e un file CSV | |
| df = pd.DataFrame(output_data) | |
| # Crea un file CSV temporaneo per il download | |
| output_filename = "report_duplicati.csv" | |
| df.to_csv(output_filename, index=False, sep=';') | |
| # Prepara l'anteprima (solo duplicati) e messaggio di stato | |
| preview_df = df[df["URL di Partenza (Isolato)"] != ""] | |
| if len(duplicate_sources) > 0: | |
| status_message = f"✅ Analisi completata. Trovati {len(duplicate_sources)} URL di partenza con destinazioni multiple. Scarica il report completo." | |
| else: | |
| status_message = "✅ Analisi completata. Nessuna regola duplicata trovata! Il report CSV conterrà tutte le regole originali." | |
| # Restituisce l'anteprima, il percorso del file da scaricare e il messaggio di stato | |
| return preview_df, output_filename, status_message | |
| # --- Costruzione dell'interfaccia Gradio --- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 📄 Generatore Report CSV per Rewrite Rules Duplicate") | |
| gr.Markdown( | |
| "Carica un file .txt per analizzare le Rewrite Rules. " | |
| "Il tool genererà un file CSV scaricabile con i risultati." | |
| ) | |
| with gr.Row(): | |
| file_input = gr.File(label="Carica il tuo file .txt", file_types=[".txt"]) | |
| analyze_button = gr.Button("Analizza File", variant="primary") | |
| status_output = gr.Textbox(label="Stato dell'Analisi") | |
| gr.Markdown("### Anteprima delle Regole Duplicate Trovate") | |
| preview_output = gr.DataFrame(headers=["Rewrite Rule Originale", "URL di Partenza (Isolato)", "URL di Destinazione (Isolato)"]) | |
| gr.Markdown("### Scarica il Report Completo") | |
| file_output = gr.File(label="Clicca sull'icona di download per scaricare il file CSV") | |
| analyze_button.click( | |
| fn=analyze_and_create_report, | |
| inputs=file_input, | |
| outputs=[preview_output, file_output, status_output] | |
| ) | |
| # --- Avvio dell'applicazione --- | |
| if __name__ == "__main__": | |
| # Aggiungi share=True per creare un link pubblico e temporaneo | |
| demo.launch(share=True) |