Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog, messagebox, scrolledtext, ttk
- import re
- def browse_file():
- file_path = filedialog.askopenfilename(
- filetypes=[("Text files", "*.txt"), ("All files", "*.*")]
- )
- if file_path:
- entry_file_path.delete(0, tk.END)
- entry_file_path.insert(0, file_path)
- extract_urls(file_path)
- def extract_urls(file_path):
- try:
- with open(file_path, 'r', encoding='utf-8') as file:
- content = file.read()
- urls = re.findall(r'(https?://\S+)', content)
- display_urls(urls)
- except Exception as e:
- messagebox.showerror("Error", f"Failed to read file: {e}")
- def display_urls(urls):
- text_urls.config(state=tk.NORMAL)
- text_urls.delete(1.0, tk.END)
- for url in urls:
- text_urls.insert(tk.END, url + "\n")
- text_urls.config(state=tk.DISABLED)
- def save_urls():
- urls = text_urls.get(1.0, tk.END).strip().split('\n')
- if urls:
- try:
- with open("urls.txt", 'w', encoding='utf-8') as file:
- for i, url in enumerate(urls):
- if url:
- channel_number = i + 1
- channel_name = f"channels{channel_number}"
- file.write(f"{channel_name} {url}\n")
- messagebox.showinfo("Success", "URLs saved successfully to urls.txt.")
- except Exception as e:
- messagebox.showerror("Error", f"Failed to save file: {e}")
- else:
- messagebox.showwarning("No URLs", "No URLs to save.")
- # Create the main window
- root = tk.Tk()
- root.title("URL Extractor")
- # Apply style
- style = ttk.Style()
- style.theme_use('clam')
- style.configure("TFrame", background="#f0f0f0")
- style.configure("TLabel", background="#f0f0f0", foreground="#333")
- style.configure("TButton", background="#0052cc", foreground="white")
- style.map("TButton", background=[('active', '#003d99')])
- # Create and place the widgets
- frame = ttk.Frame(root, padding=10)
- frame.pack(pady=10)
- label_file_path = ttk.Label(frame, text="File Path:")
- label_file_path.grid(row=0, column=0, padx=5, pady=5)
- entry_file_path = ttk.Entry(frame, width=50)
- entry_file_path.grid(row=0, column=1, padx=5, pady=5)
- button_browse = ttk.Button(frame, text="Browse", command=browse_file)
- button_browse.grid(row=0, column=2, padx=5, pady=5)
- button_save = ttk.Button(frame, text="Save URLs to File", command=save_urls)
- button_save.grid(row=1, column=1, pady=10)
- text_urls = scrolledtext.ScrolledText(root, width=70, height=20, state=tk.DISABLED, bg="#e6f2ff")
- text_urls.pack(padx=10, pady=10)
- # Start the GUI event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement