From 5cb2be7c9334af7cc28ed7dbbbd0b3371c302dab Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Tue, 3 Jun 2025 19:37:57 -0500 Subject: [PATCH] Add support for dynamic tvg-id source selection in EPG generation tvg_id_source accepts tvg_id, gracenote or the default if nothing is selected channel_number --- apps/output/views.py | 53 +++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/apps/output/views.py b/apps/output/views.py index bbcc9269..eb00270d 100644 --- a/apps/output/views.py +++ b/apps/output/views.py @@ -199,18 +199,33 @@ def generate_epg(request, profile_name=None): else: channels = Channel.objects.all() + # Check if the request wants to use direct logo URLs instead of cache + use_cached_logos = request.GET.get('cachedlogos', 'true').lower() != 'false' + + # Get the source to use for tvg-id value + # Options: 'channel_number' (default), 'tvg_id', 'gracenote' + tvg_id_source = request.GET.get('tvg_id_source', 'channel_number').lower() + # Retrieve all active channels for channel in channels: # Format channel number as integer if it has no decimal component - same as M3U generation if channel.channel_number is not None: if channel.channel_number == int(channel.channel_number): - formatted_channel_number = str(int(channel.channel_number)) + formatted_channel_number = int(channel.channel_number) else: - formatted_channel_number = str(channel.channel_number) + formatted_channel_number = channel.channel_number else: - formatted_channel_number = str(channel.id) - # Check if the request wants to use direct logo URLs instead of cache - use_cached_logos = request.GET.get('cachedlogos', 'true').lower() != 'false' + formatted_channel_number = "" + + # Determine the channel ID based on the selected source + if tvg_id_source == 'tvg_id' and channel.tvg_id: + channel_id = channel.tvg_id + elif tvg_id_source == 'gracenote' and channel.tvc_guide_stationid: + channel_id = channel.tvc_guide_stationid + else: + # Default to channel number (original behavior) + channel_id = str(formatted_channel_number) if formatted_channel_number != "" else str(channel.id) + # Add channel logo if available tvg_logo = "" if channel.logo: @@ -226,21 +241,29 @@ def generate_epg(request, profile_name=None): else: tvg_logo = request.build_absolute_uri(reverse('api:channels:logo-cache', args=[channel.logo.id])) display_name = channel.epg_data.name if channel.epg_data else channel.name - xml_lines.append(f' ') + xml_lines.append(f' ') xml_lines.append(f' {html.escape(display_name)}') xml_lines.append(f' ') xml_lines.append(' ') for channel in channels: - # Use the same formatting for channel ID in program entries - if channel.channel_number is not None: - if channel.channel_number == int(channel.channel_number): - formatted_channel_number = str(int(channel.channel_number)) - else: - formatted_channel_number = str(channel.channel_number) + # Use the same channel ID determination for program entries + if tvg_id_source == 'tvg_id' and channel.tvg_id: + channel_id = channel.tvg_id + elif tvg_id_source == 'gracenote' and channel.tvc_guide_stationid: + channel_id = channel.tvc_guide_stationid else: - formatted_channel_number = str(channel.id) + # Get formatted channel number + if channel.channel_number is not None: + if channel.channel_number == int(channel.channel_number): + formatted_channel_number = int(channel.channel_number) + else: + formatted_channel_number = channel.channel_number + else: + formatted_channel_number = "" + # Default to channel number + channel_id = str(formatted_channel_number) if formatted_channel_number != "" else str(channel.id) display_name = channel.epg_data.name if channel.epg_data else channel.name if not channel.epg_data: @@ -249,7 +272,7 @@ def generate_epg(request, profile_name=None): num_days = 1 # Default to 1 days of dummy EPG data program_length_hours = 4 # Default to 4-hour program blocks generate_dummy_epg( - formatted_channel_number, + channel_id, display_name, xml_lines, num_days=num_days, @@ -260,7 +283,7 @@ def generate_epg(request, profile_name=None): for prog in programs: start_str = prog.start_time.strftime("%Y%m%d%H%M%S %z") stop_str = prog.end_time.strftime("%Y%m%d%H%M%S %z") - xml_lines.append(f' ') + xml_lines.append(f' ') xml_lines.append(f' {html.escape(prog.title)}') # Add subtitle if available