import os import re import requests import frontmatter from bs4 import BeautifulSoup from urllib.parse import urlparse # --- Funzioni di Supporto --- HUGO_CONTENT_PATH = "content" MAX_LENGHT = 800 def get_instance_and_id(url): """ Estrae l'istanza (hostname) e un potenziale ID da un URL, basandosi su pattern comuni di Mastodon. Args: url (str): La stringa URL da analizzare. Returns: tuple: Una tupla contenente (istanza, id). Restituisce (None, None) se l'URL non è ben formato o se non è possibile estrarre un'istanza. """ parsed_url = urlparse(url) instance = parsed_url.netloc if parsed_url.netloc else None if not instance: return None, None path_segments = parsed_url.path.strip("/").split("/") # Logica per trovare l'ID basandosi sui pattern di Mastodon if len(path_segments) >= 2 and path_segments[0].startswith("@"): if len(path_segments) == 2: if path_segments[1].isdigit(): return instance, path_segments[1] else: return instance, path_segments[0] elif ( len(path_segments) > 2 and path_segments[1] == "statuses" and path_segments[2].isdigit() ): return instance, path_segments[2] elif len(path_segments) > 2 and path_segments[2].isdigit(): return instance, path_segments[2] elif ( len(path_segments) >= 3 and path_segments[0] == "web" and path_segments[1] == "statuses" and path_segments[2].isdigit() ): return instance, path_segments[2] elif ( len(path_segments) >= 4 and path_segments[0] == "users" and path_segments[2] == "statuses" and path_segments[3].isdigit() ): return instance, path_segments[3] if path_segments: if path_segments[-1].isdigit(): return instance, path_segments[-1] elif path_segments[0].startswith("@") and len(path_segments) == 1: return instance, path_segments[0] return instance, None # Nessun ID specifico trovato per URL di base o generici def get_page_content(url): """ Recupera il contenuto HTML di una pagina web dato il suo URL. Gestisce errori di rete e timeout. """ try: response = requests.get(url, timeout=10) response.raise_for_status() return response.text except requests.exceptions.RequestException as e: print(f"Errore nel recupero dell'URL {url}: {e}") return None def extract_preview_from_html(html_content, max_length=MAX_LENGHT): """ Estrae una porzione di testo pulita dal contenuto HTML per una preview. Prioritizza l'estrazione da: 1. Primo elemento con classe 'e-content'. 2. Se 'e-content' manca, il primo elemento con classe 'p-summary'. Se entrambi mancano, restituisce una stringa vuota. Rimuove il markup HTML, gli script e gli stili in modo robusto. """ if not html_content: return "" soup = BeautifulSoup(html_content, "html.parser") # Rimuove tutti i tag