优化日志输出?
This commit is contained in:
49
main.py
49
main.py
@@ -23,7 +23,7 @@ progress_cache_file = "progress.json"
|
||||
|
||||
# [配置] 心跳间隔(秒)
|
||||
# 警告:建议设置在 60 秒以上,过快的心跳可能面临风险!
|
||||
HEARTBEAT_INTERVAL = 2
|
||||
HEARTBEAT_INTERVAL = 0
|
||||
|
||||
# [配置] 每次心跳增加的进度时间(秒)
|
||||
ADD_TIME = 120
|
||||
@@ -105,9 +105,12 @@ def process_video(course_id, item_id, current_index=0, total_items=0):
|
||||
2. 请求 config XML,获取 userId, updStatusUrl, totalTime, historyId 等
|
||||
3. 循环发送心跳包,直到视频看完
|
||||
"""
|
||||
print(f"\n>>> 开始处理视频: CourseId={course_id}, ItemId={item_id}")
|
||||
# 优化日志头部
|
||||
separator = "-" * 60
|
||||
print(f"\n{separator}")
|
||||
print(f"[+] 正在处理视频 | CourseID: {course_id} | ItemID: {item_id}")
|
||||
if total_items > 0:
|
||||
print(f" 当前进度: 第 {current_index} 集 / 共 {total_items} 集")
|
||||
print(f"[*] 总体进度: 第 {current_index}/{total_items} 集")
|
||||
|
||||
# 1. 请求视频播放页
|
||||
video_page_url = f"https://zjbc.cjnep.net/lms/web/course/view?id={course_id}&itemid={item_id}"
|
||||
@@ -115,26 +118,26 @@ def process_video(course_id, item_id, current_index=0, total_items=0):
|
||||
resp = requests.get(video_page_url, headers=headers)
|
||||
resp.raise_for_status()
|
||||
except Exception as e:
|
||||
print(f" [Error] 无法访问视频页: {e}")
|
||||
print(f"[!] 无法访问视频页: {e}")
|
||||
return
|
||||
|
||||
# 2. 提取 config URL
|
||||
# 示例: config: '/lms/web/course/startupxml?courseid=677&itemid=142573&historyid=5740083&is_subcourse=0'
|
||||
config_match = re.search(r"config:\s*'([^']+)'", resp.text)
|
||||
if not config_match:
|
||||
print(" [Error] 未找到 config URL,跳过此视频。")
|
||||
print("[!] 未找到 config URL,跳过此视频。")
|
||||
return
|
||||
|
||||
config_path = config_match.group(1)
|
||||
config_url = "https://zjbc.cjnep.net" + config_path
|
||||
print(f" Config URL: {config_url}")
|
||||
# print(f" Config URL: {config_url}") # 减少冗余日志
|
||||
|
||||
# 3. 请求 XML 配置
|
||||
try:
|
||||
xml_resp = requests.get(config_url, headers=headers)
|
||||
xml_resp.raise_for_status()
|
||||
except Exception as e:
|
||||
print(f" [Error] 无法获取 XML 配置: {e}")
|
||||
print(f"[!] 无法获取 XML 配置: {e}")
|
||||
return
|
||||
|
||||
# 4. 解析 XML
|
||||
@@ -150,7 +153,7 @@ def process_video(course_id, item_id, current_index=0, total_items=0):
|
||||
sco_title = root.findtext("scoTitle")
|
||||
|
||||
if sco_title:
|
||||
print(f" [课程名称] {sco_title}")
|
||||
print(f"[*] 课程名称: {sco_title}")
|
||||
|
||||
# 尝试获取视频 URL
|
||||
video_url = None
|
||||
@@ -164,7 +167,7 @@ def process_video(course_id, item_id, current_index=0, total_items=0):
|
||||
|
||||
# 检查必要字段 (totalTime 除外)
|
||||
if not (user_id and upd_status_url and history_id):
|
||||
print(" [Error] XML 解析缺少关键字段 (userId/updStatusUrl/historyId)")
|
||||
print("[!] XML 解析缺少关键字段 (userId/updStatusUrl/historyId)")
|
||||
return
|
||||
|
||||
# 读取本地缓存
|
||||
@@ -174,7 +177,7 @@ def process_video(course_id, item_id, current_index=0, total_items=0):
|
||||
# 优先检查是否已完成,如果已完成则跳过获取时长(避免 ffprobe)
|
||||
if local_finished or finish_status == "true":
|
||||
print(
|
||||
f" [Skip] 视频已完成 (XML Finish: {finish_status}, Local Finish: {local_finished})")
|
||||
f"[-] 视频已完成,跳过 (XML: {finish_status}, Local: {local_finished})")
|
||||
return
|
||||
|
||||
# 获取时长
|
||||
@@ -182,23 +185,23 @@ def process_video(course_id, item_id, current_index=0, total_items=0):
|
||||
if total_time_str:
|
||||
total_time = float(total_time_str)
|
||||
elif video_url:
|
||||
print(" [Info] XML 中未找到 totalTime,尝试通过 ffprobe 获取视频时长...")
|
||||
print("[*] XML 中未找到 totalTime,尝试通过 ffprobe 获取视频时长...")
|
||||
duration = get_video_duration(video_url)
|
||||
if duration:
|
||||
total_time = duration
|
||||
print(f" [Info] 获取到视频时长: {total_time}s")
|
||||
print(f"[*] 获取到视频时长: {total_time}s")
|
||||
else:
|
||||
print(" [Error] 无法获取视频时长,跳过此视频")
|
||||
print("[!] 无法获取视频时长,跳过此视频")
|
||||
return
|
||||
else:
|
||||
print(" [Error] XML 中既无 totalTime 也无 videoUrl,无法继续")
|
||||
print("[!] XML 中既无 totalTime 也无 videoUrl,无法继续")
|
||||
return
|
||||
|
||||
print(
|
||||
f" [Info] UserId={user_id}, HistoryId={history_id}, TotalTime={total_time}")
|
||||
f"[*] 视频信息: User={user_id} | History={history_id} | Duration={total_time}s")
|
||||
|
||||
except ET.ParseError:
|
||||
print(" [Error] XML 解析失败")
|
||||
print("[!] XML 解析失败")
|
||||
return
|
||||
|
||||
# 5. 循环发送心跳
|
||||
@@ -215,18 +218,17 @@ def process_video(course_id, item_id, current_index=0, total_items=0):
|
||||
|
||||
# 如果进度已达标,则跳过
|
||||
if current_time >= total_time:
|
||||
print(
|
||||
f" [Skip] 视频已完成 (Progress: {current_time}/{total_time})")
|
||||
print(f"[-] 视频已完成 (Progress: {current_time}/{total_time})")
|
||||
return
|
||||
|
||||
print(f" [Start] 当前进度: {current_time}s / {total_time}s")
|
||||
print(f"[>] 初始进度: {current_time}s / {total_time}s")
|
||||
|
||||
is_first = "true"
|
||||
|
||||
while True:
|
||||
# 如果已经看完
|
||||
if current_time >= total_time:
|
||||
print(" [Done] 视频已看完。")
|
||||
print("[+] 视频已看完,发送完成包...")
|
||||
# 发送最后一次 finished=1 的包
|
||||
send_heartbeat(upd_status_url, user_id, course_id, item_id, history_id,
|
||||
add_time=0, current_time=0, total_time=total_time,
|
||||
@@ -253,7 +255,7 @@ def process_video(course_id, item_id, current_index=0, total_items=0):
|
||||
finished=finished_flag, first_update=is_first)
|
||||
|
||||
if not success:
|
||||
print(" [Error] 心跳发送失败,停止当前视频。")
|
||||
print("[!] 心跳发送失败,停止当前视频。")
|
||||
break
|
||||
|
||||
# 保存进度
|
||||
@@ -261,14 +263,15 @@ def process_video(course_id, item_id, current_index=0, total_items=0):
|
||||
total_time, finished_flag == 1)
|
||||
|
||||
if finished_flag == 1:
|
||||
print(" [Done] 视频播放结束。")
|
||||
print("[+] 视频播放结束。")
|
||||
break
|
||||
|
||||
is_first = "false"
|
||||
|
||||
# 模拟等待
|
||||
percent = min(100, int((current_time / total_time) * 100))
|
||||
print(
|
||||
f" [Progress] 进度更新: {current_time:.1f}/{total_time} (add {step}s)...")
|
||||
f" [Running] 进度: {current_time:.1f}/{total_time}s (+{step}s) | {percent}%")
|
||||
time.sleep(HEARTBEAT_INTERVAL)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user