Update iptv_udp.py
This commit is contained in:
+23
-4
@@ -311,17 +311,36 @@ keywords = ['CCTV', '剧场', '影院', '电影'] # 需要提取的关键字列
|
|||||||
pattern = '|'.join(keywords) # 创建正则表达式模式,匹配任意一个关键字
|
pattern = '|'.join(keywords) # 创建正则表达式模式,匹配任意一个关键字
|
||||||
#pattern = r"^(.*?),(?!#genre#)(.*?)$" #以分类直接复制
|
#pattern = r"^(.*?),(?!#genre#)(.*?)$" #以分类直接复制
|
||||||
with open('playlist/IPTV_UDP.txt', 'r', encoding='utf-8') as file, open('c0.txt', 'w', encoding='utf-8') as c0: #定义临时文件名
|
with open('playlist/IPTV_UDP.txt', 'r', encoding='utf-8') as file, open('c0.txt', 'w', encoding='utf-8') as c0: #定义临时文件名
|
||||||
c0.write('\组播央视,#genre#\n') #写入临时文件名$GD
|
c0.write('组播央视,#genre#\n') #写入临时文件名$GD
|
||||||
for line in file:
|
for line in file:
|
||||||
if re.search(pattern, line): # 如果行中有任意关键字
|
if re.search(pattern, line): # 如果行中有任意关键字
|
||||||
c0.write(line) # 将该行写入输出文件 #定义临时文件
|
c0.write(line) # 将该行写入输出文件 #定义临时文件
|
||||||
|
|
||||||
|
import re
|
||||||
|
def find_number_before_comma(line):
|
||||||
|
# 从行的开始到逗号前的部分,找到第一个数字
|
||||||
|
before_comma = line.split(',')[0] if ',' in line else line
|
||||||
|
numbers = re.findall(r'\d+', before_comma)
|
||||||
|
return int(numbers[0]) if numbers else None
|
||||||
|
def custom_sort_key(line):
|
||||||
|
# 返回一个元组,元组的第一个元素用于数字排序,第二个元素用于字典排序
|
||||||
|
return (find_number_before_comma(line), line)
|
||||||
|
with open('c0.txt', 'r', encoding='utf-8') as file:
|
||||||
|
lines = file.readlines()
|
||||||
|
sorted_lines = sorted(lines, key=custom_sort_key
|
||||||
|
# 写入新文件
|
||||||
|
with open('c00.txt', 'w', encoding='utf-8') as file:
|
||||||
|
for line in sorted_lines:
|
||||||
|
file.write(line)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#从整理好的文本中按类别进行特定关键词提取#
|
#从整理好的文本中按类别进行特定关键词提取#
|
||||||
keywords = ['爱动漫', '爱怀旧', '爱经典', '爱科幻', '爱幼教', '爱青春', '爱院线', '爱悬疑'] # 需要提取的关键字列表
|
keywords = ['爱动漫', '爱怀旧', '爱经典', '爱科幻', '爱幼教', '爱青春', '爱院线', '爱悬疑'] # 需要提取的关键字列表
|
||||||
pattern = '|'.join(keywords) # 创建正则表达式模式,匹配任意一个关键字
|
pattern = '|'.join(keywords) # 创建正则表达式模式,匹配任意一个关键字
|
||||||
#pattern = r"^(.*?),(?!#genre#)(.*?)$" #以分类直接复制
|
#pattern = r"^(.*?),(?!#genre#)(.*?)$" #以分类直接复制
|
||||||
with open('playlist/IPTV_UDP.txt', 'r', encoding='utf-8') as file, open('c1.txt', 'w', encoding='utf-8') as c1: #定义临时文件名
|
with open('playlist/IPTV_UDP.txt', 'r', encoding='utf-8') as file, open('c1.txt', 'w', encoding='utf-8') as c1: #定义临时文件名
|
||||||
c1.write('\组播央视,#genre#\n') #写入临时文件名$GD
|
c1.write('组播央视,#genre#\n') #写入临时文件名$GD
|
||||||
for line in file:
|
for line in file:
|
||||||
if '$GD' not in line and '4K' not in line:
|
if '$GD' not in line and '4K' not in line:
|
||||||
if re.search(pattern, line): # 如果行中有任意关键字
|
if re.search(pattern, line): # 如果行中有任意关键字
|
||||||
@@ -446,7 +465,7 @@ print('删除的行已保存到:', deleted_lines_file_path)
|
|||||||
#合并所有频道文件#
|
#合并所有频道文件#
|
||||||
# 读取要合并的频道文件,并生成临时文件#合并所有频道文件#
|
# 读取要合并的频道文件,并生成临时文件#合并所有频道文件#
|
||||||
file_contents = []
|
file_contents = []
|
||||||
file_paths = ["a0.txt", "港澳.txt", "df0.txt", "c0.txt", "c1.txt", "f.txt", "f1.txt"] # 替换为实际的文件路径列表#
|
file_paths = ["a0.txt", "港澳.txt", "df0.txt", "c00.txt", "c1.txt", "f.txt", "f1.txt"] # 替换为实际的文件路径列表#
|
||||||
for file_path in file_paths: #
|
for file_path in file_paths: #
|
||||||
with open(file_path, 'r', encoding="utf-8") as file: #
|
with open(file_path, 'r', encoding="utf-8") as file: #
|
||||||
content = file.read()
|
content = file.read()
|
||||||
@@ -558,7 +577,7 @@ def txt_to_m3u(input_file, output_file):
|
|||||||
# 将txt文件转换为m3u文件
|
# 将txt文件转换为m3u文件
|
||||||
txt_to_m3u('综合源.txt', '综合源.m3u')
|
txt_to_m3u('综合源.txt', '综合源.m3u')
|
||||||
#任务结束,删除不必要的过程文件#
|
#任务结束,删除不必要的过程文件#
|
||||||
files_to_remove = ["TW.txt", "a.txt", "a0.txt", "港澳.txt", "playlist/IPTV_UDP.txt", "df0.txt", "sr1.txt", "c0.txt", "c1.txt", \
|
files_to_remove = ["TW.txt", "a.txt", "a0.txt", "港澳.txt", "playlist/IPTV_UDP.txt", "df0.txt", "sr1.txt", "c0.txt", "c00.txt", "c1.txt", \
|
||||||
"f.txt", "f1.txt", "playlist/酒店源#.txt"]
|
"f.txt", "f1.txt", "playlist/酒店源#.txt"]
|
||||||
for file in files_to_remove:
|
for file in files_to_remove:
|
||||||
if os.path.exists(file):
|
if os.path.exists(file):
|
||||||
|
|||||||
Reference in New Issue
Block a user