import os
import subprocess
import tkinter as tk
from tkinter import messagebox, scrolledtext, filedialog # Include filedialog here
import requests
# Paths for media players
POTPLAYER_PATH = r"C:\Program Files\DAUM\PotPlayer\PotPlayerMini.exe"
VLC_PATH = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
def fetch_playlist():
"""Fetch M3U playlist from the provided URL and display it."""
url = url_entry.get().strip()
if not url:
messagebox.showwarning("Input Required", "Please enter a playlist URL.")
return
# Clear the results field
results_field.delete(1.0, tk.END)
try:
# Fetch content from the URL
response = requests.get(url, timeout=10)
response.raise_for_status() # Raise an error for HTTP errors
# Check if the response is a valid M3U playlist
if not response.text.strip().startswith("#EXTM3U"):
raise ValueError("The fetched file is not a valid M3U playlist.")
# Display content in the results field
results_field.insert(tk.END, response.text)
messagebox.showinfo("Success", "Playlist fetched successfully.")
except requests.exceptions.RequestException as e:
messagebox.showerror("Network Error", f"Failed to fetch the playlist:\n{e}")
except ValueError as ve:
messagebox.showerror("Invalid Playlist", str(ve))
def open_m3u_file():
"""Open an M3U file and display its contents in the results field."""
file_path = filedialog.askopenfilename(
filetypes=[("M3U Files", "*.m3u"), ("All Files", "*.*")]
)
if file_path:
try:
with open(file_path, "r", encoding="utf-8") as file:
links = file.readlines()
results_field.delete(1.0, tk.END)
results_field.insert(tk.END, "".join(links))
except Exception as e:
messagebox.showerror("Error", f"Failed to open file: {e}")
def save_m3u_file():
"""Save the M3U playlist content to a local file."""
content = results_field.get(1.0, tk.END).strip()
if not content:
messagebox.showwarning("No Content", "No playlist content to save.")
return
try:
with open("Channels.m3u", "w", encoding="utf-8") as file:
file.write(content)
messagebox.showinfo("Success", "File saved as Channels.m3u")
except Exception as e:
messagebox.showerror("Error", f"Failed to save file:\n{e}")
def play_selected_link():
"""Play the selected link using the chosen media player."""
try:
# Retrieve the selected text
selected_text = results_field.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
except tk.TclError:
messagebox.showwarning("No Selection", "Please select a link to play.")
return
if not selected_text.startswith("http"):
messagebox.showwarning("Invalid Selection", "Please select a valid URL.")
return
# Determine the player to use
player_path = VLC_PATH if player_var.get() == "vlc" else POTPLAYER_PATH
if not os.path.exists(player_path):
messagebox.showerror(
"Player Not Found", f"The selected player is not installed:\n{player_path}"
)
return
try:
# Use subprocess to launch the player with the selected URL
subprocess.Popen([player_path, selected_text], shell=False)
except Exception as e:
messagebox.showerror("Error", f"Failed to play link: {e}")
# Create the main Tkinter window
root = tk.Tk()
root.title("Najeeb Advanced IPTV URL Player")
root.geometry("800x600")
root.configure(bg="#2a2a2a")
# Create styles
header_font = ("Arial", 18, "bold")
button_font = ("Arial", 12)
text_font = ("Courier", 12)
bg_color = "#1f1f1f"
fg_color = "#ffffff"
button_color = "#5a9bd3"
highlight_color = "#ff9f43"
# Header
header_label = tk.Label(
root, text="Najeeb Advanced IPTV URL Player", font=header_font, bg=bg_color, fg=highlight_color
)
header_label.pack(pady=10)
# URL entry frame
url_frame = tk.Frame(root, bg=bg_color)
url_frame.pack(pady=10, fill=tk.X, padx=10)
url_label = tk.Label(
url_frame, text="Playlist URL:", font=button_font, bg=bg_color, fg=fg_color
)
url_label.pack(side=tk.LEFT, padx=5)
url_entry = tk.Entry(url_frame, font=text_font, bg="#333333", fg=fg_color, insertbackground=fg_color, width=40)
url_entry.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
fetch_button = tk.Button(
url_frame, text="Fetch Playlist", font=button_font, bg=button_color, fg=fg_color, command=fetch_playlist
)
fetch_button.pack(side=tk.LEFT, padx=5)
# Buttons frame
buttons_frame = tk.Frame(root, bg=bg_color)
buttons_frame.pack(pady=10)
open_button = tk.Button(
buttons_frame, text="Open M3U File", font=button_font, bg=button_color, fg=fg_color, command=open_m3u_file
)
open_button.grid(row=0, column=0, padx=5)
save_button = tk.Button(
buttons_frame, text="Save File", font=button_font, bg=button_color, fg=fg_color, command=save_m3u_file
)
save_button.grid(row=0, column=1, padx=5)
play_button = tk.Button(
buttons_frame, text="Play Selected Link", font=button_font, bg=button_color, fg=fg_color, command=play_selected_link
)
play_button.grid(row=0, column=2, padx=5)
# Player selection
player_var = tk.StringVar(value="vlc")
player_frame = tk.Frame(root, bg=bg_color)
player_frame.pack(pady=5)
vlc_radio = tk.Radiobutton(
player_frame, text="VLC Player", variable=player_var, value="vlc", bg=bg_color, fg=fg_color, selectcolor=bg_color
)
vlc_radio.grid(row=0, column=0, padx=10)
potplayer_radio = tk.Radiobutton(
player_frame, text="PotPlayer", variable=player_var, value="potplayer", bg=bg_color, fg=fg_color, selectcolor=bg_color
)
potplayer_radio.grid(row=0, column=1, padx=10)
# Results field
results_field = scrolledtext.ScrolledText(
root, font=text_font, bg="#333333", fg=fg_color, insertbackground=fg_color, wrap=tk.WORD
)
results_field.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Run the main event loop
root.mainloop()