Najeebsk

EXTRAC-URLS4.0.pyw

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