import os import glob import re # Directory configuration INPUT_DIR = "./channels" # Directory containing the state .md files OUTPUT_DIR = "." # ROOT directory for the generated .m3u files # Dictionary for automatic flag assignment FLAG_MAP = { "United States": "๐Ÿ‡บ๐Ÿ‡ธ", "Guyana": "๐Ÿ‡ฌ๐Ÿ‡พ", "Cyprus": "๐Ÿ‡จ๐Ÿ‡พ", "Tรผrkyie": "๐Ÿ‡น๐Ÿ‡ท", "Turkey": "๐Ÿ‡น๐Ÿ‡ท", "Slovakia": "๐Ÿ‡ธ๐Ÿ‡ฐ", "Japan": "๐Ÿ‡ฏ๐Ÿ‡ต", "Iceland": "๐Ÿ‡ฎ๐Ÿ‡ธ", "Spain": "๐Ÿ‡ช๐Ÿ‡ธ", "Columbia": "๐Ÿ‡จ๐Ÿ‡ด", "Colombia": "๐Ÿ‡จ๐Ÿ‡ด", "Argentina": "๐Ÿ‡ฆ๐Ÿ‡ท", "Australia": "๐Ÿ‡ฆ๐Ÿ‡บ", "Finland": "๐Ÿ‡ซ๐Ÿ‡ฎ", "North Macedonia": "๐Ÿ‡ฒ๐Ÿ‡ฐ", "Germany": "๐Ÿ‡ฉ๐Ÿ‡ช", "Portugal": "๐Ÿ‡ต๐Ÿ‡น", "Singapore": "๐Ÿ‡ธ๐Ÿ‡ฌ", "Brazil": "๐Ÿ‡ง๐Ÿ‡ท", "Lithuania": "๐Ÿ‡ฑ๐Ÿ‡น", "Philippines": "๐Ÿ‡ต๐Ÿ‡ญ", "Poland": "๐Ÿ‡ต๐Ÿ‡ฑ", "Belarus": "๐Ÿ‡ง๐Ÿ‡พ", "Bulgaria": "๐Ÿ‡ง๐Ÿ‡ฌ", "Vietnam": "๐Ÿ‡ป๐Ÿ‡ณ", "San Marino": "๐Ÿ‡ธ๐Ÿ‡ฒ", "Norway": "๐Ÿ‡ณ๐Ÿ‡ด", "Montenegro": "๐Ÿ‡ฒ๐Ÿ‡ช", "United Arab Emirates": "๐Ÿ‡ฆ๐Ÿ‡ช", "Israel": "๐Ÿ‡ฎ๐Ÿ‡ฑ", "Iran": "๐Ÿ‡ฎ๐Ÿ‡ท", "Mexico": "๐Ÿ‡ฒ๐Ÿ‡ฝ", "Albania": "๐Ÿ‡ฆ๐Ÿ‡ฑ", "Greece": "๐Ÿ‡ฌ๐Ÿ‡ท", "Slovenia": "๐Ÿ‡ธ๐Ÿ‡ฎ", "North Korea": "๐Ÿ‡ฐ๐Ÿ‡ต", "Switzerland": "๐Ÿ‡จ๐Ÿ‡ญ", "Bosnia and Herzegovina": "๐Ÿ‡ง๐Ÿ‡ฆ", "Serbia": "๐Ÿ‡ท๐Ÿ‡ธ", "Vatican City": "๐Ÿ‡ป๐Ÿ‡ฆ", "Croatia": "๐Ÿ‡ญ๐Ÿ‡ท", "Denmark": "๐Ÿ‡ฉ๐Ÿ‡ฐ", "Monaco": "๐Ÿ‡ฒ๐Ÿ‡จ", "Andorra": "๐Ÿ‡ฆ๐Ÿ‡ฉ", "Ukraine": "๐Ÿ‡บ๐Ÿ‡ฆ", "Austria": "๐Ÿ‡ฆ๐Ÿ‡น", "New Zealand": "๐Ÿ‡ณ๐Ÿ‡ฟ", "Moldova": "๐Ÿ‡ฒ๐Ÿ‡ฉ", "Russia": "๐Ÿ‡ท๐Ÿ‡บ", "Netherlands": "๐Ÿ‡ณ๐Ÿ‡ฑ", "Estonia": "๐Ÿ‡ช๐Ÿ‡ช", "Italy": "๐Ÿ‡ฎ๐Ÿ‡น", "Romania": "๐Ÿ‡ท๐Ÿ‡ด", "Chile": "๐Ÿ‡จ๐Ÿ‡ฑ", "France": "๐Ÿ‡ซ๐Ÿ‡ท", "South Africa": "๐Ÿ‡ฟ๐Ÿ‡ฆ", "Georgia": "๐Ÿ‡ฌ๐Ÿ‡ช", "Belgium": "๐Ÿ‡ง๐Ÿ‡ช", "Costa Rica": "๐Ÿ‡จ๐Ÿ‡ท", "India": "๐Ÿ‡ฎ๐Ÿ‡ณ", "United Kingdom": "๐Ÿ‡ฌ๐Ÿ‡ง", "Czech Republic": "๐Ÿ‡จ๐Ÿ‡ฟ", "Czechia": "๐Ÿ‡จ๐Ÿ‡ฟ", "Hungary": "๐Ÿ‡ญ๐Ÿ‡บ", "China": "๐Ÿ‡จ๐Ÿ‡ณ", "South Korea": "๐Ÿ‡ฐ๐Ÿ‡ท", "Sweden": "๐Ÿ‡ธ๐Ÿ‡ช", "Faroe Islands": "๐Ÿ‡ซ๐Ÿ‡ด", "Ireland": "๐Ÿ‡ฎ๐Ÿ‡ช", "Armenia": "๐Ÿ‡ฆ๐Ÿ‡ฒ", "Azerbaijan": "๐Ÿ‡ฆ๐Ÿ‡ฟ", "Luxembourg": "๐Ÿ‡ฑ๐Ÿ‡บ", "Jamaica": "๐Ÿ‡ฏ๐Ÿ‡ฒ", "Thailand": "๐Ÿ‡น๐Ÿ‡ญ", "Canada": "๐Ÿ‡จ๐Ÿ‡ฆ", "Latin America": "๐ŸŒŽ", "International": "๐ŸŒ", "Caribbean": "๐Ÿ๏ธ", "Africa": "๐ŸŒ" } def ensure_directories(): """Creates the output folder structure if it doesn't exist.""" os.makedirs(os.path.join(OUTPUT_DIR, "stable"), exist_ok=True) os.makedirs(os.path.join(OUTPUT_DIR, "aria+"), exist_ok=True) def apply_flag(name): """Appends a flag emoji to the state name if it's missing.""" clean_name = name.strip() # Check if the name already ends with an emoji/flag or if it's in our map for country, flag in FLAG_MAP.items(): if country.lower() == clean_name.lower(): if flag not in clean_name: return f"{clean_name} {flag}" break return clean_name def parse_md_files(): ensure_directories() mega_aria = ["#EXTM3U\n"] mega_aria_plus = ["#EXTM3U\n"] # Dictionaries to temporarily group entries by state name for global alphabetical sorting mega_stable_dict = {} mega_unstable_dict = {} md_files = glob.glob(f"{INPUT_DIR}/*.md") if not md_files: print(f"Warning: No .md files found in '{INPUT_DIR}'.") return for file_path in md_files: raw_filename = os.path.basename(file_path).replace(".md", "") # Fallback state name in case the file has no heading state_name = raw_filename.replace("_", " ").title() state_stable = [] state_unstable = [] with open(file_path, "r", encoding="utf-8") as f: lines = f.readlines() for line in lines: line = line.strip() # Extract custom title from markdown header if line.startswith("#") and not line.startswith("#EXT") and not "Kanรกly:" in line: clean_title = re.sub(r'^#+\s*', '', line).strip() if clean_title: state_name = clean_title # Apply flag to the state name state_name = apply_flag(state_name) if line.startswith("|") and not line.startswith("| # |") and not line.startswith("|:-"): parts = [p.strip() for p in line.split("|")] if len(parts) >= 7: channel_name = parts[2].strip() link_raw = parts[3] link_match = re.search(r'\((.*?)\)', link_raw) link = link_match.group(1) if link_match else "" logo_raw = parts[4] logo_match = re.search(r'src="(.*?)"', logo_raw) logo = logo_match.group(1) if logo_match else "" # Fix for the   issue - completely strip it out epg_id = parts[5].replace(" ", "").strip() stream_type = parts[6].lower().strip() if not link: continue # Generate the M3U line with the properly formatted group-title (with flag) m3u_entry = f'#EXTINF:-1 tvg-id="{epg_id}" tvg-logo="{logo}" group-title="{state_name}" tvg-status="{stream_type}",{channel_name}\n{link}\n' if stream_type == "stable": state_stable.append(m3u_entry) state_unstable.append(m3u_entry) elif stream_type == "unstable": state_unstable.append(m3u_entry) elif stream_type == "not-working": state_unstable.append(m3u_entry) else: state_unstable.append(m3u_entry) # 1. Stable state playlist (saved in root /stable/) if state_stable: state_stable_file = os.path.join(OUTPUT_DIR, "stable", f"{raw_filename}.m3u") with open(state_stable_file, "w", encoding="utf-8") as out: out.write("#EXTM3U\n" + "".join(state_stable)) # Store entries in the dictionary under the specific group/state name if state_name not in mega_stable_dict: mega_stable_dict[state_name] = [] mega_stable_dict[state_name].extend(state_stable) # 2. Extended state playlist (saved in root /aria+/) if state_unstable: state_unstable_file = os.path.join(OUTPUT_DIR, "aria+", f"{raw_filename}.m3u") with open(state_unstable_file, "w", encoding="utf-8") as out: out.write("#EXTM3U\n" + "".join(state_unstable)) # Store entries in the dictionary under the specific group/state name if state_name not in mega_unstable_dict: mega_unstable_dict[state_name] = [] mega_unstable_dict[state_name].extend(state_unstable) print(f"Processed state: {state_name} (Stable: {len(state_stable)}, Total: {len(state_unstable)})") # Compile the final lists alphabetically sorted by state_name (which includes the flag) for sorted_state in sorted(mega_stable_dict.keys()): mega_aria.extend(mega_stable_dict[sorted_state]) for sorted_state in sorted(mega_unstable_dict.keys()): mega_aria_plus.extend(mega_unstable_dict[sorted_state]) # Save mega playlists directly to root with open(os.path.join(OUTPUT_DIR, "aria.m3u"), "w", encoding="utf-8") as out: out.writelines(mega_aria) with open(os.path.join(OUTPUT_DIR, "aria+.m3u"), "w", encoding="utf-8") as out: out.writelines(mega_aria_plus) print("\n--- DONE ---") print("Playlists generated in root directory (strictly sorted alphabetically by group-title).") if __name__ == "__main__": parse_md_files()