-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
109 lines (88 loc) · 3.1 KB
/
Copy pathapp.py
File metadata and controls
109 lines (88 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
from urllib.parse import urlparse
import streamlit as st
from firecrawl import Firecrawl
from firecrawl.types import ScrapeOptions
st.set_page_config(
page_title="Firecrawl Scraper",
page_icon="🔥",
layout="wide",
)
def get_api_key() -> str | None:
try:
secret_key = st.secrets.get("FIRECRAWL_API_KEY")
if secret_key:
return secret_key
except Exception:
pass
return os.getenv("FIRECRAWL_API_KEY")
def is_valid_url(url: str) -> bool:
try:
parsed = urlparse(url)
return parsed.scheme in {"http", "https"} and bool(parsed.netloc)
except Exception:
return False
st.title("🔥 Firecrawl Scraper")
api_key = get_api_key()
if not api_key:
st.error("API key non configurata.")
st.stop()
mode = st.radio(
"Modalità",
options=["Scrape (una pagina)", "Crawl (più pagine)"],
horizontal=True,
)
url = st.text_input("URL", placeholder="https://example.com")
if mode == "Scrape (una pagina)":
if st.button("Estrai contenuto", type="primary"):
if not is_valid_url(url.strip()):
st.error("Inserisci un URL valido.")
st.stop()
with st.spinner("Firecrawl sta analizzando la pagina..."):
firecrawl = Firecrawl(api_key=api_key)
result = firecrawl.scrape(url.strip(), formats=["markdown"])
st.download_button(
"Scarica Markdown",
data=result.markdown or "",
file_name="pagina.md",
mime="text/markdown",
)
st.markdown(result.markdown or "Nessun contenuto trovato.")
else:
limit = st.slider(
"Numero massimo di pagine da crawlare",
min_value=1,
max_value=20,
value=5,
help="Ogni pagina consuma 1 credito Firecrawl. Parti con un valore basso.",
)
if st.button("Avvia crawl", type="primary"):
if not is_valid_url(url.strip()):
st.error("Inserisci un URL valido.")
st.stop()
with st.spinner(f"Crawling in corso (max {limit} pagine)..."):
firecrawl = Firecrawl(api_key=api_key)
crawl_result = firecrawl.crawl(
url.strip(),
limit=limit,
scrape_options=ScrapeOptions(formats=["markdown"]),
poll_interval=5,
)
pages = getattr(crawl_result, "data", []) or []
st.success(f"Trovate {len(pages)} pagine.")
for i, page in enumerate(pages, start=1):
page_url = getattr(page, "url", None) or getattr(
getattr(page, "metadata", None), "source_url", "URL non disponibile"
)
with st.expander(f"Pagina {i}: {page_url}"):
st.markdown(getattr(page, "markdown", "") or "Nessun contenuto.")
if pages:
combined = "\n\n---\n\n".join(
getattr(p, "markdown", "") or "" for p in pages
)
st.download_button(
"Scarica tutte le pagine (Markdown)",
data=combined,
file_name="crawl_risultato.md",
mime="text/markdown",
)