Compare commits

...

3 Commits

Author SHA1 Message Date
Cp0204
465eba0709 优化 strm 链接主机替换参数处理 2024-11-18 16:03:22 +08:00
Cp0204
64b144b613 优化 alist-strm-lite 模块,从Alist API读取存储信息 2024-11-18 15:12:36 +08:00
xiaoQQya
00e730ee9e ♻️ 重构 alist-strm-lite 模块,使用 Alist API
- 由 WebDAV 更改为 Alist API
- 避免页面暴露 Alist 明文密码

(cherry picked from commit 4d6a465057)
2024-11-18 11:52:47 +08:00
2 changed files with 124 additions and 47 deletions

View File

@ -2,22 +2,32 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
""" """
@File : alist_strm_lite.py @File : alist_strm_lite.py
@Desc : Alist 生成 strm 文件简化版基于 WebDAV @Desc : Alist 生成 strm 文件简化版
@Version : v1.0 @Version : v1.1
@Time : 2024/11/16 @Time : 2024/11/16
@Author : xiaoQQya @Author : xiaoQQya
@Contact : xiaoQQya@126.com @Contact : xiaoQQya@126.com
""" """
import os import os
import re import json
from webdav3.client import Client import requests
class Alist_strm_lite: class Alist_strm_lite:
video_exts = ["mp4", "mkv", "flv", "mov", "m4v", "avi", "webm", "wmv"] video_exts = ["mp4", "mkv", "flv", "mov", "m4v", "avi", "webm", "wmv"]
default_config = {"url": "", "webdav_username": "", "webdav_password": "", "path_prefix": "/quark", "quark_root_dir": "/", "strm_save_dir": "/media", "strm_url_host": ""} 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 is_active = False
# 缓存参数
storage_mount_path = None
quark_root_dir = None
strm_server = None
def __init__(self, **kwargs): def __init__(self, **kwargs):
if kwargs: if kwargs:
@ -26,65 +36,133 @@ class Alist_strm_lite:
setattr(self, key, kwargs[key]) setattr(self, key, kwargs[key])
else: else:
print(f"{self.__class__.__name__} 模块缺少必要参数: {key}") print(f"{self.__class__.__name__} 模块缺少必要参数: {key}")
if self.url and self.token and self.storage_id:
options = { storage_info = self.get_storage_info(self.storage_id)
"webdav_hostname": f"{self.url.rstrip('/')}/dav/", if storage_info:
"webdav_login": self.webdav_username, addition = json.loads(storage_info["addition"])
"webdav_password": self.webdav_password, # 存储挂载路径
"disable_check": True self.storage_mount_path = storage_info["mount_path"]
} # 夸克根文件夹
self.client = Client(options) self.quark_root_dir = self.get_root_folder_full_path(
addition["cookie"], addition["root_folder_id"]
if self.url and self.webdav_username and self.webdav_password and self.get_info(): )
self.is_active = True if self.storage_mount_path and self.quark_root_dir:
self.is_active = True
# 替换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): def run(self, task):
if task.get("savepath") and task.get("savepath").startswith(self.quark_root_dir): if task.get("savepath") and task.get("savepath").startswith(
path = self._normalize_path(task["savepath"]) self.quark_root_dir
self.refresh(path) ):
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 get_info(self): def get_storage_info(self, storage_id):
url = f"{self.url}/api/admin/storage/get"
headers = {"Authorization": self.token}
querystring = {"id": storage_id}
try: try:
response = self.client.info("/") response = requests.request("GET", url, headers=headers, params=querystring)
if response.get("name"): response.raise_for_status()
print(f"Alist2strm Lite: {response.get('name')}") data = response.json()
return True if data.get("code") == 200:
print(f"Alist-strm Lite: Storage[{data['data']['mount_path']}]")
return data.get("data", [])
else: else:
print(f"Alist2strm Lite: 连接失败❌ {response}") print(f"Alist-strm Lite: 连接失败❌ {response.get('message')}")
except Exception as e: except requests.exceptions.RequestException as e:
print(f"Alist2strm Lite: 获取信息出错 {e}") print(f"Alist-strm Lite: 获取Alist存储出错 {e}")
return False return False
def refresh(self, path): def refresh(self, path):
try: try:
files = self.client.list(path) response = self.get_file_list(path)
if response.get("code") != 200:
for item in files[1:]: print(f"📺 生成 STRM 文件失败❌ {response.get('message')}")
full_path = re.sub(r"/{2,}", "/", f"{path}/{item}") return
if full_path.endswith("/"): else:
self.refresh(full_path) files = response.get("data").get("content")
else: for item in files:
self.generate_strm(full_path) item_path = f"{path}/{item.get('name')}".replace("//", "/")
return True if item.get("is_dir"):
self.refresh(item_path)
else:
self.generate_strm(item_path)
except Exception as e: except Exception as e:
print(f"📺 生成STRM文件失败❌ {e}") print(f"📺 获取 Alist 文件列表失败❌ {e}")
return False
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): def generate_strm(self, file_path):
ext = file_path.split(".")[-1] ext = file_path.split(".")[-1]
if ext.lower() in self.video_exts: if ext.lower() in self.video_exts:
strm_path = re.sub(r"/{2,}", "/", f"{self.strm_save_dir}{file_path.rstrip(ext)}strm") strm_path = (
f"{self.strm_save_dir}{os.path.splitext(file_path)[0]}.strm".replace(
"//", "/"
)
)
if os.path.exists(strm_path): if os.path.exists(strm_path):
return return
if not os.path.exists(os.path.dirname(strm_path)): if not os.path.exists(os.path.dirname(strm_path)):
os.makedirs(os.path.dirname(strm_path)) os.makedirs(os.path.dirname(strm_path))
with open(strm_path, "w", encoding="utf-8") as strm_file: with open(strm_path, "w", encoding="utf-8") as strm_file:
host = self.strm_url_host.rstrip("/") if self.strm_url_host.strip() else self.url.rstrip("/") strm_file.write(f"{self.strm_server}{file_path}")
strm_file.write(f"{host}/d{file_path}")
print(f"📺 生成STRM文件 {strm_path} 成功✅") print(f"📺 生成STRM文件 {strm_path} 成功✅")
def _normalize_path(self, path): def get_root_folder_full_path(self, cookie, pdir_fid):
"""标准化路径格式""" if pdir_fid == "0":
if not path.startswith(self.path_prefix): return "/"
path = f"/{self.path_prefix}/{path.lstrip(self.quark_root_dir)}" url = "https://drive-h.quark.cn/1/clouddrive/file/sort"
return re.sub(r"/{2,}", "/", path) 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 Lite: 获取Quark路径出错 {e}")
return False

View File

@ -2,4 +2,3 @@ flask
apscheduler apscheduler
requests requests
treelib treelib
webdavclient3