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
- import subprocess
- def browse_file():
- file_path = filedialog.askopenfilename(
- filetypes=[("M3U files", "*.m3u"), ("M3U8 files", "*.m3u8"), ("All files", "*.*")]
- )
- if file_path:
- entry_file_path.delete(0, tk.END)
- entry_file_path.insert(0, file_path)
- extract_channels(file_path)
- def extract_channels(file_path):
- try:
- with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
- content = file.read()
- pattern = re.compile(r'#EXTINF:-1,(.*?)\n(http.*?)\n')
- channels = pattern.findall(content)
- channels = [(re.sub(r'\s+', '', title), url) for title, url in channels]
- display_channels(channels)
- except Exception as e:
- messagebox.showerror("Error", f"Failed to read file: {e}")
- def display_channels(channels):
- search_text = entry_search.get().lower()
- text_urls.config(state=tk.NORMAL)
- text_urls.delete(1.0, tk.END)
- filtered_channels = [f"{title} {url}" for title, url in channels if search_text in title.lower() or search_text in url.lower()]
- for channel in filtered_channels:
- text_urls.insert(tk.END, channel + "\n")
- text_urls.config(state=tk.NORMAL)
- def remove_duplicates():
- content = text_urls.get(1.0, tk.END).strip()
- channels = content.split('\n')
- unique_channels = list(dict.fromkeys(channels))
- text_urls.delete(1.0, tk.END)
- for channel in unique_channels:
- text_urls.insert(tk.END, channel + "\n")
- def save_channels():
- channels = text_urls.get(1.0, tk.END).strip().split('\n')
- if channels:
- try:
- with open("channels.txt", 'w', encoding='utf-8') as file:
- for channel in channels:
- if channel:
- file.write(channel + "\n")
- messagebox.showinfo("Success", "Channels saved successfully to channels.txt.")
- except Exception as e:
- messagebox.showerror("Error", f"Failed to save file: {e}")
- else:
- messagebox.showwarning("No Channels", "No channels to save.")
- def open_vlc(event):
- try:
- index = text_urls.index(tk.CURRENT)
- line_start = f"{index.split('.')[0]}.0"
- line_end = f"{index.split('.')[0]}.end"
- channel = text_urls.get(line_start, line_end).strip()
- url = channel.split(' ', 1)[1]
- if url:
- vlc_path = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
- subprocess.Popen([vlc_path, url])
- except Exception as e:
- messagebox.showerror("Error", f"Failed to open VLC: {e}")
- def search_channels(event):
- file_path = entry_file_path.get()
- if file_path:
- extract_channels(file_path)
- else:
- messagebox.showwarning("No File", "Please select a file first.")
- # Create the main window
- root = tk.Tk()
- root.title("Najeeb Channel Extractor and Play VLC")
- root.configure(bg="#4a4a4a")
- root.geometry("740x680")
- # 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)
- label_search = ttk.Label(frame, text="Search Channel:")
- label_search.grid(row=1, column=0, padx=5, pady=5)
- entry_search = ttk.Entry(frame, width=50)
- entry_search.grid(row=1, column=1, padx=5, pady=5)
- entry_search.bind("<KeyRelease>", search_channels)
- button_save = ttk.Button(frame, text="Save Channels to File", command=save_channels)
- button_save.grid(row=0, column=3, pady=10)
- button_remove_duplicates = ttk.Button(frame, text="Remove Duplicates", command=remove_duplicates)
- button_remove_duplicates.grid(row=0, column=4, pady=10, padx=5)
- text_urls = scrolledtext.ScrolledText(root, width=100, height=33, state=tk.NORMAL, bg="#e6f2ff")
- text_urls.pack(padx=10, pady=10)
- text_urls.bind("<Double-1>", open_vlc)
- # Start the GUI event loop
- root.mainloop()
Add Comment
Please, Sign In to add comment