Advertisement
Najeebsk

Extrac-Urls.pyw

Jun 23rd, 2024
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog, messagebox, scrolledtext, ttk
  3. import re
  4.  
  5. def browse_file():
  6.     file_path = filedialog.askopenfilename(
  7.         filetypes=[("Text files", "*.txt"), ("All files", "*.*")]
  8.     )
  9.     if file_path:
  10.         entry_file_path.delete(0, tk.END)
  11.         entry_file_path.insert(0, file_path)
  12.         extract_urls(file_path)
  13.  
  14. def extract_urls(file_path):
  15.     try:
  16.         with open(file_path, 'r', encoding='utf-8') as file:
  17.             content = file.read()
  18.             urls = re.findall(r'(https?://\S+)', content)
  19.             display_urls(urls)
  20.     except Exception as e:
  21.         messagebox.showerror("Error", f"Failed to read file: {e}")
  22.  
  23. def display_urls(urls):
  24.     text_urls.config(state=tk.NORMAL)
  25.     text_urls.delete(1.0, tk.END)
  26.     for url in urls:
  27.         text_urls.insert(tk.END, url + "\n")
  28.     text_urls.config(state=tk.DISABLED)
  29.  
  30. def save_urls():
  31.     urls = text_urls.get(1.0, tk.END).strip().split('\n')
  32.     if urls:
  33.         try:
  34.             with open("urls.txt", 'w', encoding='utf-8') as file:
  35.                 for i, url in enumerate(urls):
  36.                     if url:
  37.                         channel_number = i + 1
  38.                         channel_name = f"channels{channel_number}"
  39.                         file.write(f"{channel_name} {url}\n")
  40.                 messagebox.showinfo("Success", "URLs saved successfully to urls.txt.")
  41.         except Exception as e:
  42.             messagebox.showerror("Error", f"Failed to save file: {e}")
  43.     else:
  44.         messagebox.showwarning("No URLs", "No URLs to save.")
  45.  
  46. # Create the main window
  47. root = tk.Tk()
  48. root.title("URL Extractor")
  49.  
  50. # Apply style
  51. style = ttk.Style()
  52. style.theme_use('clam')
  53. style.configure("TFrame", background="#f0f0f0")
  54. style.configure("TLabel", background="#f0f0f0", foreground="#333")
  55. style.configure("TButton", background="#0052cc", foreground="white")
  56. style.map("TButton", background=[('active', '#003d99')])
  57.  
  58. # Create and place the widgets
  59. frame = ttk.Frame(root, padding=10)
  60. frame.pack(pady=10)
  61.  
  62. label_file_path = ttk.Label(frame, text="File Path:")
  63. label_file_path.grid(row=0, column=0, padx=5, pady=5)
  64.  
  65. entry_file_path = ttk.Entry(frame, width=50)
  66. entry_file_path.grid(row=0, column=1, padx=5, pady=5)
  67.  
  68. button_browse = ttk.Button(frame, text="Browse", command=browse_file)
  69. button_browse.grid(row=0, column=2, padx=5, pady=5)
  70.  
  71. button_save = ttk.Button(frame, text="Save URLs to File", command=save_urls)
  72. button_save.grid(row=1, column=1, pady=10)
  73.  
  74. text_urls = scrolledtext.ScrolledText(root, width=70, height=20, state=tk.DISABLED, bg="#e6f2ff")
  75. text_urls.pack(padx=10, pady=10)
  76.  
  77. # Start the GUI event loop
  78. root.mainloop()
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement