Najeebsk

EXTRAC-URLS4.0.pyw

Jul 10th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.51 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=[("M3U files", "*.m3u"), ("M3U8 files", "*.m3u8"), ("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_channels(file_path)
  14.  
  15. def extract_channels(file_path):
  16.     try:
  17.         with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
  18.             content = file.read()
  19.             pattern = re.compile(r'#EXTINF:-1,(.*?)\n(http.*?)\n')
  20.             channels = pattern.findall(content)
  21.             channels = [(re.sub(r'\s+', '', title), url) for title, url in channels]
  22.             display_channels(channels)
  23.     except Exception as e:
  24.         messagebox.showerror("Error", f"Failed to read file: {e}")
  25.  
  26. def display_channels(channels):
  27.     search_text = entry_search.get().lower()
  28.     text_urls.config(state=tk.NORMAL)
  29.     text_urls.delete(1.0, tk.END)
  30.     filtered_channels = [f"{title} {url}" for title, url in channels if search_text in title.lower() or search_text in url.lower()]
  31.     for channel in filtered_channels:
  32.         text_urls.insert(tk.END, channel + "\n")
  33.     text_urls.config(state=tk.NORMAL)
  34.  
  35. def remove_duplicates():
  36.     content = text_urls.get(1.0, tk.END).strip()
  37.     channels = content.split('\n')
  38.     unique_channels = list(dict.fromkeys(channels))
  39.     text_urls.delete(1.0, tk.END)
  40.     for channel in unique_channels:
  41.         text_urls.insert(tk.END, channel + "\n")
  42.  
  43. def save_channels():
  44.     channels = text_urls.get(1.0, tk.END).strip().split('\n')
  45.     if channels:
  46.         try:
  47.             with open("channels.txt", 'w', encoding='utf-8') as file:
  48.                 for channel in channels:
  49.                     if channel:
  50.                         file.write(channel + "\n")
  51.                 messagebox.showinfo("Success", "Channels saved successfully to channels.txt.")
  52.         except Exception as e:
  53.             messagebox.showerror("Error", f"Failed to save file: {e}")
  54.     else:
  55.         messagebox.showwarning("No Channels", "No channels to save.")
  56.  
  57. def open_vlc(event):
  58.     try:
  59.         index = text_urls.index(tk.CURRENT)
  60.         line_start = f"{index.split('.')[0]}.0"
  61.         line_end = f"{index.split('.')[0]}.end"
  62.         channel = text_urls.get(line_start, line_end).strip()
  63.         url = channel.split(' ', 1)[1]
  64.         if url:
  65.             vlc_path = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
  66.             subprocess.Popen([vlc_path, url])
  67.     except Exception as e:
  68.         messagebox.showerror("Error", f"Failed to open VLC: {e}")
  69.  
  70. def search_channels(event):
  71.     file_path = entry_file_path.get()
  72.     if file_path:
  73.         extract_channels(file_path)
  74.     else:
  75.         messagebox.showwarning("No File", "Please select a file first.")
  76.  
  77. # Create the main window
  78. root = tk.Tk()
  79. root.title("Najeeb Channel Extractor and Play VLC")
  80. root.configure(bg="#4a4a4a")
  81. root.geometry("740x680")
  82.  
  83. # Apply style
  84. style = ttk.Style()
  85. style.theme_use('clam')
  86. style.configure("TFrame", background="#f0f0f0")
  87. style.configure("TLabel", background="#f0f0f0", foreground="#333")
  88. style.configure("TButton", background="#0052cc", foreground="white")
  89. style.map("TButton", background=[('active', '#003d99')])
  90.  
  91. # Create and place the widgets
  92. frame = ttk.Frame(root, padding=10)
  93. frame.pack(pady=10)
  94.  
  95. label_file_path = ttk.Label(frame, text="File Path:")
  96. label_file_path.grid(row=0, column=0, padx=5, pady=5)
  97.  
  98. entry_file_path = ttk.Entry(frame, width=50)
  99. entry_file_path.grid(row=0, column=1, padx=5, pady=5)
  100.  
  101. button_browse = ttk.Button(frame, text="Browse", command=browse_file)
  102. button_browse.grid(row=0, column=2, padx=5, pady=5)
  103.  
  104. label_search = ttk.Label(frame, text="Search Channel:")
  105. label_search.grid(row=1, column=0, padx=5, pady=5)
  106.  
  107. entry_search = ttk.Entry(frame, width=50)
  108. entry_search.grid(row=1, column=1, padx=5, pady=5)
  109. entry_search.bind("<KeyRelease>", search_channels)
  110.  
  111. button_save = ttk.Button(frame, text="Save Channels to File", command=save_channels)
  112. button_save.grid(row=0, column=3, pady=10)
  113.  
  114. button_remove_duplicates = ttk.Button(frame, text="Remove Duplicates", command=remove_duplicates)
  115. button_remove_duplicates.grid(row=0, column=4, pady=10, padx=5)
  116.  
  117. text_urls = scrolledtext.ScrolledText(root, width=100, height=33, state=tk.NORMAL, bg="#e6f2ff")
  118. text_urls.pack(padx=10, pady=10)
  119. text_urls.bind("<Double-1>", open_vlc)
  120.  
  121. # Start the GUI event loop
  122. root.mainloop()
  123.  
Add Comment
Please, Sign In to add comment