import tkinter as tk
from tkinter import filedialog, messagebox, scrolledtext, ttk
import re
import subprocess
def browse_file():
file_path = filedialog.askopenfilename(
filetypes=[("Text files", "*.txt"), ("All files", "*.*")]
)
if file_path:
entry_file_path.delete(0, tk.END)
entry_file_path.insert(0, file_path)
extract_urls(file_path)
def extract_urls(file_path):
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
content = file.read()
urls = re.findall(r'(https?://\S+)', content)
display_urls(urls)
except Exception as e:
messagebox.showerror("Error", f"Failed to read file: {e}")
def display_urls(urls):
text_urls.config(state=tk.NORMAL)
text_urls.delete(1.0, tk.END)
for url in urls:
text_urls.insert(tk.END, url + "\n")
text_urls.config(state=tk.NORMAL)
def remove_duplicates():
content = text_urls.get(1.0, tk.END).strip()
urls = content.split('\n')
unique_urls = list(dict.fromkeys(urls))
text_urls.delete(1.0, tk.END)
for url in unique_urls:
text_urls.insert(tk.END, url + "\n")
def save_urls():
urls = text_urls.get(1.0, tk.END).strip().split('\n')
if urls:
try:
with open("urls.txt", 'w', encoding='utf-8') as file:
for index, url in enumerate(urls):
if url:
channel_name = f"Channel{index + 1}"
file.write(f"{channel_name} {url}\n")
messagebox.showinfo("Success", "URLs saved successfully to urls.txt.")
except Exception as e:
messagebox.showerror("Error", f"Failed to save file: {e}")
else:
messagebox.showwarning("No URLs", "No URLs 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"
url = text_urls.get(line_start, line_end).strip()
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}")
# Create the main window
root = tk.Tk()
root.title("Najeeb URL Extractor and Play VLC")
root.configure(bg="#4a4a4a")
root.geometry("740x640")
# 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)
button_save = ttk.Button(frame, text="Save URLs to File", command=save_urls)
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()