From 506e30af2480128fdadce41ff95d4110bdb81132 Mon Sep 17 00:00:00 2001 From: abc1763613206 Date: Thu, 13 Feb 2025 18:11:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=8EM3U8=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=B9=8B=E9=97=B4=E7=9A=84=E8=BD=AC=E6=8D=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convertor.py | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 convertor.py diff --git a/convertor.py b/convertor.py new file mode 100644 index 0000000..f9de09a --- /dev/null +++ b/convertor.py @@ -0,0 +1,138 @@ +import csv +import os + +def csv_to_m3u8(input_csv, output_m3u8, encoding='utf-8'): + """ + Convert CSV file to M3U8 format + + Args: + input_csv (str): Path to input CSV file + output_m3u8 (str): Path to output M3U8 file + """ + try: + with open(input_csv, 'r', encoding=encoding) as csv_file, \ + open(output_m3u8, 'w', encoding='utf-8') as m3u8_file: + + # Write M3U8 header + m3u8_file.write('#EXTM3U\n') + + # Read CSV file + csv_reader = csv.DictReader(csv_file) + + # Convert each row to M3U8 format + for row in csv_reader: + channel = row['Channel'] + group = row['Group'] + source = row['Source'] + link = row['Link'] + + # Write channel information + m3u8_file.write(f'#EXTINF:-1 group-title="{group}" tvg-name="{channel}" tvg-logo="" tv-source="{source}",{channel}\n') + m3u8_file.write(f'{link}\n') + + return True + + except Exception as e: + print(f"Error converting CSV to M3U8: {str(e)}") + return False + +def m3u8_to_csv(input_m3u8, output_csv, encoding='utf-8'): + """ + Convert M3U8 file to CSV format + + Args: + input_m3u8 (str): Path to input M3U8 file + output_csv (str): Path to output CSV file + """ + try: + with open(input_m3u8, 'r', encoding=encoding) as m3u8_file, \ + open(output_csv, 'w', encoding=encoding, newline='') as csv_file: + + writer = csv.DictWriter(csv_file, fieldnames=['Channel', 'Group', 'Source', 'Link']) + writer.writeheader() + + lines = m3u8_file.readlines() + i = 0 + while i < len(lines): + line = lines[i].strip() + if line.startswith('#EXTINF:'): + info = line.split('"') + group = info[1] if len(info) > 1 else '' + channel = info[3] if len(info) > 3 else '' + source = info[7] if len(info) > 7 else '' + name = info[-1].split(',')[-1] + + if i + 1 < len(lines): + link = lines[i + 1].strip() + writer.writerow({ + 'Channel': name, + 'Group': group, + 'Source': source, + 'Link': link + }) + i += 1 + + return True + + except Exception as e: + print(f"Error converting M3U8 to CSV: {str(e)}") + return False + +def tvlist_to_m3u8(input_file, output_file, encoding='utf-8'): + """ + Convert TVList file to M3U8 format + + Args: + input_file (str): Path to input TVList file + output_file (str): Path to output M3U8 file + """ + try: + with open(input_file, 'r', encoding=encoding) as tvlist_file, \ + open(output_file, 'w', encoding='utf-8') as m3u8_file: + + # Write M3U8 header + m3u8_file.write('#EXTM3U\n') + + for line in tvlist_file: + parts = line.strip().split(',') + Channel = parts[0] + Link = parts[1] + m3u8_file.write(f'#EXTINF:-1 tvg-name="{Channel}",{Channel}\n') + m3u8_file.write(f'{Link}\n') + return True + except Exception as e: + print(f"Error converting TVList to M3U8: {str(e)}") + return False + +def tvgroup_to_m3u8(output_file): + MAPPING = { + "cctv": "央视频道", + "weishi": "卫视频道", + "difang": "地方频道", + "special": "特色频道" + } + DIR = os.path.join(os.path.dirname(__file__), 'groups') + try: + with open(output_file, 'w', encoding='utf-8') as m3u8_file: + m3u8_file.write('#EXTM3U\n') + for group in MAPPING: + file = os.path.join(DIR, f"{group}.txt") + if os.path.exists(file): + with open(file, 'r', encoding='gb18030') as f: + for line in f: + parts = line.strip().split(',') + Channel = parts[0] + Link = parts[1] + m3u8_file.write(f'#EXTINF:-1 group-title="{MAPPING[group]}" tvg-name="{Channel}",{Channel}\n') + m3u8_file.write(f'{Link}\n') + return True + except Exception as e: + print(f"Error converting TVGroup to M3U8: {str(e)}") + return False + +if __name__ == "__main__": + input_file = "data.csv" + output_file = "playlist.m3u8" + + # csv_to_m3u8(input_file, output_file, encoding='gb18030') + tvgroup_to_m3u8(output_file) \ No newline at end of file