Najeebsk

IPTV-ALL-M3U-TOOL.py

Mar 13th, 2024 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.36 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import ttk
  4. import requests
  5. import subprocess
  6.  
  7. def search_channels():
  8.     search_term = url_entry.get().lower()
  9.     if search_term.startswith("http"):
  10.         search_by_url(search_term)
  11.     else:
  12.         search_by_path_or_category(search_term)
  13.  
  14. def search_by_url(url):
  15.     try:
  16.         # Check if the URL is a local file path
  17.         if os.path.exists(url):
  18.             with open(url, 'r') as file:
  19.                 m3u_data = file.readlines()
  20.             # Process M3U data
  21.             process_m3u_data(m3u_data)
  22.         else:
  23.             # Send a GET request to the provided URL
  24.             response = requests.get(url)
  25.             # Check if the request was successful (status code 200)
  26.             if response.status_code == 200:
  27.                 m3u_data = response.text.split('\n')
  28.                 # Process M3U data
  29.                 process_m3u_data(m3u_data)
  30.             else:
  31.                 result_text.insert(tk.END, f"Error: Failed to fetch channel data. Status Code: {response.status_code}")
  32.     except Exception as e:
  33.         result_text.insert(tk.END, f"Error: {str(e)}")
  34.  
  35. def search_by_path_or_category(path):
  36.     try:
  37.         if os.path.exists(path):
  38.             with open(path, 'r') as file:
  39.                 m3u_data = file.readlines()
  40.             process_m3u_data(m3u_data)
  41.         else:
  42.             # Check if the selected category exists in the category_urls dictionary
  43.             selected_category = category_var.get()
  44.             if selected_category in category_urls:
  45.                 category_url = category_urls[selected_category]
  46.                 if category_url:
  47.                     search_by_url(category_url)
  48.                 else:
  49.                     result_text.insert(tk.END, f"Error: Category URL is not provided for {selected_category}")
  50.             else:
  51.                 result_text.insert(tk.END, f"Error: Category '{selected_category}' not found")
  52.     except Exception as e:
  53.         result_text.insert(tk.END, f"Error: {str(e)}")
  54.  
  55. def process_m3u_data(m3u_data):
  56.     # Clear any previous results
  57.     result_text.delete(0, tk.END)
  58.     # Store channels' names and URLs
  59.     global channels_info
  60.     channels_info = {}
  61.     # Extract channel names and URLs
  62.     channel_name = None
  63.     for line in m3u_data:
  64.         if line.startswith('#EXTINF:'):
  65.             channel_name = line.split(',')[-1]
  66.         elif line.startswith('http') and channel_name:
  67.             channels_info[channel_name] = line.strip()
  68.             # Insert channel name into the listbox
  69.             result_text.insert(tk.END, channel_name)
  70.             channel_name = None
  71.  
  72. def play_selected_channel(event):
  73.     try:
  74.         # Get the selected channel name
  75.         selected_channel = result_text.get(tk.ACTIVE)
  76.        
  77.         # Check if the selected channel is a local file path
  78.         if os.path.exists(selected_channel):
  79.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", selected_channel])
  80.         else:
  81.             # Open the corresponding URL in VLC
  82.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", channels_info[selected_channel]])
  83.     except (tk.TclError, KeyError):
  84.         pass  # Ignore if no channel is selected or channel info is missing
  85.  
  86. def check_links():
  87.     global working_links
  88.     working_links = {}
  89.     for channel_name, url in channels_info.items():
  90.         try:
  91.             response = requests.get(url)
  92.             if response.status_code == 200:
  93.                 working_links[channel_name] = url
  94.         except requests.RequestException:
  95.             pass
  96.  
  97.     # Display working links in the result_text widget
  98.     result_text.delete(0, tk.END)
  99.     for channel_name, url in working_links.items():
  100.         result_text.insert(tk.END, f"{channel_name}: {url}\n")
  101.  
  102. def save_working_links():
  103.     with open("working_channels.m3u", "w", encoding="utf-8") as f:
  104.         for channel_name, url in working_links.items():
  105.             f.write(f"#EXTINF:-1,{channel_name}\n{url}\n")
  106.  
  107. # Create the main application window
  108. app = tk.Tk()
  109. app.title("Najeeb IPTV Channel Search M3u All Category")
  110. app.geometry("800x600")
  111. app.configure(bg="#336699")
  112.  
  113. # Add labels, entry fields, buttons, etc.
  114. url_frame = tk.Frame(app, bg="#336699")
  115. url_frame.pack(pady=10)
  116.  
  117. url_label = tk.Label(url_frame, text="Enter URL 0R local OR Select Category:", bg="#336699", fg="white")
  118. url_label.pack(side=tk.LEFT, padx=5)
  119.  
  120. url_entry = tk.Entry(url_frame, width=80)  # Adjust width here
  121. url_entry.pack(side=tk.LEFT, padx=5)
  122.  
  123. search_button = tk.Button(url_frame, text="Search", command=search_channels, bg="#FFA500", fg="white")
  124. search_button.pack(side=tk.LEFT, padx=5)
  125.  
  126. result_label = tk.Label(app, text="Najeeb Iptv Channels Run Vlc Player Check Working Or Not Working And save Working URLS in M3U File:", bg="#336699", fg="white")
  127. result_label.pack()
  128.  
  129. # Add scrollbar to the listbox
  130. scrollbar = tk.Scrollbar(app, orient=tk.VERTICAL)
  131. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  132.  
  133. result_text = tk.Listbox(app, height=30, width=130, yscrollcommand=scrollbar.set)
  134. result_text.pack()
  135.  
  136. # Configure scrollbar
  137. scrollbar.config(command=result_text.yview)
  138.  
  139. # Bind double click event to play_selected_channel function
  140. result_text.bind("<Double-1>", play_selected_channel)
  141.  
  142. # Add button to check links
  143. check_button = tk.Button(app, text="Check Links", command=check_links, bg="#008000", fg="white")
  144. check_button.pack(side=tk.RIGHT, padx=5)
  145.  
  146. # Add button to save working links
  147. save_button = tk.Button(app, text="Save Working Links", command=save_working_links, bg="#FF0000", fg="white")
  148. save_button.pack(side=tk.LEFT, padx=5)
  149.  
  150. # Dropdown menu for channel categories
  151. category_urls = {
  152.     "NAJEEB-IPTV": "",
  153.     "ALL-INDEX": "https://iptv-org.github.io/iptv/index.m3u",
  154.     "CATEGORY": "https://iptv-org.github.io/iptv/index.category.m3u",
  155.     "LANGUAGE": "https://iptv-org.github.io/iptv/index.language.m3u",
  156.     "COUNTRY": "https://iptv-org.github.io/iptv/index.country.m3u",
  157.     "REGION": "https://iptv-org.github.io/iptv/index.region.m3u",
  158.     # Add more categories here
  159. }
  160.  
  161. category_var = tk.StringVar(app)
  162. category_var.set("NAJEEB-IPTV")  # default value
  163. category_dropdown = ttk.OptionMenu(app, category_var, *category_urls.keys())
  164. category_dropdown.pack(pady=10)
  165.  
  166. # Global variable to store channels' info
  167. channels_info = {}
  168. working_links = {}
  169.  
  170. # Run the application loop
  171. app.mainloop()
  172.  
Add Comment
Please, Sign In to add comment