Najeebsk

Text-To-M3U.pyw

Aug 19th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog, messagebox
  3. import os
  4.  
  5. class M3UConverterApp(tk.Tk):
  6.     def __init__(self):
  7.         super().__init__()
  8.         self.title("Najeeb Text To M3U Converter")
  9.         self.geometry("400x200")
  10.         self.configure(bg="#282C34")
  11.  
  12.         self.create_widgets()
  13.  
  14.     def create_widgets(self):
  15.         # Select File Button
  16.         self.select_file_btn = tk.Button(self, text="Select Text File", command=self.select_file, bg='lightblue', fg='black')
  17.         self.select_file_btn.pack(pady=20)
  18.  
  19.         # Convert Button
  20.         self.convert_btn = tk.Button(self, text="Convert to M3U", command=self.convert_to_m3u, bg='lightgreen', fg='black')
  21.         self.convert_btn.pack(pady=20)
  22.  
  23.     def select_file(self):
  24.         self.file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
  25.         if self.file_path:
  26.             messagebox.showinfo("File Selected", f"Selected file: {os.path.basename(self.file_path)}")
  27.  
  28.     def convert_to_m3u(self):
  29.         if not hasattr(self, 'file_path'):
  30.             messagebox.showwarning("No File Selected", "Please select a file first.")
  31.             return
  32.  
  33.         m3u_content = "#EXTM3U\n"
  34.         with open(self.file_path, 'r', encoding='utf-8') as file:
  35.             for line in file:
  36.                 # Assuming the line format is "XXX-VIP http://url.com/path"
  37.                 if line.strip():
  38.                     name, url = line.split(maxsplit=1)
  39.                     m3u_content += f"#EXTINF:-1,{name}\n{url}\n"
  40.  
  41.         save_path = filedialog.asksaveasfilename(defaultextension=".m3u", filetypes=[("M3U Files", "*.m3u")])
  42.         if save_path:
  43.             with open(save_path, 'w', encoding='utf-8') as m3u_file:
  44.                 m3u_file.write(m3u_content)
  45.             messagebox.showinfo("Conversion Complete", f"M3U file saved as {os.path.basename(save_path)}")
  46.  
  47. if __name__ == "__main__":
  48.     app = M3UConverterApp()
  49.     app.mainloop()
  50.  
Add Comment
Please, Sign In to add comment