Fixed groups/categories

This commit is contained in:
Ignacio Rivero 2025-03-10 02:37:03 -03:00
parent dd34e76002
commit 47aaef4053

View File

@ -7,51 +7,70 @@ import sys
from utils import m3u_url from utils import m3u_url
def get_channels(): def get_channels():
""" """
Get a list of channels from the M3U playlist. Get a list of channels from the M3U playlist.
Returns: Returns:
list: A list of channel dictionaries with name, url, logo, and group. list: A list of channel dictionaries with name, url, logo, and group.
""" """
if m3u_url: if m3u_url:
try: try:
response = requests.get(m3u_url) response = requests.get(m3u_url)
response.raise_for_status() # Raise exception for HTTP errors response.raise_for_status() # Raise exception for HTTP errors
except requests.RequestException as e: except requests.RequestException as e:
print(f"Error fetching M3U playlist: {e}") print(f"Error fetching M3U playlist: {e}")
return [] return []
else: else:
print("Error: IPMPV_M3U_URL not set. Please set this environment variable to the URL of your IPTV list, in M3U format.") 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) sys.exit(1)
lines = response.text.splitlines() lines = response.text.splitlines()
channels = [] channels = []
regex = re.compile(r'tvg-logo="(.*?)".*?group-title="(.*?)"', re.IGNORECASE) # 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)): for i in range(len(lines)):
if lines[i].startswith("#EXTINF"): if lines[i].startswith("#EXTINF"):
match = regex.search(lines[i]) match = logo_group_regex.search(lines[i])
logo = match.group(1) if match else ""
group = match.group(2) if match else "Other" logo = match.group(1) if match else ""
name = lines[i].split(",")[-1]
url = lines[i + 1] # 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): def group_channels(channels):
""" """
Group channels by their group title. Group channels by their group title.
Args: Args:
channels (list): List of channel dictionaries. channels (list): List of channel dictionaries.
Returns: Returns:
dict: Dictionary of channel groups. dict: Dictionary of channel groups.
""" """
grouped_channels = {} grouped_channels = {}
for channel in channels: for channel in channels:
grouped_channels.setdefault(channel["group"], []).append(channel) grouped_channels.setdefault(channel["group"], []).append(channel)
return grouped_channels return grouped_channels