Advertisement
Najeebsk

Extrac-Urls3.0.pyw

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