import datetime
import time
from DrissionPage import ChromiumPage
watching_type = input("选择播放视频类型(1.全部视频 2.必看视频 默认为全部视频):")
if watching_type is None or watching_type=="":
watching_type = 1
while True:
try:
watching_type = int(watching_type)
except:
print("选择错误,请重新选择(只需要输入编号即可)")
watching_type = int(input("选择播放视频类型(1.全部视频 2.必看视频 默认为全部视频):"))
else:
if watching_type != 1 and watching_type != 2:
print("选择错误,请重新选择(只需要输入编号即可)")
watching_type = int(input("选择播放视频类型(1.全部视频 2.必看视频 默认为全部视频):"))
else:
break
if watching_type == 1:
print(f"[{datetime.datetime.now()}] 当前选择:{watching_type}.全部视频")
else:
print(f"[{datetime.datetime.now()}] 当前选择:{watching_type}.必看视频")
print(f"[{datetime.datetime.now()}] 启动Chrome浏览器.")
# 创建页面对象,并启动或接管浏览器
page = ChromiumPage()
print(f"[{datetime.datetime.now()}] 访问学习中心,检测是否已经登录.")
page.get("https://learning.shou.org.cn/scenter")
try:
page.ele('#username',timeout=3)
print(f"[{datetime.datetime.now()}] 检测到用户未登录,请手动登录账号。")
input("登录成功后按回车继续:")
except:
print(f"[{datetime.datetime.now()}] 检测用户已登录")
courses = {}
print(f"[{datetime.datetime.now()}] 收集课程列表信息")
course_list = page.eles('.course-title')
for course in course_list:
course_link = course.ele('tag:a').link
course_name = course.ele('tag:a').text
course_name = course_name.replace(" ","")
courses[course_name] = course_link
print(f"[{datetime.datetime.now()}] 课程名称:{course_name} 课程地址:{course_link}")
course_index = 0
for course in courses:
course_index += 1
print(f"[{datetime.datetime.now()}] 收集课程({course_index}/{len(courses)}):{course} 的视频信息,请耐心等待")
watching_video_list = {}
page.get(courses[course])
LearningCatalog = page.ele('.process_container')
course_dirs = page.eles('.dynamic_topic')
for dir in course_dirs:
try:
dir.ele('.am-icon am-icon-caret-right sh-toc-expand topic_condensation').click()
course_task_list = dir.ele('.sh-toc-list', timeout=1).eles('tag:a')
for course_task in course_task_list:
course_task_txt = course_task.text.replace(" ", "")
course_task_link = course_task.link
course_type = course_task.next('tag:span', 1, timeout=1).text
if "视频" in course_type:
if watching_type == 2:
course_must_txt = course_task.next('text=(必看)', 1, timeout=1).text
course_status = course_task.next('tag:img', 1, timeout=1).link
if "yellow" in course_status:
status = "未完成"
elif "white" in course_status:
status = "未看"
else:
status = "已完成"
print(
f"[{datetime.datetime.now()}] 课程({course_index}/{len(courses)}):{course} {status}视频:{course_task_txt}")
if status == "已完成":
continue
watching_video_list[course_task_txt] = course_task_link
except:
continue
if len(watching_video_list) == 0:
print(f"[{datetime.datetime.now()}] 课程({course_index}/{len(courses)}):{course} 所有必看视频已全部播放完毕。")
else:
print(f"[{datetime.datetime.now()}] 开始观看课程({course_index}/{len(courses)}):{course} 视频,共{len(watching_video_list)}个视频。")
index = 1
for watching_video in watching_video_list:
page.get(watching_video_list[watching_video])
play_status = ""
try_num = 1
while True:
if try_num >= 5:
print(f"[{datetime.datetime.now()}] 课程({course_index}/{len(courses)}):{course} 视频({index}/{len(watching_video_list)}):{watching_video} 达到最大重试次数,跳过此视频,请后续手动观看.")
play_status = False
break
try:
buttons = page.ele('.dplayer-icon dplayer-play-icon').click()
play_status = True
break
except BaseException as e:
print(f"[{datetime.datetime.now()}] 课程({course_index}/{len(courses)}):{course} 视频({index}/{len(watching_video_list)}):{watching_video} 未找到播放按钮,1秒后重试.error:{e}")
play_status = False
try_num += 1
time.sleep(1)
if play_status:
while True:
ptime = page.ele('.dplayer-ptime', timeout=3).text
dtime = page.ele('.dplayer-dtime', timeout=3).text
if ptime == dtime and ptime != "0:00":
print("\r", end="")
print(f"[{datetime.datetime.now()}] 课程({course_index}/{len(courses)}):{course} 视频({index}/{len(watching_video_list)}):{watching_video} 当前播放进度:{ptime}/{dtime} 已播放完毕.",flush=True, end="")
print("")
index += 1
break
else:
print("\r", end="")
print(f"[{datetime.datetime.now()}] 课程({course_index}/{len(courses)}):{course} 视频({index}/{len(watching_video_list)}):{watching_video} 当前播放进度:{ptime}/{dtime}",flush=True,end="")
time.sleep(3)
DrissionPage 自动播放
最新推荐文章于 2025-01-31 00:37:39 发布