import m3u8_to_mp4 from utils.MySqlUtil import MySqlUtil from apscheduler.schedulers.blocking import BlockingScheduler import time from utils.Log import Log from pathlib import Path from utils.Db_Execute import get_movie_list, movie_options, movie_add import os log = Log() def download_m3u8(): try: current_directory = os.path.dirname(os.path.abspath(__file__)) root_path = os.path.abspath(os.path.dirname(current_directory) + os.path.sep + ".") project_name = root_path.split(os.path.sep)[-1] project_root_path = os.path.abspath(os.path.dirname(__file__)).split(project_name)[0] + project_name + '/mp4/' # 调用 get_movie_list() 获取电影列表 movie_list = get_movie_list() for movie in movie_list: if not movie or len(movie) < 3: # 校验结果是否有效 log.info("没有找到电影记录或无效数据。") return id, name, url = movie[0], movie[1], movie[2] # 构造目标文件路径 file_path = Path(project_root_path).joinpath(f"{name}.mp4") # 更新数据库状态,使用参数化查询防止 SQL 注入 movie_options(id) log.info(f"任务下载中,正在下载 {name}...") log.info(file_path) # 下载 m3u8 文件并转换为 MP4 m3u8_to_mp4.multithread_download(url, file_path=file_path) log.info(f"成功下载并转换 {name} to {file_path}.") except Exception as e: log.error(f"下载过程中出现错误: {e}") if __name__ == '__main__': download_m3u8() # str_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # sch = BlockingScheduler(timezone='Asia/Shanghai') # sch.add_job(download_m3u8, 'cron', minute='*/2') # sch.start()