|
| 1 | +import threading |
| 2 | +import requests |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import filedialog, messagebox |
| 5 | +import ttkbootstrap as tb |
| 6 | +from ttkbootstrap.constants import * |
| 7 | + |
| 8 | +# ---------------- GLOBALS ---------------- # |
| 9 | +url_list = [] |
| 10 | + |
| 11 | +# ---------------- FUNCTIONS ---------------- # |
| 12 | +def add_urls(): |
| 13 | + """Add URLs from entry (comma separated)""" |
| 14 | + urls = url_entry.get().strip() |
| 15 | + if not urls: |
| 16 | + return |
| 17 | + for url in urls.split(","): |
| 18 | + url = url.strip() |
| 19 | + if url and url not in url_list: |
| 20 | + url_list.append(url) |
| 21 | + tree.insert("", tk.END, iid=url, values=(url, "Pending"), tags=("pending",)) |
| 22 | + url_entry.delete(0, tk.END) |
| 23 | + |
| 24 | +def remove_selected(): |
| 25 | + """Remove selected URLs from Treeview and list""" |
| 26 | + selected = tree.selection() |
| 27 | + for url in selected: |
| 28 | + url_list.remove(url) |
| 29 | + tree.delete(url) |
| 30 | + |
| 31 | +def check_status(url): |
| 32 | + """Check HTTP status and update Treeview row""" |
| 33 | + try: |
| 34 | + response = requests.get(url, timeout=5) |
| 35 | + status_text = f"{response.status_code} {response.reason}" |
| 36 | + color_tag = "ok" if response.status_code == 200 else "error" |
| 37 | + except requests.exceptions.RequestException as e: |
| 38 | + status_text = f"Error: {str(e)}" |
| 39 | + color_tag = "error" |
| 40 | + # Update treeview row safely |
| 41 | + tree.item(url, values=(url, status_text), tags=(color_tag,)) |
| 42 | + |
| 43 | +def check_all_urls(): |
| 44 | + """Start threads to check all URLs""" |
| 45 | + for url in url_list: |
| 46 | + tree.item(url, values=(url, "Checking..."), tags=("checking",)) |
| 47 | + threading.Thread(target=check_status, args=(url,), daemon=True).start() |
| 48 | + |
| 49 | +def save_results(): |
| 50 | + """Save URL status results to a text file""" |
| 51 | + file_path = filedialog.asksaveasfilename( |
| 52 | + defaultextension=".txt", |
| 53 | + filetypes=[("Text Files", "*.txt")] |
| 54 | + ) |
| 55 | + if not file_path: |
| 56 | + return |
| 57 | + with open(file_path, "w") as f: |
| 58 | + for url in url_list: |
| 59 | + status = tree.item(url)["values"][1] |
| 60 | + f.write(f"{url} → {status}\n") |
| 61 | + messagebox.showinfo("Saved", f"Results saved to {file_path}") |
| 62 | + |
| 63 | +# ---------------- GUI ---------------- # |
| 64 | +app = tb.Window(themename="darkly", title="Live URL Status Checker", size=(800, 500)) |
| 65 | + |
| 66 | +# Input Frame |
| 67 | +input_frame = tb.Frame(app) |
| 68 | +input_frame.pack(fill=tk.X, padx=10, pady=10) |
| 69 | +url_entry = tb.Entry(input_frame, width=60) |
| 70 | +url_entry.pack(side=tk.LEFT, padx=5) |
| 71 | +tb.Button(input_frame, text="Add URL(s)", bootstyle="primary", command=add_urls).pack(side=tk.LEFT, padx=5) |
| 72 | +tb.Button(input_frame, text="Remove Selected", bootstyle="danger", command=remove_selected).pack(side=tk.LEFT, padx=5) |
| 73 | + |
| 74 | +# Treeview Frame |
| 75 | +tree_frame = tb.Frame(app) |
| 76 | +tree_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=5) |
| 77 | +tree = tb.Treeview(tree_frame, columns=("URL", "Status"), show="headings", height=15) |
| 78 | +tree.heading("URL", text="URL") |
| 79 | +tree.heading("Status", text="Status") |
| 80 | +tree.column("URL", width=500) |
| 81 | +tree.column("Status", width=200) |
| 82 | +tree.pack(fill=tk.BOTH, expand=True) |
| 83 | + |
| 84 | +# Tag colors |
| 85 | +tree.tag_configure("pending", background="#444444", foreground="white") |
| 86 | +tree.tag_configure("checking", background="#666666", foreground="yellow") |
| 87 | +tree.tag_configure("ok", background="#1abc9c", foreground="black") # greenish |
| 88 | +tree.tag_configure("error", background="#e74c3c", foreground="white") # red |
| 89 | + |
| 90 | +# Controls Frame |
| 91 | +control_frame = tb.Frame(app) |
| 92 | +control_frame.pack(pady=10) |
| 93 | +tb.Button(control_frame, text="Check Status", bootstyle="success", command=check_all_urls).pack(side=tk.LEFT, padx=5) |
| 94 | +tb.Button(control_frame, text="Save Results", bootstyle="info", command=save_results).pack(side=tk.LEFT, padx=5) |
| 95 | + |
| 96 | +app.mainloop() |
0 commit comments