Najeebsk

IPTV-TEXT-TO-M3U.pyw

May 19th, 2024
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog, messagebox, scrolledtext
  3.  
  4. def browse_file():
  5.     filepath = filedialog.askopenfilename(
  6.         title="Open Playlist .txt file",
  7.         filetypes=(("Text Files", "*.txt"), ("All Files", "*.*"))
  8.     )
  9.     if filepath:
  10.         try:
  11.             with open(filepath, 'r', encoding='utf-8') as file:
  12.                 content = file.read()
  13.                 text_field.delete(1.0, tk.END)
  14.                 text_field.insert(tk.END, content)
  15.         except Exception as e:
  16.             messagebox.showerror("Error", f"An error occurred while reading the file: {e}")
  17.  
  18. def convert_playlist():
  19.     try:
  20.         content = text_field.get(1.0, tk.END).strip()
  21.         lines = content.split('\n')
  22.         if len(lines) % 2 != 0:
  23.             messagebox.showerror("Error", "Invalid content format. Make sure each URL is preceded by a channel name.")
  24.             return
  25.  
  26.         m3u_lines = ['#EXTM3U\n']
  27.         for i in range(0, len(lines), 2):
  28.             name_line = lines[i].strip()
  29.             url_line = lines[i + 1].strip()
  30.             m3u_lines.append(f'#EXTINF:-1 group-title="{name_line}"\n{url_line}\n')
  31.        
  32.         output_filepath = filedialog.asksaveasfilename(
  33.             defaultextension=".m3u",
  34.             filetypes=(("M3U Files", "*.m3u"), ("All Files", "*.*")),
  35.             title="Save Converted Playlist"
  36.         )
  37.        
  38.         if output_filepath:
  39.             with open(output_filepath, 'w') as output_file:
  40.                 output_file.writelines(m3u_lines)
  41.             messagebox.showinfo("Success", f"Playlist successfully saved to {output_filepath}")
  42.    
  43.     except Exception as e:
  44.         messagebox.showerror("Error", f"An error occurred: {e}")
  45.  
  46. root = tk.Tk()
  47. root.title("Najeeb Playlist Converter")
  48. root.configure(bg="#4a4a4a")
  49.  
  50. frame = tk.Frame(root, padx=5, pady=5, bg="#4a4a4a")
  51. frame.pack(padx=5, pady=5)
  52.  
  53. browse_button = tk.Button(frame, text="Browse .txt File", command=browse_file, bg="#5a5a5a", fg="white")
  54. browse_button.pack()
  55.  
  56. text_frame = tk.Frame(frame)
  57. text_frame.pack(padx=5, pady=5, fill=tk.BOTH, expand=True)
  58.  
  59. scrollbar = tk.Scrollbar(text_frame)
  60. #scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  61.  
  62. text_field = scrolledtext.ScrolledText(text_frame, wrap=tk.WORD, yscrollcommand=scrollbar.set, height=20, width=110)
  63. text_field.pack(padx=5, pady=5, fill=tk.BOTH, expand=True)
  64. scrollbar.config(command=text_field.yview)
  65.  
  66. convert_button = tk.Button(frame, text="Convert to .m3u", command=convert_playlist, bg="#5a5a5a", fg="white")
  67. convert_button.pack(pady=10)
  68.  
  69. root.mainloop()
  70.  
Add Comment
Please, Sign In to add comment