Najeebsk

IPTV-CH-SAVE.pyw

May 15th, 2024 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.21 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', encoding='utf-8') 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', encoding='utf-8') 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 in the desired format
  59.     playlist_data = ""
  60.     # Extract channel names and URLs
  61.     channel_name = None
  62.     for line in m3u_data:
  63.         if line.startswith('#EXTINF:'):
  64.             channel_name = line.split(',')[-1]
  65.         elif line.startswith('http') and channel_name:
  66.             playlist_data += f"{channel_name} {line.strip()}\n"
  67.             # Insert channel name into the listbox
  68.             result_text.insert(tk.END, channel_name)
  69.             channel_name = None
  70.    
  71.     # Save the playlist data to a text file
  72.     with open("Playlist.txt", "w", encoding="utf-8") as playlist_file:
  73.         playlist_file.write(playlist_data)
  74.  
  75. def play_selected_channel(event):
  76.     try:
  77.         # Get the selected channel name
  78.         selected_channel = result_text.get(tk.ACTIVE)
  79.        
  80.         # Check if the selected channel is a local file path
  81.         if os.path.exists(selected_channel):
  82.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", selected_channel])
  83.         else:
  84.             # Open the corresponding URL in VLC
  85.             subprocess.Popen([r"C:\Program Files\VideoLAN\VLC\vlc.exe", channels_info[selected_channel]])
  86.     except (tk.TclError, KeyError):
  87.         pass  # Ignore if no channel is selected or channel info is missing
  88.  
  89. def check_links():
  90.     global working_links
  91.     working_links = {}
  92.     for channel_name, url in channels_info.items():
  93.         try:
  94.             response = requests.get(url)
  95.             if response.status_code == 200:
  96.                 working_links[channel_name] = url
  97.         except requests.RequestException:
  98.             pass
  99.  
  100.     # Display working links in the result_text widget
  101.     result_text.delete(0, tk.END)
  102.     for channel_name, url in working_links.items():
  103.         result_text.insert(tk.END, f"{channel_name}: {url}\n")
  104.  
  105. def save_working_links():
  106.     with open("working_channels.m3u", "w", encoding="utf-8") as f:
  107.         for channel_name, url in working_links.items():
  108.             f.write(f"#EXTINF:-1,{channel_name}\n{url}\n")
  109.  
  110. # Create the main application window
  111. app = tk.Tk()
  112. app.title("Najeeb IPTV Channel Search M3u All Category")
  113. app.geometry("800x620")
  114. app.configure(bg="#336699")
  115.  
  116.  
  117. # Add labels, entry fields, buttons, etc.
  118. url_frame = tk.Frame(app, bg="#336699")
  119. url_frame.pack(pady=10)
  120.  
  121. url_label = tk.Label(url_frame, text="Enter URL 0R local OR Select Category:", bg="#336699", fg="white")
  122. url_label.pack(side=tk.LEFT, padx=5)
  123.  
  124. url_entry = tk.Entry(url_frame, width=80)  # Adjust width here
  125. url_entry.pack(side=tk.LEFT, padx=5)
  126.  
  127. search_button = tk.Button(url_frame, text="Search", command=search_channels, bg="#FFA500", fg="white")
  128. search_button.pack(side=tk.LEFT, padx=5)
  129.  
  130. 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")
  131. result_label.pack()
  132.  
  133. # Add search field
  134. search_frame = tk.Frame(app, bg="#336699")
  135. search_frame.pack()
  136.  
  137. search_label = tk.Label(search_frame, text="Search Channel Name :", bg="#336699", fg="white")
  138. search_label.pack(side=tk.LEFT, padx=0)
  139.  
  140. search_entry = tk.Entry(search_frame, width=105)  # Adjust width here
  141. search_entry.pack(side=tk.LEFT, padx=5)
  142.  
  143. def filter_channels(event=None):
  144.     keyword = search_entry.get().lower()
  145.     result_text.delete(0, tk.END)
  146.     for channel_name, url in channels_info.items():
  147.         if keyword in channel_name.lower():
  148.             result_text.insert(tk.END, channel_name)
  149.  
  150. search_entry.bind('<KeyRelease>', filter_channels)
  151.  
  152. # Add scrollbar to the listbox
  153. scrollbar = tk.Scrollbar(app, orient=tk.VERTICAL)
  154. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  155.  
  156. result_text = tk.Listbox(app, height=30, width=130, yscrollcommand=scrollbar.set)
  157. result_text.pack()
  158.  
  159. # Configure scrollbar
  160. scrollbar.config(command=result_text.yview)
  161.  
  162. # Bind double click event to play_selected_channel function
  163. result_text.bind("<Double-1>", play_selected_channel)
  164.  
  165. # Add button to check links
  166. check_button = tk.Button(app, text="Check Links", command=check_links, bg="#008000", fg="white")
  167. check_button.pack(side=tk.RIGHT, padx=5)
  168.  
  169. # Add button to save working links
  170. save_button = tk.Button(app, text="Save Working Links", command=save_working_links, bg="#FF0000", fg="white")
  171. save_button.pack(side=tk.LEFT, padx=5)
  172.  
  173. # Dropdown menu for channel categories
  174. category_urls = {
  175.     "NAJEEB-IPTV": "",
  176.     "ALL-INDEX": "https://iptv-org.github.io/iptv/index.m3u",
  177.     "CATEGORY": "https://iptv-org.github.io/iptv/index.category.m3u",
  178.     "LANGUAGE": "https://iptv-org.github.io/iptv/index.language.m3u",
  179.     "COUNTRY": "https://iptv-org.github.io/iptv/index.country.m3u",
  180.     "REGION": "https://iptv-org.github.io/iptv/index.region.m3u",
  181.     # Add more categories here
  182. }
  183.  
  184. category_var = tk.StringVar(app)
  185. category_var.set("NAJEEB-IPTV")  # default value
  186. category_dropdown = ttk.OptionMenu(app, category_var, *category_urls.keys())
  187. category_dropdown.pack(pady=10)
  188.  
  189. # Global variable to store channels' info
  190. channels_info = {}
  191. working_links = {}
  192.  
  193. # Run the application loop
  194. app.mainloop()
  195.  
Add Comment
Please, Sign In to add comment