Skip to content

Commit

Permalink
feat:重构运行逻辑,使用APScheduler定时运行
Browse files Browse the repository at this point in the history
  • Loading branch information
Akimio521 committed Jul 8, 2024
1 parent cdf00e1 commit 3662594
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 155 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
FROM python:3.12.4-alpine

ENV TZ=Asia/Shanghai \
CONFIG_PATH=/config/config.yaml \
LOG_LEVEL=INFO\
INTERVAL=3600
CONFIG_PATH=/config/config.yaml

COPY entrypoint.sh entrypoint.sh

Expand Down
88 changes: 0 additions & 88 deletions app/autofilm.py

This file was deleted.

1 change: 1 addition & 0 deletions app/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .config import settings
68 changes: 68 additions & 0 deletions app/core/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
# encoding: utf-8


import logging
from pathlib import Path
from yaml import safe_load

from version import APP_VERSION


class SettingManager:
"""
系统配置
"""

# APP 名称
APP_NAME: str = "Autofilm"
# APP 版本
APP_VERSION: int = APP_VERSION
# 时区
TZ: str = "Asia/Shanghai"

def __init__(self) -> None:
"""
初始化 SettingManager 对象
"""
self.__mkdir()
self.__init_logging()

def __mkdir(self) -> None:
"""
创建目录
"""
with self.CONFIG_DIR as dir_path:
if not dir_path.exists():
dir_path.mkdir(parents=True, exist_ok=True)

def __init_logging(self):
"""
初始化 loggging 日志模块内容
"""
with self.CONFIG.open(mode="r", encoding="utf-8") as file:
log_level = safe_load(file).get("Settings").get("log_level") or "INFO"

formatter = "[%(levelname)s]%(asctime)s - %(message)s"
logging.basicConfig(format=formatter, level=getattr(logging, log_level))

@property
def BASE_DIR(self) -> Path:
return Path(__file__).parents[2]

@property
def CONFIG_DIR(self) -> Path:
return self.BASE_DIR / "config"

@property
def CONFIG(self) -> Path:
return self.CONFIG_DIR / "config.yaml"

@property
def AlistServerList(self) -> list[dict[str, any]]:
with self.CONFIG.open(mode="r", encoding="utf-8") as file:
alist_server_list = safe_load(file).get("Alist2StrmList", [])
return alist_server_list


settings = SettingManager()
49 changes: 27 additions & 22 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
# -*- coding:utf-8 -*-
import argparse
#!/usr/bin/env python3
# encoding: utf-8

import logging
import asyncio
from sys import path
from os.path import dirname

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger

import autofilm
from version import APP_VERSION
path.append(dirname(__file__))
from core import settings
from modules import Alist2Strm

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Autofilm参数配置")
parser.add_argument(
"--config_path", type=str, help="配置文件路径", default="../config/config.yaml"
)
parser.add_argument(
"--log_level",
type=str,
help="日志级别",
default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
)
args = parser.parse_args()
logging.info(f"当前的APP版本是:{settings.APP_VERSION}")

formatter = "[%(asctime)s][%(levelname)s]%(funcName)s:%(message)s"
logging.basicConfig(format=formatter, level=getattr(logging, args.log_level))
scheduler = AsyncIOScheduler()

for server in settings.AlistServerList:
cron = server.get("cron")
if cron:
scheduler.add_job(Alist2Strm(**server).run,trigger=CronTrigger.from_crontab(cron))
logging.info(f"{server["id"]}已被添加至后台任务")
else:
logging.warning(f"{server["id"]}未设置Cron")

logging.info(f"当前的APP版本是:{APP_VERSION}")
logging.info(f"配置文件路径:{args.config_path}")
scheduler.start()

my_autofilm = autofilm.AutoFilm(config_path=args.config_path)
my_autofilm.run_Alist2Strm()
try:
asyncio.get_event_loop().run_forever()
except (KeyboardInterrupt, SystemExit):
print("AutoFilm程序退出!")
55 changes: 26 additions & 29 deletions config/config.yaml.example
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
Settings:
output_dir: /media # 输出路径
subtitle: True # 是否下载字幕
img: False # 是否下载图片
nfo: True # 是否下载视频信息文件
library_mode: False # 媒体库模式
log_level: DEBUG # 日志输出等级(可选,默认 INFO)

AlistServerList:
- id: 动漫A # 任意字符,用于标识Alist服务器
url: https://alist.example.com # Alist服务器URL
username: alist # Alist账号
password: adminadmin # Alist密码
base_path: /ani/A/ # Alist服务器上文件夹路径
token: # AList未启用签名时,设置为空字符串
async_mode: False # 设置 subtitle,img,nfo 等二进制文件是否启用异步下载
overwrite: False # 本地路径存在同名文件时是否重新生成/下载该文件

- id: 电影A
url: https://alist.example1.com
- cron: 0 20 * * * # 后台定时任务 Crontable 表达式
url: https://alist.akimio.top # Alist 服务器地址
username: admin # Alist 账号
password: adminadmin # Alist 密码
token: # AList 未启用签名时,设置为空字符串
source_dir: /ani/A # Alist 服务器上文件夹路径
target_dir: D:\media\ # 输出路径
flatten_mode: False # 平铺模式,开启后 subtitle、image、nfo 强制关闭(可选,默认 False)
subtitle: False # 是否下载字幕文件(可选,默认 False)
image: False # 是否下载图片文件(可选,默认 False)
nfo: False # 是否下载 .nfo 文件(可选,默认 False)
overwrite: False # 覆盖模式,本地路径存在同名文件时是否重新生成/下载该文件(可选,默认 False)
max_workers: 5 # 下载文件最大并发数(可选,默认 5)
- cron: 0 0 7 * *
url: http://alist.example2.com:5244
username: alist
password: alist
base_path: /movie/A/
token:
async_mode: True
overwrite: False

- id: OneDrive
url: https://alist.example2.com
username: alist
password: adminadmin
base_path: /网盘/OneDrive/
password: alist
token: alist-a1b2c3d4-12...
async_mode: False
overwrite: True
source_dir: /ani/A
target_dir: ~/media/my_video
flatten_mode: False
subtitle: False
image: False
nfo: False
overwrite: False
max_workers: 5

13 changes: 1 addition & 12 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,4 @@ if [[ ! -f $CONFIG_PATH ]]; then
echo "配置文件 $CONFIG_PATH 不存在,请到映射目录下进行设置!"
fi

if [[ "$INTERVAL" -eq 0 ]]; then
echo "正在执行主程序..."
python /app/main.py --config_path $CONFIG_PATH --log_level $LOG_LEVEL
echo "执行完成,容器即将退出。"
else
while true; do
echo "正在执行主程序..."
python /app/main.py --config_path $CONFIG_PATH --log_level $LOG_LEVEL
echo "等待 $INTERVAL 秒后再次执行..."
sleep $INTERVAL
done
fi
python /app/main.py
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aiofile == 3.8.8
PyYAML == 6.0.1
python-alist == 0.0.13.0.2
python-alist == 0.0.13.0.2
APScheduler == 3.10.4

0 comments on commit 3662594

Please sign in to comment.