Skip to content
Open
13 changes: 9 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@ FROM python:3.12.7-alpine
ENV TZ=Asia/Shanghai
VOLUME ["/config", "/logs", "/media"]

# 安装编译依赖来构建psutil
RUN apk update && \
apk add --no-cache \
build-base \
gcc \
python3-dev \
musl-dev \
linux-headers

COPY requirements.txt requirements.txt
# 确保requirements.txt中包含psutil
RUN pip install --no-cache-dir -r requirements.txt && \
rm requirements.txt
rm requirements.txt

COPY --from=builder /builder/app /app

RUN apk del build-base linux-headers && \
# 删除不再需要的编译工具
RUN apk del gcc python3-dev musl-dev linux-headers && \
rm -rf /tmp/*

ENTRYPOINT ["python", "/app/main.py"]
ENTRYPOINT ["python", "/app/main.py"]
6 changes: 6 additions & 0 deletions app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ def Ani2AlistList(self) -> list[dict[str, any]]:
with self.CONFIG.open(mode="r", encoding="utf-8") as file:
ani2alist_list = safe_load(file).get("Ani2AlistList", [])
return ani2alist_list

@property
def TelegramBot(self) -> dict[str, any]:
with self.CONFIG.open(mode="r", encoding="utf-8") as file:
telegram_bot = safe_load(file).get("TelegramBot", {})
return telegram_bot


settings = SettingManager()
50 changes: 39 additions & 11 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from asyncio import get_event_loop
from sys import path
from os.path import dirname
import asyncio
from sys import path
from typing import Optional

path.append(dirname(dirname(__file__)))

Expand All @@ -10,26 +11,30 @@
from app.core import settings, logger
from app.extensions import LOGO
from app.modules import Alist2Strm, Ani2Alist
from app.modules.telegram_bot import TelegramBot


def print_logo() -> None:
"""
打印 Logo
"""

print(LOGO)
print(f" {settings.APP_NAME} {settings.APP_VERSION} ".center(65, "="))
print("")


if __name__ == "__main__":
async def main() -> None:
"""
主程序入口,初始化并启动所有服务
"""
print_logo()

logger.info(f"AutoFilm {settings.APP_VERSION} 启动中...")
logger.debug(f"是否开启 DEBUG 模式: {settings.DEBUG}")


# 初始化调度器
scheduler = AsyncIOScheduler()


# 配置Alist2Strm任务
if settings.AlistServerList:
logger.info("检测到 Alist2Strm 模块配置,正在添加至后台任务")
for server in settings.AlistServerList:
Expand All @@ -43,7 +48,8 @@ def print_logo() -> None:
logger.warning(f"{server['id']} 未设置 cron")
else:
logger.warning("未检测到 Alist2Strm 模块配置")


# 配置Ani2Alist任务
if settings.Ani2AlistList:
logger.info("检测到 Ani2Alist 模块配置,正在添加至后台任务")
for server in settings.Ani2AlistList:
Expand All @@ -57,11 +63,33 @@ def print_logo() -> None:
logger.warning(f"{server['id']} 未设置 cron")
else:
logger.warning("未检测到 Ani2Alist 模块配置")


# 初始化并运行Telegram机器人(如果已配置)
telegram_bot: Optional[TelegramBot] = None
if hasattr(settings, 'TelegramBot') and settings.TelegramBot.get('token'):
logger.info("检测到 Telegram Bot 配置,正在启动")
telegram_bot = TelegramBot(**settings.TelegramBot)
# 启动Telegram机器人
await telegram_bot.start()
else:
logger.info("未检测到 Telegram Bot 配置")

# 启动调度器
scheduler.start()
logger.info("AutoFilm 启动完成")

try:
get_event_loop().run_forever()
# 保持程序运行
while True:
await asyncio.sleep(1)
except (KeyboardInterrupt, SystemExit):
logger.info("AutoFilm 程序退出!")
if telegram_bot:
# 停止Telegram机器人
await telegram_bot.stop()
# 关闭调度器
scheduler.shutdown()


if __name__ == "__main__":
asyncio.run(main())
25 changes: 12 additions & 13 deletions app/modules/alist2strm/alist2strm.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,31 +210,30 @@ def __get_local_path(self, path: AlistPath) -> Path:

return local_path

async def __cleanup_local_files(self) -> None:
async def __cleanup_local_files(self) -> None :
"""
删除服务器中已删除的本地的 .strm 文件及其关联文件
如果文件后缀在 sync_ignore 中,则不会被删除
删除服务器中已删除的本地的 .strm 文件,保留元数据文件
"""
logger.info("开始清理本地文件")

if self.flatten_mode:
all_local_files = [f for f in self.target_dir.iterdir() if f.is_file()]
else:
all_local_files = [f for f in self.target_dir.rglob("*") if f.is_file()]
if self.flatten_mode :
all_local_files = [f for f in self.target_dir.iterdir() if f.is_file() and f.suffix.lower() == ".strm"]
else :
all_local_files = [f for f in self.target_dir.rglob("*.strm") if f.is_file()]

files_to_delete = set(all_local_files) - self.processed_local_paths

for file_path in files_to_delete:
for file_path in files_to_delete :
# 检查文件是否匹配忽略正则表达式
if self.sync_ignore_pattern and self.sync_ignore_pattern.search(
file_path.name
):
file_path.name
) :
logger.debug(f"文件 {file_path.name} 在忽略列表中,跳过删除")
continue

try:
if file_path.exists():
try :
if file_path.exists() :
await to_thread(file_path.unlink)
logger.info(f"删除文件:{file_path}")
except Exception as e:
except Exception as e :
logger.error(f"删除文件 {file_path} 失败:{e}")
4 changes: 4 additions & 0 deletions app/modules/telegram_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
Telegram Bot Module for AutoFilm
"""
from app.modules.telegram_bot.telegram_bot import TelegramBot
Loading