Najeebsk

IPTV-DUPLICAT-CHECK-M3U.pyw

May 21st, 2024
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog, messagebox, scrolledtext
  3.  
  4. class IPTVEditor(tk.Tk):
  5.     def __init__(self):
  6.         super().__init__()
  7.         self.title("Najeeb IPTV Playlist Duplicate Editor")
  8.         self.geometry("800x600")
  9.         self.configure(bg="#4a4a4a")
  10.        
  11.         self.playlist = []
  12.         self.duplicates = []
  13.  
  14.         self.create_widgets()
  15.  
  16.     def create_widgets(self):
  17.         # Frame for buttons
  18.         frame = tk.Frame(self, bg="#4a4a4a")
  19.         #frame = tk.Frame(self)
  20.         frame.pack(fill=tk.X, padx=10, pady=10)
  21.  
  22.         load_button = tk.Button(frame, text="Browse IPTV", command=self.load_file, bg="#5a5a5a", fg="white")
  23.         load_button.pack(side=tk.LEFT, padx=5)
  24.  
  25.         remove_button = tk.Button(frame, text="Remove Duplicates", command=self.remove_duplicates, bg="#5a5a5a", fg="white")
  26.         remove_button.pack(side=tk.LEFT, padx=5)
  27.  
  28.         save_button = tk.Button(frame, text="Save .m3u", command=self.save_m3u, bg="#5a5a5a", fg="white")
  29.         save_button.pack(side=tk.LEFT, padx=5)
  30.  
  31.         # Text field for displaying the playlist
  32.         self.text_area = scrolledtext.ScrolledText(self, wrap=tk.WORD)
  33.         self.text_area.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
  34.         self.text_area.config(state=tk.DISABLED)
  35.  
  36.     def load_file(self):
  37.         file_path = filedialog.askopenfilename(filetypes=[("Playlist files", "*.m3u *.m3u8 *.txt")])
  38.         if file_path:
  39.             with open(file_path, 'r', encoding='utf-8') as file:
  40.                 self.playlist = file.readlines()
  41.             self.find_duplicates()
  42.             self.display_playlist()
  43.  
  44.     def find_duplicates(self):
  45.         url_set = set()
  46.         self.duplicates = []
  47.         i = 0
  48.         while i < len(self.playlist):
  49.             line = self.playlist[i]
  50.             if line.startswith("http"):
  51.                 if line in url_set:
  52.                     self.duplicates.append((self.playlist[i-1], line))  # Add both the #EXTINF and URL
  53.                 else:
  54.                     url_set.add(line)
  55.             i += 1
  56.  
  57.     def display_playlist(self):
  58.         self.text_area.config(state=tk.NORMAL)
  59.         self.text_area.delete(1.0, tk.END)
  60.         i = 0
  61.         while i < len(self.playlist):
  62.             line = self.playlist[i]
  63.             if line.startswith("#EXTINF:"):
  64.                 next_line = self.playlist[i+1] if i+1 < len(self.playlist) else ''
  65.                 if (line, next_line) in self.duplicates:
  66.                     self.text_area.insert(tk.END, line, "duplicate")
  67.                     self.text_area.insert(tk.END, next_line, "duplicate")
  68.                     i += 2
  69.                     continue
  70.             self.text_area.insert(tk.END, line)
  71.             i += 1
  72.         self.text_area.tag_config("duplicate", background="yellow")
  73.         self.text_area.config(state=tk.DISABLED)
  74.  
  75.     def remove_duplicates(self):
  76.         seen_urls = set()
  77.         new_playlist = []
  78.         i = 0
  79.         while i < len(self.playlist):
  80.             line = self.playlist[i]
  81.             if line.startswith("http"):
  82.                 if line not in seen_urls:
  83.                     seen_urls.add(line)
  84.                     new_playlist.extend([self.playlist[i-1], line])  # Add both the #EXTINF and URL
  85.                 i += 1
  86.             else:
  87.                 if i == 0 or not (i+1 < len(self.playlist) and self.playlist[i+1].startswith("http")):
  88.                     new_playlist.append(line)
  89.                 i += 1
  90.  
  91.         self.playlist = new_playlist
  92.         self.find_duplicates()
  93.         self.display_playlist()
  94.  
  95.     def save_m3u(self):
  96.         file_path = filedialog.asksaveasfilename(defaultextension=".m3u", filetypes=[("M3U files", "*.m3u")])
  97.         if file_path:
  98.             with open(file_path, 'w', encoding='utf-8') as file:
  99.                 file.writelines(self.playlist)
  100.             messagebox.showinfo("Save", "Playlist saved successfully")
  101.  
  102. if __name__ == "__main__":
  103.     app = IPTVEditor()
  104.     app.mainloop()
  105.  
Add Comment
Please, Sign In to add comment