Advertisement
Najeebsk

Extrac-Urls2.0.pyw

Jun 25th, 2024
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog, messagebox, scrolledtext, ttk
  3. import re
  4. import subprocess
  5.  
  6. def browse_file():
  7.     file_path = filedialog.askopenfilename(
  8.         filetypes=[("Text files", "*.txt"), ("All files", "*.*")]
  9.     )
  10.     if file_path:
  11.         entry_file_path.delete(0, tk.END)
  12.         entry_file_path.insert(0, file_path)
  13.         extract_urls(file_path)
  14.  
  15. def extract_urls(file_path):
  16.     try:
  17.         with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
  18.             content = file.read()
  19.             urls = re.findall(r'(https?://\S+)', content)
  20.             display_urls(urls)
  21.     except Exception as e:
  22.         messagebox.showerror("Error", f"Failed to read file: {e}")
  23.  
  24. def display_urls(urls):
  25.     text_urls.config(state=tk.NORMAL)
  26.     text_urls.delete(1.0, tk.END)
  27.     for url in urls:
  28.         text_urls.insert(tk.END, url + "\n")
  29.     text_urls.config(state=tk.NORMAL)
  30.  
  31. def remove_duplicates():
  32.     content = text_urls.get(1.0, tk.END).strip()
  33.     urls = content.split('\n')
  34.     unique_urls = list(dict.fromkeys(urls))
  35.     text_urls.delete(1.0, tk.END)
  36.     for url in unique_urls:
  37.         text_urls.insert(tk.END, url + "\n")
  38.  
  39. def save_urls():
  40.     urls = text_urls.get(1.0, tk.END).strip().split('\n')
  41.     channel_name_prefix = entry_channel_name.get().strip()
  42.     if urls:
  43.         try:
  44.             with open("urls.txt", 'w', encoding='utf-8') as file:
  45.                 for index, url in enumerate(urls):
  46.                     if url:
  47.                         channel_name = f"{channel_name_prefix}{index + 1}"
  48.                         file.write(f"{channel_name} {url}\n")
  49.                 messagebox.showinfo("Success", "URLs saved successfully to urls.txt.")
  50.         except Exception as e:
  51.             messagebox.showerror("Error", f"Failed to save file: {e}")
  52.     else:
  53.         messagebox.showwarning("No URLs", "No URLs to save.")
  54.  
  55. def open_vlc(event):
  56.     try:
  57.         index = text_urls.index(tk.CURRENT)
  58.         line_start = f"{index.split('.')[0]}.0"
  59.         line_end = f"{index.split('.')[0]}.end"
  60.         url = text_urls.get(line_start, line_end).strip()
  61.         if url:
  62.             vlc_path = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
  63.             subprocess.Popen([vlc_path, url])
  64.     except Exception as e:
  65.         messagebox.showerror("Error", f"Failed to open VLC: {e}")
  66.  
  67. # Create the main window
  68. root = tk.Tk()
  69. root.title("Najeeb URL Extractor and Play VLC")
  70. root.configure(bg="#4a4a4a")
  71. root.geometry("740x640")
  72.  
  73. # Apply style
  74. style = ttk.Style()
  75. style.theme_use('clam')
  76. style.configure("TFrame", background="#f0f0f0")
  77. style.configure("TLabel", background="#f0f0f0", foreground="#333")
  78. style.configure("TButton", background="#0052cc", foreground="white")
  79. style.map("TButton", background=[('active', '#003d99')])
  80.  
  81. # Create and place the widgets
  82. frame = ttk.Frame(root, padding=10)
  83. frame.pack(pady=10)
  84.  
  85. label_file_path = ttk.Label(frame, text="File Path:")
  86. label_file_path.grid(row=0, column=0, padx=5, pady=5)
  87.  
  88. entry_file_path = ttk.Entry(frame, width=50)
  89. entry_file_path.grid(row=0, column=1, padx=5, pady=5)
  90.  
  91. button_browse = ttk.Button(frame, text="Browse", command=browse_file)
  92. button_browse.grid(row=0, column=2, padx=5, pady=5)
  93.  
  94. label_channel_name = ttk.Label(frame, text="Channel Name Prefix:")
  95. label_channel_name.grid(row=1, column=0, padx=5, pady=5)
  96.  
  97. entry_channel_name = ttk.Entry(frame, width=50)
  98. entry_channel_name.grid(row=1, column=1, padx=5, pady=5)
  99.  
  100. button_save = ttk.Button(frame, text="Save URLs to File", command=save_urls)
  101. button_save.grid(row=1, column=2, pady=10)
  102.  
  103. button_remove_duplicates = ttk.Button(frame, text="Remove Duplicates", command=remove_duplicates)
  104. button_remove_duplicates.grid(row=1, column=3, pady=10, padx=5)
  105.  
  106. text_urls = scrolledtext.ScrolledText(root, width=100, height=33, state=tk.NORMAL, bg="#e6f2ff")
  107. text_urls.pack(padx=10, pady=10)
  108. text_urls.bind("<Double-1>", open_vlc)
  109.  
  110. # Start the GUI event loop
  111. root.mainloop()
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement