Skip to content

Commit f916269

Browse files
authored
Update m3u playlist builder (#231)
1 parent 937e72b commit f916269

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

mediaflow_proxy/routes/playlist_builder.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,6 @@ def rewrite_m3u_links_streaming(
143143
if key:
144144
processed_url_content += f"&key={key}"
145145

146-
# Aggiungi chiavi da #KODIPROP se presenti
147-
license_key = current_kodi_props.get("inputstream.adaptive.license_key")
148-
if license_key and ":" in license_key:
149-
key_id_kodi, key_kodi = license_key.split(":", 1)
150-
processed_url_content += f"&key_id={key_id_kodi}"
151-
processed_url_content += f"&key={key_kodi}"
152-
153146
elif ".php" in logical_line:
154147
encoded_url = urllib.parse.quote(logical_line, safe="")
155148
processed_url_content = f"{base_url}/proxy/hls/manifest.m3u8?d={encoded_url}"
@@ -158,6 +151,16 @@ def rewrite_m3u_links_streaming(
158151
encoded_url = urllib.parse.quote(logical_line, safe="")
159152
processed_url_content = f"{base_url}/proxy/hls/manifest.m3u8?d={encoded_url}"
160153

154+
# Aggiungi chiavi da #KODIPROP se presenti
155+
license_key = current_kodi_props.get("inputstream.adaptive.license_key")
156+
if license_key and ":" in license_key:
157+
key_id_kodi, key_kodi = license_key.split(":", 1)
158+
# Aggiungi key_id e key solo se non sono già stati aggiunti (es. dall'URL MPD)
159+
if "&key_id=" not in processed_url_content:
160+
processed_url_content += f"&key_id={key_id_kodi}"
161+
if "&key=" not in processed_url_content:
162+
processed_url_content += f"&key={key_kodi}"
163+
161164
# Applica gli header raccolti prima di api_password
162165
if current_ext_headers:
163166
header_params_str = "".join(
@@ -256,7 +259,7 @@ async def async_generate_combined_playlist(playlist_definitions: list[str], base
256259
)
257260

258261
# Raggruppa le playlist da ordinare e quelle da non ordinare
259-
sorted_playlist_lines = []
262+
channel_entries_to_sort = []
260263
unsorted_playlists_data = []
261264

262265
for idx, result in enumerate(results):
@@ -269,7 +272,10 @@ async def async_generate_combined_playlist(playlist_definitions: list[str], base
269272
continue
270273

271274
if task_info.get("sort", False):
272-
sorted_playlist_lines.extend(result)
275+
# Se la playlist deve essere ordinata, estraiamo i canali e li mettiamo nel pool globale da ordinare
276+
entries = parse_channel_entries(result)
277+
for entry_lines in entries:
278+
channel_entries_to_sort.append((entry_lines, task_info["proxy"]))
273279
else:
274280
unsorted_playlists_data.append({"lines": result, "proxy": task_info["proxy"]})
275281

@@ -292,28 +298,18 @@ def yield_header_once(lines_iter):
292298
first_playlist_header_handled = True
293299

294300
# 1. Processa e ordina le playlist marcate con 'sort'
295-
if sorted_playlist_lines:
296-
# Extract channel entries and keep proxy info
297-
channel_entries_with_proxy_info = []
298-
for idx, result in enumerate(results):
299-
task_info = download_tasks[idx]
300-
if task_info.get("sort") and isinstance(result, list):
301-
entries = parse_channel_entries(result) # result is the list of playlist lines
302-
for entry_lines in entries:
303-
# The proxy option applies to the entire channel block
304-
channel_entries_with_proxy_info.append((entry_lines, task_info["proxy"]))
305-
306-
# Sort entries by channel name (from #EXTINF)
301+
if channel_entries_to_sort:
302+
# Sort all entries from ALL sorted playlists together by channel name (from #EXTINF)
307303
# The first line of each entry is always #EXTINF
308-
channel_entries_with_proxy_info.sort(key=lambda x: x[0][0].split(",")[-1].strip())
304+
channel_entries_to_sort.sort(key=lambda x: x[0][0].split(",")[-1].strip().lower())
309305

310306
# Handle the header only once for the sorted block
311307
if not first_playlist_header_handled:
312308
yield "#EXTM3U\n"
313309
first_playlist_header_handled = True
314310

315311
# Apply link rewriting selectively
316-
for entry_lines, should_proxy in channel_entries_with_proxy_info:
312+
for entry_lines, should_proxy in channel_entries_to_sort:
317313
# The URL is the last line of the entry
318314
url = entry_lines[-1]
319315
# Yield tutte le righe prima dell'URL

0 commit comments

Comments
 (0)