47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
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
|
|
|
|
|
|
def download_m3u8(download_path='./mp4/'):
|
|
try:
|
|
log = Log().getlog()
|
|
# 初始化数据库连接
|
|
movie_config = MySqlUtil("movie")
|
|
|
|
# 获取未处理的电影记录
|
|
movie_message = MySqlUtil.get_one(movie_config, 'SELECT * FROM `movie` WHERE is_ok=0 LIMIT 1')
|
|
if not movie_message or len(movie_message) < 3: # 校验结果是否有效
|
|
log.info("没有找到电影记录或无效数据。")
|
|
return
|
|
|
|
id, name, url = movie_message[0], movie_message[1], movie_message[2]
|
|
|
|
# 构造目标文件路径
|
|
file_path = Path(download_path).joinpath(f"{name}.mp4")
|
|
|
|
# 更新数据库状态,使用参数化查询防止 SQL 注入
|
|
sql = f'UPDATE `movie`.`movie` SET `is_ok` = 1 WHERE `id` = {id}'
|
|
MySqlUtil.update(movie_config, sql=sql)
|
|
|
|
log.info(f"任务下载中,正在下载 {name}...")
|
|
|
|
# 下载 m3u8 文件并转换为 MP4
|
|
m3u8_To_MP4.multithread_download(url, file_path=str(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()
|