Advertisement
Najeebsk

IPTV-CHANNEL-URL-M3U.pyw

Nov 29th, 2024
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.05 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import tkinter as tk
  4. from tkinter import messagebox, scrolledtext, filedialog  # Include filedialog here
  5. import requests
  6.  
  7. # Paths for media players
  8. POTPLAYER_PATH = r"C:\Program Files\DAUM\PotPlayer\PotPlayerMini.exe"
  9. VLC_PATH = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
  10.  
  11. def fetch_playlist():
  12.     """Fetch M3U playlist from the provided URL and display it."""
  13.     url = url_entry.get().strip()
  14.     if not url:
  15.         messagebox.showwarning("Input Required", "Please enter a playlist URL.")
  16.         return
  17.    
  18.     # Clear the results field
  19.     results_field.delete(1.0, tk.END)
  20.    
  21.     try:
  22.         # Fetch content from the URL
  23.         response = requests.get(url, timeout=10)
  24.         response.raise_for_status()  # Raise an error for HTTP errors
  25.        
  26.         # Check if the response is a valid M3U playlist
  27.         if not response.text.strip().startswith("#EXTM3U"):
  28.             raise ValueError("The fetched file is not a valid M3U playlist.")
  29.        
  30.         # Display content in the results field
  31.         results_field.insert(tk.END, response.text)
  32.         messagebox.showinfo("Success", "Playlist fetched successfully.")
  33.     except requests.exceptions.RequestException as e:
  34.         messagebox.showerror("Network Error", f"Failed to fetch the playlist:\n{e}")
  35.     except ValueError as ve:
  36.         messagebox.showerror("Invalid Playlist", str(ve))
  37.  
  38. def open_m3u_file():
  39.     """Open an M3U file and display its contents in the results field."""
  40.     file_path = filedialog.askopenfilename(
  41.         filetypes=[("M3U Files", "*.m3u"), ("All Files", "*.*")]
  42.     )
  43.     if file_path:
  44.         try:
  45.             with open(file_path, "r", encoding="utf-8") as file:
  46.                 links = file.readlines()
  47.                 results_field.delete(1.0, tk.END)
  48.                 results_field.insert(tk.END, "".join(links))
  49.         except Exception as e:
  50.             messagebox.showerror("Error", f"Failed to open file: {e}")
  51.        
  52. def save_m3u_file():
  53.     """Save the M3U playlist content to a local file."""
  54.     content = results_field.get(1.0, tk.END).strip()
  55.     if not content:
  56.         messagebox.showwarning("No Content", "No playlist content to save.")
  57.         return
  58.    
  59.     try:
  60.         with open("Channels.m3u", "w", encoding="utf-8") as file:
  61.             file.write(content)
  62.         messagebox.showinfo("Success", "File saved as Channels.m3u")
  63.     except Exception as e:
  64.         messagebox.showerror("Error", f"Failed to save file:\n{e}")
  65.  
  66. def play_selected_link():
  67.     """Play the selected link using the chosen media player."""
  68.     try:
  69.         # Retrieve the selected text
  70.         selected_text = results_field.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  71.     except tk.TclError:
  72.         messagebox.showwarning("No Selection", "Please select a link to play.")
  73.         return
  74.  
  75.     if not selected_text.startswith("http"):
  76.         messagebox.showwarning("Invalid Selection", "Please select a valid URL.")
  77.         return
  78.  
  79.     # Determine the player to use
  80.     player_path = VLC_PATH if player_var.get() == "vlc" else POTPLAYER_PATH
  81.     if not os.path.exists(player_path):
  82.         messagebox.showerror(
  83.             "Player Not Found", f"The selected player is not installed:\n{player_path}"
  84.         )
  85.         return
  86.  
  87.     try:
  88.         # Use subprocess to launch the player with the selected URL
  89.         subprocess.Popen([player_path, selected_text], shell=False)
  90.     except Exception as e:
  91.         messagebox.showerror("Error", f"Failed to play link: {e}")
  92.  
  93. # Create the main Tkinter window
  94. root = tk.Tk()
  95. root.title("Najeeb Advanced IPTV URL Player")
  96. root.geometry("800x600")
  97. root.configure(bg="#2a2a2a")
  98.  
  99. # Create styles
  100. header_font = ("Arial", 18, "bold")
  101. button_font = ("Arial", 12)
  102. text_font = ("Courier", 12)
  103. bg_color = "#1f1f1f"
  104. fg_color = "#ffffff"
  105. button_color = "#5a9bd3"
  106. highlight_color = "#ff9f43"
  107.  
  108. # Header
  109. header_label = tk.Label(
  110.     root, text="Najeeb Advanced IPTV URL Player", font=header_font, bg=bg_color, fg=highlight_color
  111. )
  112. header_label.pack(pady=10)
  113.  
  114. # URL entry frame
  115. url_frame = tk.Frame(root, bg=bg_color)
  116. url_frame.pack(pady=10, fill=tk.X, padx=10)
  117.  
  118. url_label = tk.Label(
  119.     url_frame, text="Playlist URL:", font=button_font, bg=bg_color, fg=fg_color
  120. )
  121. url_label.pack(side=tk.LEFT, padx=5)
  122.  
  123. url_entry = tk.Entry(url_frame, font=text_font, bg="#333333", fg=fg_color, insertbackground=fg_color, width=40)
  124. url_entry.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
  125.  
  126. fetch_button = tk.Button(
  127.     url_frame, text="Fetch Playlist", font=button_font, bg=button_color, fg=fg_color, command=fetch_playlist
  128. )
  129. fetch_button.pack(side=tk.LEFT, padx=5)
  130.  
  131. # Buttons frame
  132. buttons_frame = tk.Frame(root, bg=bg_color)
  133. buttons_frame.pack(pady=10)
  134.  
  135. open_button = tk.Button(
  136.     buttons_frame, text="Open M3U File", font=button_font, bg=button_color, fg=fg_color, command=open_m3u_file
  137. )
  138. open_button.grid(row=0, column=0, padx=5)
  139.  
  140. save_button = tk.Button(
  141.     buttons_frame, text="Save File", font=button_font, bg=button_color, fg=fg_color, command=save_m3u_file
  142. )
  143. save_button.grid(row=0, column=1, padx=5)
  144.  
  145. play_button = tk.Button(
  146.     buttons_frame, text="Play Selected Link", font=button_font, bg=button_color, fg=fg_color, command=play_selected_link
  147. )
  148. play_button.grid(row=0, column=2, padx=5)
  149.  
  150. # Player selection
  151. player_var = tk.StringVar(value="vlc")
  152. player_frame = tk.Frame(root, bg=bg_color)
  153. player_frame.pack(pady=5)
  154.  
  155. vlc_radio = tk.Radiobutton(
  156.     player_frame, text="VLC Player", variable=player_var, value="vlc", bg=bg_color, fg=fg_color, selectcolor=bg_color
  157. )
  158. vlc_radio.grid(row=0, column=0, padx=10)
  159.  
  160. potplayer_radio = tk.Radiobutton(
  161.     player_frame, text="PotPlayer", variable=player_var, value="potplayer", bg=bg_color, fg=fg_color, selectcolor=bg_color
  162. )
  163. potplayer_radio.grid(row=0, column=1, padx=10)
  164.  
  165. # Results field
  166. results_field = scrolledtext.ScrolledText(
  167.     root, font=text_font, bg="#333333", fg=fg_color, insertbackground=fg_color, wrap=tk.WORD
  168. )
  169. results_field.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
  170.  
  171. # Run the main event loop
  172. root.mainloop()
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement