Najeebsk

IPTV-ALL-CH.pyw

May 12th, 2024 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.10 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import ttk, filedialog, messagebox
  4. import requests
  5. import subprocess
  6. import threading
  7. import time
  8. import vlc
  9.  
  10. # Functions for the main functionalities
  11. def search_channels():
  12.     search_term = url_entry.get().lower()
  13.     if search_term.startswith("http"):
  14.         search_by_url(search_term)
  15.     else:
  16.         search_by_path_or_category(search_term)
  17.  
  18. def search_by_url(url):
  19.     try:
  20.         if os.path.exists(url):
  21.             with open(url, 'r', encoding='utf-8') as file:
  22.                 m3u_data = file.readlines()
  23.             process_m3u_data(m3u_data)
  24.         else:
  25.             response = requests.get(url)
  26.             if response.status_code == 200:
  27.                 m3u_data = response.text.split('\n')
  28.                 process_m3u_data(m3u_data)
  29.             else:
  30.                 result_text.insert(tk.END, f"Error: Failed to fetch channel data. Status Code: {response.status_code}\n")
  31.     except Exception as e:
  32.         result_text.insert(tk.END, f"Error: {str(e)}\n")
  33.  
  34. def search_by_path_or_category(path):
  35.     try:
  36.         if os.path.exists(path):
  37.             with open(path, 'r', encoding='utf-8') as file:
  38.                 m3u_data = file.readlines()
  39.             process_m3u_data(m3u_data)
  40.         else:
  41.             selected_category = category_var.get()
  42.             if selected_category in category_urls:
  43.                 category_url = category_urls[selected_category]
  44.                 if category_url:
  45.                     search_by_url(category_url)
  46.                 else:
  47.                     result_text.insert(tk.END, f"Error: Category URL is not provided for {selected_category}\n")
  48.             else:
  49.                 result_text.insert(tk.END, f"Error: Category '{selected_category}' not found\n")
  50.     except Exception as e:
  51.         result_text.insert(tk.END, f"Error: {str(e)}\n")
  52.  
  53. def process_m3u_data(m3u_data):
  54.     result_text.delete(0, tk.END)
  55.     global channels_info
  56.     channels_info = {}
  57.     channel_name = None
  58.     for line in m3u_data:
  59.         if line.startswith('#EXTINF:'):
  60.             channel_name = line.split(',')[-1].strip()
  61.         elif line.startswith('http') and channel_name:
  62.             channels_info[channel_name] = line.strip()
  63.             result_text.insert(tk.END, channel_name)
  64.             channel_name = None
  65.  
  66. def play_selected_channel(event):
  67.     try:
  68.         selected_channel = result_text.get(tk.ACTIVE)
  69.         if os.path.exists(selected_channel):
  70.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", selected_channel])
  71.         else:
  72.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", channels_info[selected_channel]])
  73.     except (tk.TclError, KeyError):
  74.         pass
  75.  
  76. def check_links():
  77.     global working_links
  78.     working_links = {}
  79.     for channel_name, url in channels_info.items():
  80.         try:
  81.             response = requests.get(url)
  82.             if response.status_code == 200:
  83.                 working_links[channel_name] = url
  84.         except requests.RequestException:
  85.             pass
  86.  
  87.     result_text.delete(0, tk.END)
  88.     for channel_name, url in working_links.items():
  89.         result_text.insert(tk.END, f"{channel_name}: {url}\n")
  90.  
  91. def save_working_links():
  92.     with open("working_channels.m3u", "w", encoding="utf-8") as f:
  93.         for channel_name, url in working_links.items():
  94.             f.write(f"#EXTINF:-1,{channel_name}\n{url}\n")
  95.  
  96. def filter_channels(event=None):
  97.     keyword = search_entry.get().lower()
  98.     result_text.delete(0, tk.END)
  99.     for channel_name, url in channels_info.items():
  100.         if keyword in channel_name.lower():
  101.             result_text.insert(tk.END, channel_name)
  102.  
  103. def browse_file():
  104.     file_path = filedialog.askopenfilename(filetypes=[("M3U Files", "*.m3u"), ("All Files", "*.*")])
  105.     if file_path:
  106.         url_entry.delete(0, tk.END)
  107.         url_entry.insert(0, file_path)
  108.  
  109. def preview_selected_link():
  110.     try:
  111.         selected_channel = result_text.get(tk.ACTIVE).strip()
  112.         if selected_channel in channels_info:
  113.             url = channels_info[selected_channel]
  114.             preview_frame.pack(fill="both", expand=True)
  115.             media = instance.media_new(url)
  116.             player.set_media(media)
  117.             player.play()
  118.             update_slider()
  119.         else:
  120.             messagebox.showerror("Error", "Selected text is not a valid URL.")
  121.     except tk.TclError:
  122.         messagebox.showerror("Error", "No text selected.")
  123.  
  124. def stop_preview():
  125.     player.stop()
  126.     preview_frame.pack_forget()
  127.  
  128. def capture_video():
  129.     try:
  130.         selected_channel = result_text.get(tk.ACTIVE).strip()
  131.         if selected_channel in channels_info:
  132.             url = channels_info[selected_channel]
  133.             filename = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("MP4 files", "*.mp4")])
  134.             if filename:
  135.                 command = ['ffmpeg', '-y', '-i', url, '-t', '00:05:00', '-c', 'copy', filename]
  136.                 threading.Thread(target=lambda: subprocess.run(command)).start()
  137.                 messagebox.showinfo("Capturing", f"Capturing 5 minutes of video to {filename}")
  138.         else:
  139.             messagebox.showerror("Error", "Selected text is not a valid URL.")
  140.     except tk.TclError:
  141.         messagebox.showerror("Error", "No text selected.")
  142.  
  143. def capture_screenshots():
  144.     if player.get_media():
  145.         filename_base = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
  146.         if filename_base:
  147.             interval = 5
  148.             num_screenshots = 5
  149.             for i in range(num_screenshots):
  150.                 time.sleep(interval)
  151.                 filename = f"{filename_base}_{i+1}.png"
  152.                 player.video_take_snapshot(0, filename, 0, 0)
  153.                 messagebox.showinfo("Captured", f"Screenshot {i+1} saved to {filename}")
  154.  
  155. def update_slider():
  156.     if player.get_media():
  157.         length = player.get_length() / 1000
  158.         position = player.get_time() / 1000
  159.         if length > 0:
  160.             slider.set(position / length * 100)
  161.         root.after(1000, update_slider)
  162.  
  163. def set_position(event):
  164.     if player.get_media():
  165.         length = player.get_length() / 1000
  166.         player.set_time(int(slider.get() / 100 * length * 1000))
  167.  
  168. def on_configure(event):
  169.     if event.widget == canvas:
  170.         player.set_hwnd(canvas.winfo_id())
  171.  
  172. def toggle_mute():
  173.     is_muted = player.audio_get_mute()
  174.     player.audio_set_mute(not is_muted)
  175.  
  176. # GUI setup
  177. root = tk.Tk()
  178. root.title("Najeeb IPTV Channel Link Checker")
  179. root.configure(bg="#4a4a4a")
  180.  
  181. url_frame = tk.Frame(root, bg="#4a4a4a")
  182. url_frame.pack(pady=10)
  183.  
  184. url_label = tk.Label(url_frame, text="Enter URL or local path or select category:", bg="#4a4a4a", fg="white")
  185. url_label.pack(side=tk.LEFT, padx=5)
  186.  
  187. url_entry = tk.Entry(url_frame, width=80)
  188. url_entry.pack(side=tk.LEFT, padx=5)
  189.  
  190. search_button = tk.Button(url_frame, text="Search", command=search_channels, bg="#FFA500", fg="white")
  191. search_button.pack(side=tk.LEFT, padx=5)
  192.  
  193. result_label = tk.Label(root, text="Check and save working URLs in M3U file:", bg="#4a4a4a", fg="white")
  194. result_label.pack()
  195.  
  196. search_frame = tk.Frame(root, bg="#4a4a4a")
  197. search_frame.pack()
  198.  
  199. search_label = tk.Label(search_frame, text="Search Channel Name:", bg="#4a4a4a", fg="white")
  200. search_label.pack(side=tk.LEFT, padx=5)
  201.  
  202. search_entry = tk.Entry(search_frame, width=105)
  203. search_entry.pack(side=tk.LEFT, padx=5)
  204. search_entry.bind('<KeyRelease>', filter_channels)
  205.  
  206. scrollbar = tk.Scrollbar(root, orient=tk.VERTICAL)
  207. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  208.  
  209. result_text = tk.Listbox(root, height=12, width=160, yscrollcommand=scrollbar.set)
  210. result_text.pack()
  211.  
  212. scrollbar.config(command=result_text.yview)
  213. result_text.bind("<Double-1>", play_selected_channel)
  214.  
  215. check_button = tk.Button(root, text="Check Links", command=check_links, bg="#008000", fg="white")
  216. check_button.pack(side=tk.RIGHT, padx=5)
  217.  
  218. save_button = tk.Button(root, text="Save Working Links", command=save_working_links, bg="#FF0000", fg="white")
  219. save_button.pack(side=tk.LEFT, padx=5)
  220.  
  221. category_urls = {
  222.     "NAJEEB-IPTV": "",
  223.     "ALL-INDEX": "https://iptv-org.github.io/iptv/index.m3u",
  224.     "CATEGORY": "https://iptv-org.github.io/iptv/index.category.m3u",
  225.     "LANGUAGE": "https://iptv-org.github.io/iptv/index.language.m3u",
  226.     "REGION": "https://iptv-org.github.io/iptv/index.region.m3u",
  227. }
  228.  
  229. category_var = tk.StringVar(root)
  230. category_var.set("NAJEEB-IPTV")
  231. category_dropdown = ttk.OptionMenu(root, category_var, *category_urls.keys())
  232. category_dropdown.pack(pady=10)
  233.  
  234. button_frame = tk.Frame(root, bg="#4a4a4a")
  235. button_frame.pack()
  236.  
  237. browse_button = tk.Button(button_frame, text="Browse Playlist", command=browse_file, bg="#5a5a5a", fg="white")
  238. browse_button.pack(side=tk.LEFT, padx=5)
  239.  
  240. preview_button = tk.Button(button_frame, text="Preview", command=preview_selected_link, bg="#5a5a5a", fg="white")
  241. preview_button.pack(side=tk.LEFT, padx=5)
  242.  
  243. capture_video_button = tk.Button(button_frame, text="Capture Video", command=capture_video, bg="#5a5a5a", fg="white")
  244. capture_video_button.pack(side=tk.LEFT, padx=5)
  245.  
  246. capture_screenshot_button = tk.Button(button_frame, text="Capture Screenshots", command=capture_screenshots, bg="#5a5a5a", fg="white")
  247. capture_screenshot_button.pack(side=tk.LEFT, padx=5)
  248.  
  249. toggle_mute_button = tk.Button(button_frame, text="Toggle Audio Mute", command=toggle_mute, bg="#5a5a5a", fg="white")
  250. toggle_mute_button.pack(side=tk.LEFT, padx=5)
  251.  
  252. stop_button = tk.Button(button_frame, text="Close Preview", command=stop_preview, bg="#5a5a5a", fg="white")
  253. stop_button.pack(side=tk.LEFT, padx=5)
  254.  
  255. preview_frame = tk.Frame(root, bg="#4a4a4a", height=200)
  256. preview_frame.pack(fill="both", expand=True)
  257. preview_frame.pack_forget()
  258.  
  259. canvas = tk.Canvas(preview_frame, bg="#4a4a4a")
  260. canvas.pack(fill="both", expand=True)
  261. canvas.bind("<Configure>", on_configure)
  262.  
  263. slider = tk.Scale(preview_frame, from_=0, to=100, orient=tk.HORIZONTAL, command=set_position, bg="#4a4a4a", fg="white")
  264. slider.pack(fill="x", padx=5)
  265.  
  266. instance = vlc.Instance()
  267. player = instance.media_player_new()
  268.  
  269. channels_info = {}
  270. working_links = {}
  271.  
  272. root.mainloop()
  273.  
Add Comment
Please, Sign In to add comment