Advertisement
Najeebsk

IPTV-EXTRAC-URLS.pyw

Jun 21st, 2024
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 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.     if urls:
  42.         try:
  43.             with open("urls.txt", 'w', encoding='utf-8') as file:
  44.                 for index, url in enumerate(urls):
  45.                     if url:
  46.                         channel_name = f"Channel{index + 1}"
  47.                         file.write(f"{channel_name} {url}\n")
  48.                 messagebox.showinfo("Success", "URLs saved successfully to urls.txt.")
  49.         except Exception as e:
  50.             messagebox.showerror("Error", f"Failed to save file: {e}")
  51.     else:
  52.         messagebox.showwarning("No URLs", "No URLs to save.")
  53.  
  54. def open_vlc(event):
  55.     try:
  56.         index = text_urls.index(tk.CURRENT)
  57.         line_start = f"{index.split('.')[0]}.0"
  58.         line_end = f"{index.split('.')[0]}.end"
  59.         url = text_urls.get(line_start, line_end).strip()
  60.         if url:
  61.             vlc_path = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
  62.             subprocess.Popen([vlc_path, url])
  63.     except Exception as e:
  64.         messagebox.showerror("Error", f"Failed to open VLC: {e}")
  65.  
  66. # Create the main window
  67. root = tk.Tk()
  68. root.title("Najeeb URL Extractor and Play VLC")
  69. root.configure(bg="#4a4a4a")
  70. root.geometry("740x640")
  71.  
  72. # Apply style
  73. style = ttk.Style()
  74. style.theme_use('clam')
  75. style.configure("TFrame", background="#f0f0f0")
  76. style.configure("TLabel", background="#f0f0f0", foreground="#333")
  77. style.configure("TButton", background="#0052cc", foreground="white")
  78. style.map("TButton", background=[('active', '#003d99')])
  79.  
  80. # Create and place the widgets
  81. frame = ttk.Frame(root, padding=10)
  82. frame.pack(pady=10)
  83.  
  84. label_file_path = ttk.Label(frame, text="File Path:")
  85. label_file_path.grid(row=0, column=0, padx=5, pady=5)
  86.  
  87. entry_file_path = ttk.Entry(frame, width=50)
  88. entry_file_path.grid(row=0, column=1, padx=5, pady=5)
  89.  
  90. button_browse = ttk.Button(frame, text="Browse", command=browse_file)
  91. button_browse.grid(row=0, column=2, padx=5, pady=5)
  92.  
  93. button_save = ttk.Button(frame, text="Save URLs to File", command=save_urls)
  94. button_save.grid(row=0, column=3, pady=10)
  95.  
  96. button_remove_duplicates = ttk.Button(frame, text="Remove Duplicates", command=remove_duplicates)
  97. button_remove_duplicates.grid(row=0, column=4, pady=10, padx=5)
  98.  
  99. text_urls = scrolledtext.ScrolledText(root, width=100, height=33, state=tk.NORMAL, bg="#e6f2ff")
  100. text_urls.pack(padx=10, pady=10)
  101. text_urls.bind("<Double-1>", open_vlc)
  102.  
  103. # Start the GUI event loop
  104. root.mainloop()
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement