-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcn2en.py
58 lines (48 loc) · 1.57 KB
/
cn2en.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import re
import termcolor
import argparse
from googletrans import Translator
translator = Translator()
# import requests
# import re
# from concurrent.futures import ThreadPoolExecutor, as_completed
# from tqdm import tqdm
# import subprocess
def parse_playlist(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
entries = []
entry = []
for line in lines:
if line.startswith('#EXTINF'):
channel_name = line.split(',')[1]
translated_name = translator.translate(channel_name, src='zh-CN', dest='en').text
translated_line = f"{line.split(',')[0]}, {translated_name}"
if entry:
entries.append(entry)
entry = [translated_line]
elif line.strip():
entry.append(line)
if entry:
entries.append(entry)
return entries
def write_playlist(file_path, entries):
with open(file_path, 'w', encoding='utf-8') as file:
file.write('#EXTM3U\n')
for entry in entries:
for line in entry:
file.write(line)
file.write('\n') # Ensure there is a single newline after each entry
def main():
input_path = "IPTV.m3u"
output_path = "enIPTV.m3u"
print("Parsing playlist...")
entries = parse_playlist(input_path)
# print("Translate cn 2 en ...")
# valid_entries = translate_channel_names(input_path)
print("Writing sorted playlist...")
write_playlist(output_path, entries)
print("Process completed.")
if __name__ == '__main__':
main()