mirror of
https://github.com/theariatv/theariatv.github.io.git
synced 2026-11-19 14:18:27 +01:00
Enhance playlist update functionality
Refactor channel parsing and markdown generation logic to preserve user edits. Add function to parse existing markdown and merge with new data.
This commit is contained in:
parent
30707701ff
commit
485b0f7cd3
1 changed files with 92 additions and 25 deletions
|
|
@ -15,7 +15,6 @@ def parse_m3u(file_path, stream_type):
|
|||
|
||||
current_channel = {}
|
||||
|
||||
# Regular expressions for individual attributes
|
||||
group_pattern = re.compile(r'group-title="([^"]+)"')
|
||||
logo_pattern = re.compile(r'tvg-logo="([^"]+)"')
|
||||
id_pattern = re.compile(r'tvg-id="([^"]+)"')
|
||||
|
|
@ -26,11 +25,9 @@ def parse_m3u(file_path, stream_type):
|
|||
continue
|
||||
|
||||
if line.startswith("#EXTINF:"):
|
||||
# Get the channel name (everything after the last comma)
|
||||
parts = line.split(",")
|
||||
name = parts[-1].strip() if len(parts) > 1 else "Unknown Channel"
|
||||
|
||||
# Get attributes using regex
|
||||
match_group = group_pattern.search(line)
|
||||
match_logo = logo_pattern.search(line)
|
||||
match_id = id_pattern.search(line)
|
||||
|
|
@ -53,63 +50,133 @@ def parse_m3u(file_path, stream_type):
|
|||
|
||||
return channels
|
||||
|
||||
def parse_existing_markdown(file_path):
|
||||
"""
|
||||
Reads an existing markdown file and extracts its title and channel records.
|
||||
This prevents overwriting manual user edits.
|
||||
"""
|
||||
title = ""
|
||||
existing_channels = {}
|
||||
|
||||
if not os.path.exists(file_path):
|
||||
return title, existing_channels
|
||||
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
|
||||
# Capture the custom heading (e.g., "# Czech Republic 🇨🇿")
|
||||
if line.startswith('#') and not title:
|
||||
title = line
|
||||
|
||||
# Parse the table rows (skip headers)
|
||||
elif line.startswith('|') and not ':-:' in line and not '| # |' in line:
|
||||
parts = [p.strip() for p in line.split('|')]
|
||||
|
||||
# Check if it's a valid row with exactly 8 pipe separations
|
||||
# (empty start, num, name, link, logo, epg, type, empty end)
|
||||
if len(parts) >= 8:
|
||||
name = parts[2]
|
||||
|
||||
# Safely extract the raw URL from the markdown link syntax [>](url)
|
||||
url_match = re.search(r'\[.*?\]\((.*?)\)', parts[3])
|
||||
url = url_match.group(1) if url_match else parts[3]
|
||||
|
||||
logo_col = parts[4]
|
||||
epg_id = parts[5]
|
||||
c_type = parts[6]
|
||||
|
||||
# Store existing records
|
||||
existing_channels[name] = {
|
||||
'url': url,
|
||||
'logo_col': logo_col,
|
||||
'epg_id': epg_id,
|
||||
'type': c_type
|
||||
}
|
||||
|
||||
return title, existing_channels
|
||||
|
||||
def generate_markdown_by_state(stable_channels, unstable_channels, output_dir="channels"):
|
||||
"""
|
||||
Groups channels by state and generates a separate MD file for each
|
||||
according to the specified structure.
|
||||
Merges parsed M3U channels with existing markdown files without losing manual edits.
|
||||
"""
|
||||
all_channels = stable_channels + unstable_channels
|
||||
grouped_channels = {}
|
||||
|
||||
# Group by state
|
||||
# Group by state with Title Case normalization
|
||||
for ch in all_channels:
|
||||
state = ch['state']
|
||||
state = ch['state'].strip().title()
|
||||
if state not in grouped_channels:
|
||||
grouped_channels[state] = []
|
||||
grouped_channels[state].append(ch)
|
||||
|
||||
# Create the target directory if it doesn't exist
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# Generate files
|
||||
# Merge and generate files
|
||||
for state, channels in grouped_channels.items():
|
||||
# Safely format the filename (e.g., "CZ - Czechia" -> "cz___czechia")
|
||||
safe_filename = "".join([c if c.isalnum() else "_" for c in state]).lower()
|
||||
safe_filename = re.sub(r'_+', '_', safe_filename).strip('_')
|
||||
|
||||
file_path = os.path.join(output_dir, f"{safe_filename}.md")
|
||||
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
# Markdown heading
|
||||
f.write(f"# {state}\n\n")
|
||||
# Load existing data to avoid destruction of manual edits
|
||||
existing_title, existing_channels = parse_existing_markdown(file_path)
|
||||
|
||||
# Keep custom title if it exists, otherwise generate a default one
|
||||
if not existing_title:
|
||||
existing_title = f"# {state}"
|
||||
|
||||
# Merge new M3U data into existing channels
|
||||
for ch in channels:
|
||||
name = ch['name']
|
||||
new_logo_col = f'<img height="20" src="{ch["logo"]}"/>' if ch["logo"] else name
|
||||
|
||||
# Table header exactly matching your template
|
||||
if name in existing_channels:
|
||||
# Update URL and Type because stream sources change frequently
|
||||
existing_channels[name]['url'] = ch['url']
|
||||
existing_channels[name]['type'] = ch['type']
|
||||
|
||||
# Only overwrite the Logo if the current one is just text (or missing) and M3U provided a real logo
|
||||
if ch['logo'] and (existing_channels[name]['logo_col'] == name or existing_channels[name]['logo_col'] == ""):
|
||||
existing_channels[name]['logo_col'] = new_logo_col
|
||||
|
||||
# Only overwrite EPG ID if the current one is missing/placeholder
|
||||
if ch['epg_id'] != " " and (existing_channels[name]['epg_id'] == " " or existing_channels[name]['epg_id'] == ""):
|
||||
existing_channels[name]['epg_id'] = ch['epg_id']
|
||||
else:
|
||||
# Append completely new channel from the M3U playlist
|
||||
existing_channels[name] = {
|
||||
'url': ch['url'],
|
||||
'logo_col': new_logo_col,
|
||||
'epg_id': ch['epg_id'],
|
||||
'type': ch['type']
|
||||
}
|
||||
|
||||
# Write the merged content back to the Markdown file
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(f"{existing_title}\n\n")
|
||||
f.write("| # | Channel | Link | Logo | EPG id | Type |\n")
|
||||
f.write("|:-:|:-------:|:----:|:----:|:------:|:----:|\n")
|
||||
|
||||
# Print channels with numbering
|
||||
for i, ch in enumerate(channels, start=1):
|
||||
# Handle missing logo - replace with the channel name
|
||||
logo_col = f'<img height="20" src="{ch["logo"]}"/>' if ch["logo"] else ch["name"]
|
||||
# Iterate over the merged dictionary and number them sequentially
|
||||
for i, (name, data) in enumerate(existing_channels.items(), start=1):
|
||||
f.write(f"| {i} | {name} | [>]({data['url']}) | {data['logo_col']} | {data['epg_id']} | {data['type']} |\n")
|
||||
|
||||
# Write the row
|
||||
f.write(f"| {i} | {ch['name']} | [>]({ch['url']}) | {logo_col} | {ch['epg_id']} | {ch['type']} |\n")
|
||||
|
||||
print(f"Generated file: {file_path} ({len(channels)} channels)")
|
||||
print(f"Updated file: {file_path} ({len(existing_channels)} channels)")
|
||||
|
||||
if __name__ == "__main__":
|
||||
stable_file = "aria.m3u"
|
||||
unstable_file = "aria+.m3u"
|
||||
|
||||
print("Starting extraction from playlists...")
|
||||
print("Starting extraction and merging from playlists...")
|
||||
stable_data = parse_m3u(stable_file, "stable")
|
||||
unstable_data = parse_m3u(unstable_file, "unstable")
|
||||
|
||||
total_channels = len(stable_data) + len(unstable_data)
|
||||
|
||||
if total_channels > 0:
|
||||
print(f"Total channels loaded: {total_channels}. Splitting by state...")
|
||||
print(f"Total channels loaded: {total_channels}. Splitting by state and merging...")
|
||||
generate_markdown_by_state(stable_data, unstable_data)
|
||||
print("All MD files have been successfully generated in the 'channels/' directory.")
|
||||
print("All MD files have been successfully updated in the 'channels/' directory.")
|
||||
else:
|
||||
print("No channels found. Please check the .m3u files.")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue