-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadvanced_clone_gui.py
More file actions
193 lines (158 loc) · 6.7 KB
/
Copy pathadvanced_clone_gui.py
File metadata and controls
193 lines (158 loc) · 6.7 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import re
import requests
from urllib.parse import urljoin, urlparse
from bs4 import BeautifulSoup
import tkinter as tk
from tkinter import ttk, filedialog, messagebox, scrolledtext
import threading
class WebClonerGUI:
def __init__(self, root):
self.root = root
self.root.title("Advanced Web Cloner")
self.root.geometry("800x600")
# Variabel GUI
self.target_url = tk.StringVar()
self.output_dir = tk.StringVar(value="cloned_site")
self.max_depth = tk.IntVar(value=2)
self.is_running = False
# Setup GUI
self.setup_gui()
def setup_gui(self):
# Frame input
input_frame = ttk.Frame(self.root, padding="10")
input_frame.grid(row=0, column=0, sticky="ew")
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(1, weight=1)
ttk.Label(input_frame, text="URL Target:").grid(row=0, column=0, sticky="w", pady=2)
ttk.Entry(input_frame, textvariable=self.target_url, width=60).grid(row=0, column=1, padx=5, pady=2)
ttk.Button(input_frame, text="Pilih Folder", command=self.select_output_dir).grid(row=0, column=2, padx=5)
ttk.Label(input_frame, text="Output Folder:").grid(row=1, column=0, sticky="w", pady=2)
ttk.Entry(input_frame, textvariable=self.output_dir, width=60).grid(row=1, column=1, padx=5, pady=2)
ttk.Label(input_frame, text="Max Kedalaman (rekursif):").grid(row=2, column=0, sticky="w", pady=2)
ttk.Spinbox(input_frame, from_=1, to=5, textvariable=self.max_depth, width=5).grid(row=2, column=1, sticky="w", padx=5, pady=2)
# Tombol
btn_frame = ttk.Frame(self.root, padding="10")
btn_frame.grid(row=1, column=0, sticky="nsew")
self.start_btn = ttk.Button(btn_frame, text="Mulai Clone", command=self.start_clone)
self.start_btn.grid(row=0, column=0, padx=5)
# Log area
self.log_text = scrolledtext.ScrolledText(btn_frame, wrap=tk.WORD, state='disabled')
self.log_text.grid(row=1, column=0, pady=10, sticky="nsew")
btn_frame.columnconfigure(0, weight=1)
btn_frame.rowconfigure(1, weight=1)
def select_output_dir(self):
folder = filedialog.askdirectory()
if folder:
self.output_dir.set(folder)
def log(self, msg):
self.log_text.config(state='normal')
self.log_text.insert(tk.END, msg + "\n")
self.log_text.see(tk.END)
self.log_text.config(state='disabled')
self.root.update_idletasks()
def is_valid(self, url):
parsed = urlparse(url)
return bool(parsed.netloc) and bool(parsed.scheme)
def get_all_links(self, soup, base_url):
links = set()
for a_tag in soup.find_all("a", href=True):
href = a_tag["href"]
full_url = urljoin(base_url, href)
if self.is_valid(full_url) and urlparse(full_url).netloc == urlparse(base_url).netloc:
links.add(full_url)
return links
def get_all_resources(self, soup, base_url):
resources = set()
# CSS
for link in soup.find_all("link", rel="stylesheet"):
href = link.get("href")
if href:
full_url = urljoin(base_url, href)
if self.is_valid(full_url):
resources.add(full_url)
# JS
for script in soup.find_all("script", src=True):
src = script["src"]
full_url = urljoin(base_url, src)
if self.is_valid(full_url):
resources.add(full_url)
# Images
for img in soup.find_all("img", src=True):
src = img["src"]
full_url = urljoin(base_url, src)
if self.is_valid(full_url):
resources.add(full_url)
return resources
def download_file(self, url, folder):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
# Buat path lokal
parsed = urlparse(url)
rel_path = parsed.path.lstrip("/")
if not rel_path or rel_path.endswith("/"):
rel_path = os.path.join(rel_path, "index.html")
full_path = os.path.join(folder, rel_path)
os.makedirs(os.path.dirname(full_path), exist_ok=True)
with open(full_path, "wb") as f:
f.write(response.content)
self.log(f"Downloaded: {url}")
except Exception as e:
self.log(f"Failed to download {url}: {e}")
def clone_page(self, url, folder, visited, depth, max_depth):
if depth > max_depth or url in visited:
return
visited.add(url)
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
# Simpan HTML
parsed = urlparse(url)
rel_path = parsed.path.lstrip("/")
if not rel_path or rel_path.endswith("/"):
rel_path = os.path.join(rel_path, "index.html")
full_path = os.path.join(folder, rel_path)
os.makedirs(os.path.dirname(full_path), exist_ok=True)
with open(full_path, "w", encoding="utf-8") as f:
f.write(str(soup))
self.log(f"Saved: {url}")
# Ambil resource
resources = self.get_all_resources(soup, url)
for res_url in resources:
self.download_file(res_url, folder)
# Ambil link untuk rekursif
if depth < max_depth:
links = self.get_all_links(soup, url)
for link in links:
self.clone_page(link, folder, visited, depth + 1, max_depth)
except Exception as e:
self.log(f"Error cloning {url}: {e}")
def start_clone(self):
if self.is_running:
return
self.is_running = True
self.start_btn.config(state='disabled')
threading.Thread(target=self.run_clone, daemon=True).start()
def run_clone(self):
try:
url = self.target_url.get().strip()
folder = self.output_dir.get()
max_depth = self.max_depth.get()
if not url:
messagebox.showerror("Error", "URL tidak boleh kosong!")
return
self.log("Mulai cloning...")
visited = set()
self.clone_page(url, folder, visited, 1, max_depth)
self.log("Cloning selesai!")
except Exception as e:
self.log(f"Error: {e}")
finally:
self.is_running = False
self.start_btn.config(state='normal')
if __name__ == "__main__":
root = tk.Tk()
app = WebClonerGUI(root)
root.mainloop()