binuser007 commited on
Commit
07c9187
·
verified ·
1 Parent(s): b9cfc73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -38
app.py CHANGED
@@ -2,41 +2,48 @@ import gradio as gr
2
  import subprocess
3
  import os
4
 
5
- def run_katana(url):
6
- katana_config_dir = "/tmp/katana"
7
- os.makedirs(katana_config_dir, exist_ok=True)
8
-
9
- config_file_path = os.path.join(katana_config_dir, "config.yaml")
10
- if not os.path.exists(config_file_path):
11
- with open(config_file_path, 'w') as f:
12
- f.write("# Dummy configuration\n")
13
-
14
- output_file = "/tmp/urls.txt"
15
- command = f"katana -config {config_file_path} -u {url} -o {output_file}"
16
-
17
- try:
18
- result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
19
- if os.path.exists(output_file):
20
- return output_file
21
- else:
22
- return f"Katana ran successfully, but no output file was created. Katana output: {result.stdout}"
23
- except subprocess.CalledProcessError as e:
24
- error_message = f"Error running katana: {e.stderr}"
25
- with open(output_file, 'w') as f:
26
- f.write(error_message)
27
- return output_file
28
-
29
- # Ensure the /tmp/flagged directory exists
30
- os.makedirs("/tmp/flagged", exist_ok=True)
31
-
32
- # Gradio interface
33
- interface = gr.Interface(
34
- fn=run_katana,
35
- inputs="text",
36
- outputs="file",
37
- title="Katana URL Crawler",
38
- description="Enter a URL to crawl with Katana.",
39
- flagging_dir="/tmp/flagged" # Specify a writable directory for flagging
40
- )
41
-
42
- interface.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
2
  import subprocess
3
  import os
4
 
5
+ def crawl_website(url):
6
+ """Crawls a website using Katana and saves the output to a text file."""
7
+
8
+ output_file = "katana_output.txt"
9
+ katana_command = [
10
+ "katana",
11
+ "-u", url,
12
+ "-o", output_file,
13
+ "-silent" # Only display output, no progress bars or logs
14
+ ]
15
+
16
+ # Execute Katana command
17
+ try:
18
+ subprocess.run(katana_command, check=True)
19
+ except subprocess.CalledProcessError as e:
20
+ return f"Error running Katana: {e}", None
21
+
22
+ # Read output file content
23
+ with open(output_file, "r") as f:
24
+ output_content = f.read()
25
+
26
+ return output_content, output_file
27
+
28
+ with gr.Blocks() as demo:
29
+ gr.Markdown("## Website Crawler using Katana")
30
+ with gr.Row():
31
+ url_input = gr.Textbox(label="Enter Website URL")
32
+ output_text = gr.Textbox(label="Crawled Endpoints", lines=15)
33
+ download_button = gr.Button("Download Output File")
34
+
35
+ # Trigger crawling on button click
36
+ url_input.submit(
37
+ fn=crawl_website,
38
+ inputs=url_input,
39
+ outputs=[output_text, download_button]
40
+ )
41
+
42
+ # Download output file
43
+ download_button.click(
44
+ fn=lambda file: file,
45
+ inputs=download_button,
46
+ outputs=gr.File(label="Download Results")
47
+ )
48
+
49
+ demo.launch()