Update 网络收集.py

This commit is contained in:
frxz751113
2024-09-19 17:27:06 +08:00
committed by GitHub
parent 2bcbd58f00
commit 8b1ece7401
+42 -51
View File
@@ -379,6 +379,7 @@ with open('2.txt', 'r', encoding='utf-8') as file, open('2.txt', 'w', encoding='
############################################################### ###############################################################
import re import re
def parse_file(input_file_path, output_file_name): def parse_file(input_file_path, output_file_name):
# 正则表达式匹配从'//'开始到第一个'/'或第一个'::'结束的部分 # 正则表达式匹配从'//'开始到第一个'/'或第一个'::'结束的部分
ip_or_domain_pattern = re.compile(r'//([^/:]*:[^/:]*::[^/:]*|[^/]*)') ip_or_domain_pattern = re.compile(r'//([^/:]*:[^/:]*::[^/:]*|[^/]*)')
@@ -425,6 +426,7 @@ parse_file('2.txt', '2.txt')
import cv2 import cv2
import time import time
from tqdm import tqdm from tqdm import tqdm
@@ -446,56 +448,47 @@ def merge_and_filter():
total_lines = len(lines) total_lines = len(lines)
import time # 处理输入文件中的数据并进行检测
import cv2 with open(output_file_path, 'a', encoding='utf-8') as output_file:
from tqdm import tqdm for i, line in tqdm(enumerate(lines), total=total_lines, desc="Processing", unit='line'):
if 'genre' in line:
# 处理输入文件中的数据并进行检测 output_file.write(line)
with open(output_file_path, 'a', encoding='utf-8') as output_file: continue
valid_line_count = 0 parts = line.split(',', 1)
for i, line in tqdm(enumerate(lines), total=total_lines, desc="Processing", unit='line'): if len(parts) == 2:
if 'genre' in line: channel_name, url = parts
output_file.write(line) channel_name = channel_name.strip()
continue url = url.strip()
parts = line.split(',', 1) ip_key = get_ip_key(url)
if len(parts) == 2: if ip_key and ip_key in detected_ips:
channel_name, url = parts if detected_ips[ip_key]['status'] == 'ok':
channel_name = channel_name.strip() output_file.write(line)
url = url.strip() elif ip_key:
ip_key = get_ip_key(url) cap = cv2.VideoCapture(url)
if ip_key and ip_key in detected_ips: start_time = time.time()
if detected_ips[ip_key]['status'] == 'ok': frame_count = 0
output_file.write(line) while frame_count < 50 and (time.time() - start_time) < 3:
valid_line_count += 1 ret, frame = cap.read()
elif ip_key: if not ret:
cap = cv2.VideoCapture(url) break
start_time = time.time() frame_count += 1
frame_count = 0 cap.release()
while frame_count < 50 and (time.time() - start_time) < 3: if frame_count >= 50:
ret, frame = cap.read() detected_ips[ip_key] = {'status': 'ok'}
if not ret: output_file.write(line)
break else:
frame_count += 1 detected_ips[ip_key] = {'status': 'fail'}
cap.release() print(f"有效的总行数为:{valid_line_count}")
if frame_count >= 50:
detected_ips[ip_key] = {'status': 'ok'}
output_file.write(line)
valid_line_count += 1
else:
detected_ips[ip_key] = {'status': 'fail'}
print(f"有效的总行数为:{valid_line_count}")
# 合并任意字符加上网络收集.txt 的文件 # 合并任意字符加上网络收集.txt 的文件
all_files = [f for f in os.listdir(os.getcwd()) if f.endswith('网络收集.txt')] all_files = [f for f in os.listdir(os.getcwd()) if f.endswith('网络收集.txt')]
with open(output_file_path, 'a', encoding='utf-8') as main_output: with open(output_file_path, 'a', encoding='utf-8') as main_output:
for file_name in all_files: for file_name in all_files:
if file_name!= output_file_path: if file_name!= output_file_path:
with open(file_name, 'r', encoding='utf-8') as other_file: with open(file_name, 'r', encoding='utf-8') as other_file:
content = other_file.read() content = other_file.read()
if content: if content:
main_output.write('\n') main_output.write('\n')
main_output.write(content) main_output.write(content)
detected_ips = {} detected_ips = {}
merge_and_filter() merge_and_filter()
@@ -700,5 +693,3 @@ for file in files_to_remove:
else: # 如果文件不存在,则提示异常并打印提示信息 else: # 如果文件不存在,则提示异常并打印提示信息
print(f"文件 {file} 不存在,跳过删除。") print(f"文件 {file} 不存在,跳过删除。")
print("任务运行完毕,频道列表可查看文件夹内源.txt文件!") print("任务运行完毕,频道列表可查看文件夹内源.txt文件!")