Najeebsk

CHECK-IPTV-M3U-PREVIWE.pyw

May 21st, 2024 (edited)
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.84 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.                 try:
  28.                     m3u_data = response.text.split('\n')
  29.                 except UnicodeDecodeError:
  30.                     m3u_data = response.content.decode('ISO-8859-1').split('\n')
  31.                 process_m3u_data(m3u_data)
  32.             else:
  33.                 result_text.insert(tk.END, f"Error: Failed to fetch channel data. Status Code: {response.status_code}\n")
  34.     except Exception as e:
  35.         result_text.insert(tk.END, f"Error: {str(e)}\n")
  36.  
  37. def search_by_path_or_category(path):
  38.     try:
  39.         if os.path.exists(path):
  40.             try:
  41.                 with open(path, 'r', encoding='utf-8') as file:
  42.                     m3u_data = file.readlines()
  43.             except UnicodeDecodeError:
  44.                 with open(path, 'r', encoding='ISO-8859-1') as file:
  45.                     m3u_data = file.readlines()
  46.             process_m3u_data(m3u_data)
  47.         else:
  48.             selected_category = category_var.get()
  49.             if selected_category in category_urls:
  50.                 category_url = category_urls[selected_category]
  51.                 if category_url:
  52.                     search_by_url(category_url)
  53.                 else:
  54.                     result_text.insert(tk.END, f"Error: Category URL is not provided for {selected_category}\n")
  55.             else:
  56.                 result_text.insert(tk.END, f"Error: Category '{selected_category}' not found\n")
  57.     except Exception as e:
  58.         result_text.insert(tk.END, f"Error: {str(e)}\n")
  59.  
  60. def process_m3u_data(m3u_data):
  61.     result_text.delete(0, tk.END)
  62.     global channels_info
  63.     channels_info = {}
  64.     channel_name = None
  65.     for line in m3u_data:
  66.         if line.startswith('#EXTINF:'):
  67.             channel_name = line.split(',')[-1].strip()
  68.         elif line.startswith('http') and channel_name:
  69.             channels_info[channel_name] = line.strip()
  70.             result_text.insert(tk.END, channel_name)
  71.             channel_name = None
  72.  
  73. def play_selected_channel(event):
  74.     try:
  75.         selected_channel = result_text.get(tk.ACTIVE)
  76.         if os.path.exists(selected_channel):
  77.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", selected_channel])
  78.         else:
  79.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", channels_info[selected_channel]])
  80.     except (tk.TclError, KeyError):
  81.         pass
  82.  
  83. def check_links():
  84.     global working_links
  85.     working_links = {}
  86.     for channel_name, url in channels_info.items():
  87.         try:
  88.             response = requests.get(url)
  89.             if response.status_code == 200:
  90.                 working_links[channel_name] = url
  91.         except requests.RequestException:
  92.             pass
  93.  
  94.     result_text.delete(0, tk.END)
  95.     for channel_name, url in working_links.items():
  96.         result_text.insert(tk.END, f"{channel_name}: {url}\n")
  97.  
  98. def save_working_links():
  99.     with open("working_channels.m3u", "w", encoding="utf-8") as f:
  100.         for channel_name, url in working_links.items():
  101.             f.write(f"#EXTINF:-1,{channel_name}\n{url}\n")
  102.  
  103. def filter_channels(event=None):
  104.     keyword = search_entry.get().lower()
  105.     result_text.delete(0, tk.END)
  106.     for channel_name, url in channels_info.items():
  107.         if keyword in channel_name.lower():
  108.             result_text.insert(tk.END, channel_name)
  109.  
  110. def browse_file():
  111.     file_path = filedialog.askopenfilename(filetypes=[("M3U Files", "*.m3u"), ("All Files", "*.*")])
  112.     if file_path:
  113.         url_entry.delete(0, tk.END)
  114.         url_entry.insert(0, file_path)
  115.  
  116. def preview_selected_link():
  117.     try:
  118.         selected_channel = result_text.get(tk.ACTIVE).strip()
  119.         if selected_channel in channels_info:
  120.             url = channels_info[selected_channel]
  121.             preview_frame.pack(fill="both", expand=True)
  122.             media = instance.media_new(url)
  123.             player.set_media(media)
  124.             player.play()
  125.             update_slider()
  126.         else:
  127.             messagebox.showerror("Error", "Selected text is not a valid URL.")
  128.     except tk.TclError:
  129.         messagebox.showerror("Error", "No text selected.")
  130.  
  131. def stop_preview():
  132.     player.stop()
  133.     preview_frame.pack_forget()
  134.  
  135. def capture_video():
  136.     try:
  137.         selected_channel = result_text.get(tk.ACTIVE).strip()
  138.         if selected_channel in channels_info:
  139.             url = channels_info[selected_channel]
  140.             filename = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("MP4 files", "*.mp4")])
  141.             if filename:
  142.                 command = ['ffmpeg', '-y', '-i', url, '-t', '03:55:00', '-c', 'copy', filename]
  143.                 threading.Thread(target=lambda: subprocess.run(command)).start()
  144.                 messagebox.showinfo("Capturing", f"Capturing 03:55 minutes of video to {filename}")
  145.         else:
  146.             messagebox.showerror("Error", "Selected text is not a valid URL.")
  147.     except tk.TclError:
  148.         messagebox.showerror("Error", "No text selected.")
  149.  
  150. def record_audio():
  151.     try:
  152.         selected_channel = result_text.get(tk.ACTIVE).strip()
  153.         if selected_channel in channels_info:
  154.             url = channels_info[selected_channel]
  155.             filename = filedialog.asksaveasfilename(defaultextension=".mp3", filetypes=[("MP3 files", "*.mp3")])
  156.             if filename:
  157.                 command = ['ffmpeg', '-y', '-i', url, '-f', 'mp3', '-c:a', 'libmp3lame', filename]
  158.                 global process
  159.                 process = subprocess.Popen(command)
  160.                 messagebox.showinfo("Recording", f"Recording audio to {filename}")
  161.         else:
  162.             messagebox.showerror("Error", "Selected text is not a valid URL.")
  163.     except tk.TclError:
  164.         messagebox.showerror("Error", "No text selected.")
  165.  
  166. def stop_recording():
  167.     if process:
  168.         process.terminate()
  169.         messagebox.showinfo("Stopped", "Recording stopped")
  170.        
  171. def capture_screenshots():
  172.     if player.get_media():
  173.         filename_base = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
  174.         if filename_base:
  175.             interval = 5
  176.             num_screenshots = 5
  177.             for i in range(num_screenshots):
  178.                 time.sleep(interval)
  179.                 filename = f"{filename_base}_{i+1}.png"
  180.                 player.video_take_snapshot(0, filename, 0, 0)
  181.                 messagebox.showinfo("Captured", f"Screenshot {i+1} saved to {filename}")
  182.  
  183. def update_slider():
  184.     if player.get_media():
  185.         length = player.get_length() / 1000
  186.         position = player.get_time() / 1000
  187.         if length > 0:
  188.             slider.set(position / length * 100)
  189.         root.after(1000, update_slider)
  190.  
  191. def set_position(event):
  192.     if player.get_media():
  193.         length = player.get_length() / 1000
  194.         player.set_time(int(slider.get() / 100 * length * 1000))
  195.  
  196. def on_configure(event):
  197.     if event.widget == canvas:
  198.         player.set_hwnd(canvas.winfo_id())
  199.  
  200. #def toggle_mute():
  201.     #is_muted = player.audio_get_mute()
  202.     #player.audio_set_mute(not is_muted)
  203.  
  204. def set_volume(value):
  205.         player.audio_set_volume(int(value))        
  206.  
  207. # GUI setup
  208. root = tk.Tk()
  209. root.title("Najeeb IPTV Channel Link Checker")
  210. root.configure(bg="#4a4a4a")
  211.  
  212. url_frame = tk.Frame(root, bg="#4a4a4a")
  213. url_frame.pack(pady=10)
  214.  
  215. url_label = tk.Label(url_frame, text="Enter URL or local path or select category:", bg="#4a4a4a", fg="white")
  216. url_label.pack(side=tk.LEFT, padx=5)
  217.  
  218. url_entry = tk.Entry(url_frame, width=80)
  219. url_entry.pack(side=tk.LEFT, padx=5)
  220.  
  221. search_button = tk.Button(url_frame, text="Search", command=search_channels, bg="#FFA500", fg="white")
  222. search_button.pack(side=tk.LEFT, padx=5)
  223.  
  224. result_label = tk.Label(root, text="Check and save working URLs in M3U file:", bg="#4a4a4a", fg="white")
  225. result_label.pack()
  226.  
  227. search_frame = tk.Frame(root, bg="#4a4a4a")
  228. search_frame.pack()
  229.  
  230. search_label = tk.Label(search_frame, text="Search Channel Name:", bg="#4a4a4a", fg="white")
  231. search_label.pack(side=tk.LEFT, padx=5)
  232.  
  233. search_entry = tk.Entry(search_frame, width=105)
  234. search_entry.pack(side=tk.LEFT, padx=5)
  235. search_entry.bind('<KeyRelease>', filter_channels)
  236.  
  237. scrollbar = tk.Scrollbar(root, orient=tk.VERTICAL)
  238. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  239.  
  240. result_text = tk.Listbox(root, height=12, width=160, yscrollcommand=scrollbar.set)
  241. result_text.pack()
  242.  
  243. scrollbar.config(command=result_text.yview)
  244. result_text.bind("<Double-1>", play_selected_channel)
  245.  
  246. check_button = tk.Button(root, text="Check Links", command=check_links, bg="#008000", fg="white")
  247. check_button.pack(side=tk.RIGHT, padx=5)
  248.  
  249. save_button = tk.Button(root, text="Save Working Links", command=save_working_links, bg="#FF0000", fg="white")
  250. save_button.pack(side=tk.LEFT, padx=5)
  251.  
  252. category_urls = {
  253.     "NAJEEB-IPTV": "",
  254.     "ALL-INDEX": "https://iptv-org.github.io/iptv/index.m3u",
  255.     "CATEGORY": "https://iptv-org.github.io/iptv/index.category.m3u",
  256.     "LANGUAGE": "https://iptv-org.github.io/iptv/index.language.m3u",
  257.     "REGION": "https://iptv-org.github.io/iptv/index.region.m3u",
  258.     "Brazil": "https://iptv-org.github.io/iptv/countries/br.m3u",
  259.     "France": "https://iptv-org.github.io/iptv/countries/fr.m3u",
  260.     "India": "https://iptv-org.github.io/iptv/countries/in.m3u",
  261.     "Italy": "https://iptv-org.github.io/iptv/countries/it.m3u",
  262.     "Pakistan": "https://iptv-org.github.io/iptv/countries/pk.m3u",
  263.     "Spain": "https://iptv-org.github.io/iptv/countries/es.m3u",
  264.     "Thailand": "https://iptv-org.github.io/iptv/countries/th.m3u",
  265.     "UK": "https://iptv-org.github.io/iptv/countries/uk.m3u",
  266.     "USA": "https://iptv-org.github.io/iptv/countries/us.m3u",
  267.     "Classic": "https://iptv-org.github.io/iptv/categories/classic.m3u",
  268.     "Comedy": "https://iptv-org.github.io/iptv/categories/comedy.m3u",
  269.     "Documentary": "https://iptv-org.github.io/iptv/categories/documentary.m3u",
  270.     "Entertainment": "https://iptv-org.github.io/iptv/categories/entertainment.m3u",
  271.     "Kids": "https://iptv-org.github.io/iptv/categories/kids.m3u",
  272.     "Movies": "https://iptv-org.github.io/iptv/categories/movies.m3u",
  273.     "Music": "https://iptv-org.github.io/iptv/categories/music.m3u",
  274.     "News": "https://iptv-org.github.io/iptv/categories/news.m3u",
  275.     "Science": "https://iptv-org.github.io/iptv/categories/science.m3u",
  276.     "Sports": "https://iptv-org.github.io/iptv/categories/sports.m3u",
  277.     "Travel": "https://iptv-org.github.io/iptv/categories/travel.m3u",
  278.     "PLAYLIST-1": "C:/Users/Najeeb/Desktop/IPTV/PL1.m3u",
  279.     "PLAYLIST-2": "C:/Users/Najeeb/Desktop/IPTV/PL2.m3u",
  280.     "PLAYLIST-3": "C:/Users/Najeeb/Desktop/IPTV/PL3.m3u",
  281.     "PLAYLIST-4": "C:/Users/Najeeb/Desktop/IPTV/PL4.m3u",
  282.     "PLAYLIST-5": "C:/Users/Najeeb/Desktop/IPTV/PL5.m3u",
  283.     "PLAYLIST-6": "C:/Users/Najeeb/Desktop/IPTV/PL6.m3u",
  284.     "PLAYLIST-7": "C:/Users/Najeeb/Desktop/IPTV/PL7.m3u",
  285.     "PLAYLIST-R": "C:/Users/Najeeb/Desktop/IPTV/PLR.m3u",
  286.     "PLAYLIST-X": "C:/Users/Najeeb/Desktop/IPTV/PLX.m3u",
  287. }
  288.  
  289. category_var = tk.StringVar(root)
  290. category_var.set("NAJEEB-IPTV")
  291. category_dropdown = ttk.OptionMenu(root, category_var, *category_urls.keys())
  292. category_dropdown.pack(pady=10)
  293.  
  294. volume_scale = tk.Scale(root, from_=0, to=100, orient="vertical", command=set_volume, label="", bg="#4a4a4a", fg="white")
  295. volume_scale.set(50)  # Set the initial volume to 50%
  296. volume_scale.pack(side="left", fill="x", padx=5, pady=5)
  297.  
  298. button_frame = tk.Frame(root, bg="#4a4a4a")
  299. button_frame.pack()
  300.  
  301. browse_button = tk.Button(button_frame, text="Browse Playlist", command=browse_file, bg="#5a5a5a", fg="white")
  302. browse_button.pack(side=tk.LEFT, padx=5)
  303.  
  304. preview_button = tk.Button(button_frame, text="Preview", command=preview_selected_link, bg="#5a5a5a", fg="white")
  305. preview_button.pack(side=tk.LEFT, padx=5)
  306.  
  307. capture_video_button = tk.Button(button_frame, text="Capture Video", command=capture_video, bg="#5a5a5a", fg="white")
  308. capture_video_button.pack(side=tk.LEFT, padx=5)
  309.  
  310. record_button = tk.Button(button_frame, text="Record Audio", command=record_audio, bg="#5a5a5a", fg="white")
  311. record_button.pack(side=tk.LEFT, padx=5)
  312.  
  313. stop_button = tk.Button(button_frame, text="Stop Recording", command=stop_recording, bg="#5a5a5a", fg="white")
  314. stop_button.pack(side=tk.LEFT, padx=5)
  315.  
  316. capture_screenshot_button = tk.Button(button_frame, text="Capture Screenshots", command=capture_screenshots, bg="#5a5a5a", fg="white")
  317. capture_screenshot_button.pack(side=tk.LEFT, padx=5)
  318.  
  319. #toggle_mute_button = tk.Button(button_frame, text="Toggle Audio Mute", command=toggle_mute, bg="#5a5a5a", fg="white")
  320. #toggle_mute_button.pack(side=tk.LEFT, padx=5)
  321.  
  322. stop_button = tk.Button(button_frame, text="Close Preview", command=stop_preview, bg="#5a5a5a", fg="white")
  323. stop_button.pack(side=tk.LEFT, padx=5)
  324.  
  325. preview_frame = tk.Frame(root, bg="#4a4a4a", height=200)
  326. preview_frame.pack(fill="both", expand=True)
  327. preview_frame.pack_forget()
  328.  
  329. canvas = tk.Canvas(preview_frame, bg="#4a4a4a")
  330. canvas.pack(fill="both", expand=True)
  331. canvas.bind("<Configure>", on_configure)
  332.  
  333. slider = tk.Scale(preview_frame, from_=0, to=100, orient=tk.HORIZONTAL, command=set_position, bg="#4a4a4a", fg="white")
  334. slider.pack(fill="x", padx=5)
  335.  
  336. instance = vlc.Instance()
  337. player = instance.media_player_new()
  338.  
  339. channels_info = {}
  340. working_links = {}
  341.  
  342. root.mainloop()
  343.  
Add Comment
Please, Sign In to add comment