From 47aaef4053d68c0af736e292cb8d36743bd0b808 Mon Sep 17 00:00:00 2001 From: Ignacio Rivero Date: Mon, 10 Mar 2025 02:37:03 -0300 Subject: [PATCH] Fixed groups/categories --- channels.py | 103 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 42 deletions(-) diff --git a/channels.py b/channels.py index 390fc73..a9b3a01 100755 --- a/channels.py +++ b/channels.py @@ -7,51 +7,70 @@ import sys from utils import m3u_url def get_channels(): - """ - Get a list of channels from the M3U playlist. - - Returns: - list: A list of channel dictionaries with name, url, logo, and group. - """ - if m3u_url: - try: - response = requests.get(m3u_url) - response.raise_for_status() # Raise exception for HTTP errors - except requests.RequestException as e: - print(f"Error fetching M3U playlist: {e}") - return [] - else: - print("Error: IPMPV_M3U_URL not set. Please set this environment variable to the URL of your IPTV list, in M3U format.") - sys.exit(1) - - lines = response.text.splitlines() + """ + Get a list of channels from the M3U playlist. + + Returns: + list: A list of channel dictionaries with name, url, logo, and group. + """ + if m3u_url: + try: + response = requests.get(m3u_url) + response.raise_for_status() # Raise exception for HTTP errors + except requests.RequestException as e: + print(f"Error fetching M3U playlist: {e}") + return [] + else: + print("Error: IPMPV_M3U_URL not set. Please set this environment variable to the URL of your IPTV list, in M3U format.") + sys.exit(1) + + lines = response.text.splitlines() - channels = [] - regex = re.compile(r'tvg-logo="(.*?)".*?group-title="(.*?)"', re.IGNORECASE) + channels = [] + # Match tvg-logo and group-title attributes + logo_group_regex = re.compile(r'tvg-logo="(.*?)".*?group-title="(.*?)"', re.IGNORECASE) - for i in range(len(lines)): - if lines[i].startswith("#EXTINF"): - match = regex.search(lines[i]) - logo = match.group(1) if match else "" - group = match.group(2) if match else "Other" - name = lines[i].split(",")[-1] - url = lines[i + 1] + for i in range(len(lines)): + if lines[i].startswith("#EXTINF"): + match = logo_group_regex.search(lines[i]) + + logo = match.group(1) if match else "" + + # Handle multiple groups separated by semicolons + groups = [] + if match and match.group(2): + # Split by semicolon and create a channel entry for each group + groups = [group.strip() for group in match.group(2).split(';')] + + # Default to "Other" if no groups found + if not groups: + groups = ["Other"] + + name = lines[i].split(",")[-1] + url = lines[i + 1] - channels.append({"name": name, "url": url, "logo": logo, "group": group}) + # Create a channel entry for the primary group + primary_group = groups[0] + channels.append({"name": name, "url": url, "logo": logo, "group": primary_group}) + + # Create additional channel entries for secondary groups if present + for group in groups[1:]: + if group: # Skip empty groups + channels.append({"name": name, "url": url, "logo": logo, "group": group}) - return channels + return channels def group_channels(channels): - """ - Group channels by their group title. - - Args: - channels (list): List of channel dictionaries. - - Returns: - dict: Dictionary of channel groups. - """ - grouped_channels = {} - for channel in channels: - grouped_channels.setdefault(channel["group"], []).append(channel) - return grouped_channels + """ + Group channels by their group title. + + Args: + channels (list): List of channel dictionaries. + + Returns: + dict: Dictionary of channel groups. + """ + grouped_channels = {} + for channel in channels: + grouped_channels.setdefault(channel["group"], []).append(channel) + return grouped_channels