luciagomez commited on
Commit
d5082f6
·
verified ·
1 Parent(s): 0cd25f5

filename with url paths

Browse files
Files changed (1) hide show
  1. app.py +28 -2
app.py CHANGED
@@ -10,6 +10,31 @@ import os
10
  import gradio as gr
11
  from collections import deque
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # ===========================================================
14
  # 🌐 WEBSITE CRAWLER
15
  # ===========================================================
@@ -126,7 +151,7 @@ def extract_all_content_as_zip(url: str, max_links: int, max_depth: int) -> Tupl
126
  if title:
127
  markdown_text = f"# {title.text.strip()}\n\n{markdown_text}"
128
 
129
- filename = f"page_{i}.md"
130
  zip_file.writestr(filename, markdown_text)
131
  html_ok += 1
132
 
@@ -137,7 +162,8 @@ def extract_all_content_as_zip(url: str, max_links: int, max_depth: int) -> Tupl
137
  for j, pdf_url in enumerate(pdf_links, 1):
138
  try:
139
  resp = session.get(pdf_url, timeout=20)
140
- zip_file.writestr(f"pdfs/document_{j}.pdf", resp.content)
 
141
  pdf_ok += 1
142
  except Exception:
143
  pass
 
10
  import gradio as gr
11
  from collections import deque
12
 
13
+
14
+ # ===========================================================
15
+ # FORMATING FOR FILENAMES TO PRESERVE URL PATH
16
+ # ===========================================================
17
+
18
+
19
+ def url_to_zip_path(url, extension=".md"):
20
+ parsed = urlparse(url)
21
+
22
+ # example.com
23
+ domain = parsed.netloc.replace("www.", "")
24
+
25
+ # about/team/
26
+ path = parsed.path.strip("/")
27
+
28
+ if path == "":
29
+ # Homepage
30
+ return os.path.join(domain, "index" + extension)
31
+
32
+ # Remove extension if present
33
+ path = re.sub(r"\.[^.]+$", "", path)
34
+
35
+ return os.path.join(domain, path + extension)
36
+
37
+
38
  # ===========================================================
39
  # 🌐 WEBSITE CRAWLER
40
  # ===========================================================
 
151
  if title:
152
  markdown_text = f"# {title.text.strip()}\n\n{markdown_text}"
153
 
154
+ filename = url_to_zip_path(link_url, ".md")
155
  zip_file.writestr(filename, markdown_text)
156
  html_ok += 1
157
 
 
162
  for j, pdf_url in enumerate(pdf_links, 1):
163
  try:
164
  resp = session.get(pdf_url, timeout=20)
165
+ pdf_path = url_to_zip_path(pdf_url, ".pdf")
166
+ zip_file.writestr(pdf_path, resp.content)
167
  pdf_ok += 1
168
  except Exception:
169
  pass