Felguk commited on
Commit
a682e5d
Β·
verified Β·
1 Parent(s): f7d9daf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -31,9 +31,18 @@ def extract_additional_resources(url):
31
  except Exception as e:
32
  return [], [], []
33
 
 
 
 
 
 
 
 
 
 
34
  def convert_to_text(url):
35
  if not is_valid_url(url):
36
- return "Error: Please enter a valid URL.", "", None, [], [], [] # Return error message and empty data
37
 
38
  try:
39
  # Set headers to mimic a browser request
@@ -56,9 +65,13 @@ def convert_to_text(url):
56
  # Extract additional resources
57
  css_links, js_links, img_links = extract_additional_resources(url)
58
 
59
- return results, response.text, file_path, css_links, js_links, img_links
 
 
 
 
60
  except requests.exceptions.RequestException as e:
61
- return f"Error: {e}", "", None, [], [], [] # Return error message and empty data
62
 
63
  # HTML and JavaScript for the "Copy Code" button
64
  copy_button_html = """
@@ -98,7 +111,11 @@ with gr.Blocks(css=css) as demo:
98
  submit_button.click(
99
  fn=convert_to_text,
100
  inputs=url_input,
101
- outputs=[results_output, text_output, file_output, gr.Textbox(label="CSS Files"), gr.Textbox(label="JS Files"), gr.Textbox(label="Images")]
 
 
 
 
102
  )
103
 
104
  # Add an Accordion to show/hide additional resources
@@ -111,6 +128,12 @@ with gr.Blocks(css=css) as demo:
111
 
112
  gr.Markdown("### Images")
113
  img_output = gr.Textbox(label="Images", interactive=False)
 
 
 
 
 
 
114
 
115
  # Launch the interface
116
  demo.launch()
 
31
  except Exception as e:
32
  return [], [], []
33
 
34
+ def fetch_file_content(url):
35
+ """Fetches the content of a file (CSS, JS, etc.) from a URL."""
36
+ try:
37
+ response = requests.get(url)
38
+ response.raise_for_status()
39
+ return response.text
40
+ except:
41
+ return "Failed to fetch content."
42
+
43
  def convert_to_text(url):
44
  if not is_valid_url(url):
45
+ return "Error: Please enter a valid URL.", "", None, [], [], [], [], [] # Return error message and empty data
46
 
47
  try:
48
  # Set headers to mimic a browser request
 
65
  # Extract additional resources
66
  css_links, js_links, img_links = extract_additional_resources(url)
67
 
68
+ # Fetch CSS and JS content
69
+ css_content = [fetch_file_content(link) for link in css_links]
70
+ js_content = [fetch_file_content(link) for link in js_links]
71
+
72
+ return results, response.text, file_path, css_links, js_links, img_links, css_content, js_content
73
  except requests.exceptions.RequestException as e:
74
+ return f"Error: {e}", "", None, [], [], [], [], [] # Return error message and empty data
75
 
76
  # HTML and JavaScript for the "Copy Code" button
77
  copy_button_html = """
 
111
  submit_button.click(
112
  fn=convert_to_text,
113
  inputs=url_input,
114
+ outputs=[
115
+ results_output, text_output, file_output,
116
+ gr.Textbox(label="CSS Files"), gr.Textbox(label="JS Files"), gr.Textbox(label="Images"),
117
+ gr.Textbox(label="CSS Content"), gr.Textbox(label="JS Content")
118
+ ]
119
  )
120
 
121
  # Add an Accordion to show/hide additional resources
 
128
 
129
  gr.Markdown("### Images")
130
  img_output = gr.Textbox(label="Images", interactive=False)
131
+
132
+ gr.Markdown("### CSS Content")
133
+ css_content_output = gr.Textbox(label="CSS Content", interactive=True)
134
+
135
+ gr.Markdown("### JS Content")
136
+ js_content_output = gr.Textbox(label="JS Content", interactive=True)
137
 
138
  # Launch the interface
139
  demo.launch()