Compare commits

...

7 Commits

Author SHA1 Message Date
Cp0204
357c59eb55 添加媒体服务器优先级配置及加载逻辑
Some checks are pending
Docker Publish / build-and-push (push) Waiting to run
- 新增媒体服务器优先级配置文件(_priority.json)
- 调整模块加载顺序以支持优先级设置
2024-11-19 04:20:34 +08:00
Cp0204
1539b56717 📝 添加媒体库模块说明 2024-11-19 03:45:38 +08:00
Cp0204
2eb7376fb5 优化 Alist 模块,以 storage_id 获取存储路径参数
- 增加 storage_id_to_path 方法以处理存储 ID
- 更新构造函数以支持存储路径和根目录的缓存
- 修改 run 方法以使用新的存储路径逻辑
- 改进 get_info 方法以提供更清晰的输出信息
- 添加 get_storage_info 方法以获取存储信息
- 更新 refresh 方法以提供更详细的错误信息
- 增加 get_root_folder_full_path 方法以获取根文件夹完整路径
2024-11-19 03:06:58 +08:00
Cp0204
c639b0d763 💄 优化Alist_strm配置注释及日志信息
- 更新默认配置字典的注释,增加可读性
- 修改打印语句,使其更清晰地表达配置运行状态
2024-11-19 03:05:38 +08:00
Cp0204
8a53651195 调整 storage_id 参数处理逻辑
- 新增 storage_id_to_path 方法以处理存储 ID
- 支持以 /aaa:/bbb 直接匹配挂载路径和夸克根文件夹
- 优化存储信息获取逻辑,简化代码结构
2024-11-19 03:04:51 +08:00
Cp0204
baf5d751ca 🔧 优化Quark模块,增加ls_dir参数及异常处理
- 为 ls_dir 方法添加可选参数 kwargs,以增强灵活性
- 在加载媒体服务器时,捕获异常并打印详细错误信息
2024-11-19 03:00:19 +08:00
Cp0204
42212dc615
新增 alist_strm_gen 模块,可直接生成 strm (#41)
*  新增 alist-strm-lite 模块,基于 WebDAV 生成 strm

(cherry picked from commit 9c859b212e)

* ♻️ 重构 alist-strm-lite 模块,使用 Alist API

- 由 WebDAV 更改为 Alist API
- 避免页面暴露 Alist 明文密码

(cherry picked from commit 4d6a465057)

*  优化 alist-strm-lite 模块,从Alist API读取存储信息

*  优化 strm 链接主机替换参数处理

* 🎨 增加驱动不支持提示

* 🔧 重命名 alist_strm_lite 为 alist_strm_gen

- 更易识别功能

---------

Co-authored-by: xiaoQQya <xiaoQQya@126.com>
2024-11-19 01:52:11 +08:00
6 changed files with 335 additions and 30 deletions

View File

@ -77,10 +77,12 @@ docker run -d \
-e WEBUI_USERNAME=admin \
-e WEBUI_PASSWORD=admin123 \
-v ./quark-auto-save/config:/app/config \
-v /etc/localtime:/etc/localtime \
-v ./quark-auto-save/media:/media \ # 可选模块alist_strm_gen生成strm使用
-v /etc/localtime:/etc/localtime \ # 可选,同步宿主机时区
--network bridge \
--restart unless-stopped \
cp0204/quark-auto-save:latest
# registry.cn-shenzhen.aliyuncs.com/cp0204/quark-auto-save:latest # 国内镜像地址
```
docker-compose.yml
@ -90,7 +92,6 @@ name: quark-auto-save
services:
quark-auto-save:
image: cp0204/quark-auto-save:latest
# image: registry.cn-shenzhen.aliyuncs.com/cp0204/quark-auto-save:latest
container_name: quark-auto-save
network_mode: bridge
ports:
@ -101,6 +102,7 @@ services:
WEBUI_PASSWORD: "admin123"
volumes:
- ./quark-auto-save/config:/app/config
- ./quark-auto-save/media:/media
- /etc/localtime:/etc/localtime
```
@ -130,7 +132,7 @@ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtow
程序也支持以青龙定时任务的方式运行,但该方式无法使用 WebUI 管理任务,需手动修改配置文件。
青龙部署说明已转移到 Wiki [青龙部署教程](https://github.com/Cp0204/quark-auto-save/wiki/%E9%83%A8%E7%BD%B2%E6%95%99%E7%A8%8B#%E9%9D%92%E9%BE%99%E9%83%A8%E7%BD%B2)
青龙部署说明已转移到 Wiki [青龙部署教程](https://github.com/Cp0204/quark-auto-save/wiki/青龙部署教程)
### 正则整理示例
@ -143,7 +145,15 @@ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtow
| `$TV` | | [魔法匹配](#魔法匹配)剧集文件 |
| `^(\d+)\.mp4` | `$TASKNAME.S02E\1.mp4` | 01.mp4 → 任务名.S02E01.mp4 |
更多正则使用说明已转移到 Wiki [正则处理教程](https://github.com/Cp0204/quark-auto-save/wiki/%E6%AD%A3%E5%88%99%E5%A4%84%E7%90%86%E6%95%99%E7%A8%8B)
更多正则使用说明已转移到 Wiki [正则处理教程](https://github.com/Cp0204/quark-auto-save/wiki/正则处理教程)
### 媒体库配置
媒体库模块主要在执行任务,有新转存时触发完成相应功能,如刷新媒体库、生成 .strm 文件等。
媒体库目前已完成模块化,可以很方便地挂载集成,如果你有兴趣请参考[媒体库模块开发指南](https://github.com/Cp0204/quark-auto-save/tree/main/media_servers)。
目前已完成的模块配置参考 Wiki [媒体库模块配置教程](https://github.com/Cp0204/quark-auto-save/wiki/媒体库模块配置教程)
### 特殊场景使用技巧

View File

@ -0,0 +1,7 @@
[
"alist",
"alist_strm",
"alist_strm_gen",
"emby",
"plex"
]

View File

@ -1,4 +1,6 @@
import os
import re
import json
import requests
@ -7,27 +9,38 @@ class Alist:
default_config = {
"url": "", # Alist服务器URL
"token": "", # Alist服务器Token
"quark_root_path": "/quark", # 夸克根目录在Alist中的挂载路径
"storage_id": "", # Alist 服务器夸克存储 ID
}
is_active = False
# 缓存参数
storage_mount_path = None
quark_root_dir = None
def __init__(self, **kwargs):
if kwargs:
for key, value in self.default_config.items():
for key, _ in self.default_config.items():
if key in kwargs:
setattr(self, key, kwargs[key])
else:
print(f"{self.__class__.__name__} 模块缺少必要参数: {key}")
if self.url and self.token:
if self.get_info():
self.is_active = True
success, result = self.storage_id_to_path(self.storage_id)
if success:
self.storage_mount_path, self.quark_root_dir = result
self.is_active = True
def run(self, task):
if task.get("savepath"):
full_path = os.path.normpath(
os.path.join(self.quark_root_path, task["savepath"].lstrip("/"))
if task.get("savepath") and task.get("savepath").startswith(
self.quark_root_dir
):
alist_path = os.path.normpath(
os.path.join(
self.storage_mount_path,
task["savepath"].replace(self.quark_root_dir, "", 1).lstrip("/"),
)
).replace("\\", "/")
self.refresh(full_path)
self.refresh(alist_path)
def get_info(self):
url = f"{self.url}/api/admin/setting/list"
@ -39,15 +52,52 @@ class Alist:
response = response.json()
if response.get("code") == 200:
print(
f"Alist: {response.get('data',[])[1].get('value','')} {response.get('data',[])[0].get('value','')}"
f"Alist刷新: {response.get('data',[])[1].get('value','')} {response.get('data',[])[0].get('value','')}"
)
return True
else:
print(f"Alist: 连接失败❌ {response.get('message')}")
print(f"Alist刷新: 连接失败❌ {response.get('message')}")
except requests.exceptions.RequestException as e:
print(f"获取Alist信息出错: {e}")
return False
def storage_id_to_path(self, storage_id):
# 1. 检查是否符合 /aaa:/bbb 格式
match = re.match(r"^(\/[^:]*):(\/[^:]*)$", storage_id)
if match:
return True, (match.group(1), match.group(2))
# 2. 调用 Alist API 获取存储信息
storage_info = self.get_storage_info(storage_id)
if storage_info:
if storage_info["driver"] == "Quark":
addition = json.loads(storage_info["addition"])
# 存储挂载路径
storage_mount_path = storage_info["mount_path"]
# 夸克根文件夹
quark_root_dir = self.get_root_folder_full_path(
addition["cookie"], addition["root_folder_id"]
)
if storage_mount_path and quark_root_dir:
return True, (storage_mount_path, quark_root_dir)
else:
print(f"Alist刷新: 不支持[{storage_info['driver']}]驱动 ❌")
def get_storage_info(self, storage_id):
url = f"{self.url}/api/admin/storage/get"
headers = {"Authorization": self.token}
querystring = {"id": storage_id}
try:
response = requests.request("GET", url, headers=headers, params=querystring)
response.raise_for_status()
data = response.json()
if data.get("code") == 200:
return data.get("data", [])
else:
print(f"Alist刷新: 存储{storage_id}连接失败❌ {data.get('message')}")
except requests.exceptions.RequestException as e:
print(f"Alist刷新: 获取Alist存储出错 {e}")
return False
def refresh(self, path, force_refresh=True):
url = f"{self.url}/api/fs/list"
headers = {"Authorization": self.token}
@ -61,22 +111,55 @@ class Alist:
try:
response = requests.request("POST", url, headers=headers, json=payload)
response.raise_for_status()
response = response.json()
if response.get("code") == 200:
print(f"📁 刷新Alist目录[{path}] 成功✅")
return response.get("data")
elif "object not found" in response.get("message", ""):
data = response.json()
if data.get("code") == 200:
print(f"📁 Alist刷新:目录[{path}] 成功✅")
return data.get("data")
elif "object not found" in data.get("message", ""):
# 如果是根目录就不再往上查找
if path == "/" or path == self.quark_root_path:
print(f"📁 刷新Alist目录:根目录不存在,请检查 Alist 配置")
print(f"📁 Alist刷新:根目录不存在,请检查 Alist 配置")
return False
# 获取父目录
parent_path = os.path.dirname(path)
print(f"📁 刷新Alist目录[{path}] 不存在,转父目录 [{parent_path}]")
print(f"📁 Alist刷新[{path}] 不存在,转父目录 [{parent_path}]")
# 递归刷新父目录
return self.refresh(parent_path)
else:
print(f"📁 刷新Alist目录失败❌ {response.get('message')}")
print(f"📁 Alist刷新失败❌ {data.get('message')}")
except requests.exceptions.RequestException as e:
print(f"刷新Alist目录出错: {e}")
print(f"Alist刷新目录出错: {e}")
return False
def get_root_folder_full_path(self, cookie, pdir_fid):
if pdir_fid == "0":
return "/"
url = "https://drive-h.quark.cn/1/clouddrive/file/sort"
headers = {
"cookie": cookie,
"content-type": "application/json",
}
querystring = {
"pr": "ucpro",
"fr": "pc",
"uc_param_str": "",
"pdir_fid": pdir_fid,
"_page": 1,
"_size": "50",
"_fetch_total": "1",
"_fetch_sub_dirs": "0",
"_sort": "file_type:asc,updated_at:desc",
"_fetch_full_path": 1,
}
try:
response = requests.request(
"GET", url, headers=headers, params=querystring
).json()
if response["code"] == 0:
file_names = [
item["file_name"] for item in response["data"]["full_path"]
]
return "/".join(file_names)
except requests.exceptions.RequestException as e:
print(f"Alist刷新: 获取Quark路径出错 {e}")
return False

View File

@ -1,12 +1,19 @@
import re
import requests
"""
配合 alist-strm 项目触发特定配置运行
https://github.com/tefuirZ/alist-strm
"""
class Alist_strm:
default_config = {"url": "", "cookie": "", "config_id": ""}
default_config = {
"url": "", # alist-strm服务器URL
"cookie": "", # alist-strm的cookieF12抓取关键参数session=ey***
"config_id": "", # 要触发运行的配置ID
}
is_active = False
def __init__(self, **kwargs):
@ -34,10 +41,10 @@ class Alist_strm:
match = re.search(r'name="config_name" value="([^"]+)"', html_content)
if match:
config_name = match.group(1)
print(f"alist-strm配置: {config_name}")
print(f"alist-strm配置运行: {config_name}")
return True
else:
print(f"alist-strm配置: 匹配失败❌")
print(f"alist-strm配置运行: 匹配失败❌")
except requests.exceptions.RequestException as e:
print(f"获取alist-strm配置信息出错: {e}")
return False

View File

@ -0,0 +1,186 @@
#!/usr/bin/python3
# -*- encoding: utf-8 -*-
"""
@File : alist_strm_gen.py
@Desc : Alist 生成 strm 文件简化版
@Version : v1.1
@Time : 2024/11/16
@Author : xiaoQQya
@Contact : xiaoQQya@126.com
"""
import os
import re
import json
import requests
class Alist_strm_gen:
video_exts = ["mp4", "mkv", "flv", "mov", "m4v", "avi", "webm", "wmv"]
default_config = {
"url": "", # Alist 服务器 URL
"token": "", # Alist 服务器 Token
"storage_id": "", # Alist 服务器夸克存储 ID
"strm_save_dir": "/media", # 生成的 strm 文件保存的路径
"strm_replace_host": "", # strm 文件内链接的主机地址 (可选,缺省时=url
}
is_active = False
# 缓存参数
storage_mount_path = None
quark_root_dir = None
strm_server = None
def __init__(self, **kwargs):
if kwargs:
for key, _ in self.default_config.items():
if key in kwargs:
setattr(self, key, kwargs[key])
else:
print(f"{self.__class__.__name__} 模块缺少必要参数: {key}")
if self.url and self.token and self.storage_id:
success, result = self.storage_id_to_path(self.storage_id)
if success:
self.is_active = True
# 存储挂载路径, 夸克根文件夹
self.storage_mount_path, self.quark_root_dir = result
# 替换strm文件内链接的主机地址
self.strm_replace_host = self.strm_replace_host.strip()
if self.strm_replace_host:
if self.strm_replace_host.startswith("http"):
self.strm_server = f"{self.strm_replace_host}/d"
else:
self.strm_server = f"http://{self.strm_replace_host}/d"
else:
self.strm_server = f"{self.url.strip()}/d"
def run(self, task):
if task.get("savepath") and task.get("savepath").startswith(
self.quark_root_dir
):
alist_path = os.path.normpath(
os.path.join(
self.storage_mount_path,
task["savepath"].replace(self.quark_root_dir, "", 1).lstrip("/"),
)
).replace("\\", "/")
self.refresh(alist_path)
def storage_id_to_path(self, storage_id):
# 1. 检查是否符合 /aaa:/bbb 格式
match = re.match(r"^(\/[^:]*):(\/[^:]*)$", storage_id)
if match:
return True, (match.group(1), match.group(2))
# 2. 调用 Alist API 获取存储信息
storage_info = self.get_storage_info(storage_id)
if storage_info:
if storage_info["driver"] == "Quark":
addition = json.loads(storage_info["addition"])
# 存储挂载路径
storage_mount_path = storage_info["mount_path"]
# 夸克根文件夹
quark_root_dir = self.get_root_folder_full_path(
addition["cookie"], addition["root_folder_id"]
)
if storage_mount_path and quark_root_dir:
return True, (storage_mount_path, quark_root_dir)
else:
print(f"Alist刷新: 不支持[{storage_info['driver']}]驱动 ❌")
def get_storage_info(self, storage_id):
url = f"{self.url}/api/admin/storage/get"
headers = {"Authorization": self.token}
querystring = {"id": storage_id}
try:
response = requests.request("GET", url, headers=headers, params=querystring)
response.raise_for_status()
data = response.json()
if data.get("code") == 200:
print(
f"Alist-Strm生成: {data['data']['driver']}[{data['data']['mount_path']}]"
)
return data.get("data", [])
else:
print(f"Alist-Strm生成: 连接失败❌ {response.get('message')}")
except requests.exceptions.RequestException as e:
print(f"Alist-Strm生成: 获取Alist存储出错 {e}")
return False
def refresh(self, path):
try:
response = self.get_file_list(path)
if response.get("code") != 200:
print(f"📺 生成 STRM 文件失败❌ {response.get('message')}")
return
else:
files = response.get("data").get("content")
for item in files:
item_path = f"{path}/{item.get('name')}".replace("//", "/")
if item.get("is_dir"):
self.refresh(item_path)
else:
self.generate_strm(item_path)
except Exception as e:
print(f"📺 获取 Alist 文件列表失败❌ {e}")
def get_file_list(self, path):
url = f"{self.url}/api/fs/list"
headers = {"Authorization": self.token}
payload = {
"path": path,
"refresh": False,
"password": "",
"page": 1,
"per_page": 0,
}
response = requests.request("POST", url, headers=headers, json=payload)
response.raise_for_status()
return response.json()
def generate_strm(self, file_path):
ext = file_path.split(".")[-1]
if ext.lower() in self.video_exts:
strm_path = (
f"{self.strm_save_dir}{os.path.splitext(file_path)[0]}.strm".replace(
"//", "/"
)
)
if os.path.exists(strm_path):
return
if not os.path.exists(os.path.dirname(strm_path)):
os.makedirs(os.path.dirname(strm_path))
with open(strm_path, "w", encoding="utf-8") as strm_file:
strm_file.write(f"{self.strm_server}{file_path}")
print(f"📺 生成STRM文件 {strm_path} 成功✅")
def get_root_folder_full_path(self, cookie, pdir_fid):
if pdir_fid == "0":
return "/"
url = "https://drive-h.quark.cn/1/clouddrive/file/sort"
headers = {
"cookie": cookie,
"content-type": "application/json",
}
querystring = {
"pr": "ucpro",
"fr": "pc",
"uc_param_str": "",
"pdir_fid": pdir_fid,
"_page": 1,
"_size": "50",
"_fetch_total": "1",
"_fetch_sub_dirs": "0",
"_sort": "file_type:asc,updated_at:desc",
"_fetch_full_path": 1,
}
try:
response = requests.request(
"GET", url, headers=headers, params=querystring
).json()
if response["code"] == 0:
file_names = [
item["file_name"] for item in response["data"]["full_path"]
]
return "/".join(file_names)
except requests.exceptions.RequestException as e:
print(f"Alist-Strm生成: 获取Quark路径出错 {e}")
return False

View File

@ -274,7 +274,7 @@ class Quark:
break
return fids
def ls_dir(self, pdir_fid):
def ls_dir(self, pdir_fid, **kwargs):
file_list = []
page = 1
while True:
@ -289,6 +289,7 @@ class Quark:
"_fetch_total": "1",
"_fetch_sub_dirs": "0",
"_sort": "file_type:asc,updated_at:desc",
"_fetch_full_path": kwargs.get("fetch_full_path", 0),
}
headers = self.common_headers()
response = requests.request(
@ -689,11 +690,22 @@ class Quark:
def load_media_servers(media_servers_config, media_servers_dir="media_servers"):
media_servers = {}
available_modules = [
all_modules = [
f.replace(".py", "") for f in os.listdir(media_servers_dir) if f.endswith(".py")
]
# 调整模块优先级
priority_path = os.path.join(media_servers_dir, "_priority.json")
try:
with open(priority_path, encoding="utf-8") as f:
priority_modules = json.load(f)
if priority_modules:
all_modules = [
module for module in priority_modules if module in all_modules
] + [module for module in all_modules if module not in priority_modules]
except (FileNotFoundError, json.JSONDecodeError):
priority_modules = []
print(f"🧩 载入媒体库模块")
for module_name in available_modules:
for module_name in all_modules:
try:
module = importlib.import_module(f"{media_servers_dir}.{module_name}")
ServerClass = getattr(module, module_name.capitalize())
@ -703,8 +715,8 @@ def load_media_servers(media_servers_config, media_servers_dir="media_servers"):
media_servers[module_name] = ServerClass(**server_config)
else:
media_servers_config[module_name] = ServerClass().default_config
except (ImportError, AttributeError):
print(f"载入模块 {module_name} 失败")
except (ImportError, AttributeError) as e:
print(f"载入模块 {module_name} 失败: {e}")
print()
return media_servers