mirror of
https://github.com/Cp0204/quark-auto-save.git
synced 2026-01-16 01:10:44 +08:00
♻️ 重构 alist-strm-lite 模块,使用 Alist API
- 由 WebDAV 更改为 Alist API
- 避免页面暴露 Alist 明文密码
(cherry picked from commit 4d6a465057)
This commit is contained in:
parent
e8beac0d8a
commit
00e730ee9e
@ -2,21 +2,27 @@
|
|||||||
# -*- 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.0
|
||||||
@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 requests
|
||||||
from webdav3.client import Client
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
"quark_root_path": "/quark", # 夸克根目录在 Alist 中的挂载路径
|
||||||
|
"quark_root_dir": "/", # 夸克在 Alist 中挂载的根目录
|
||||||
|
"strm_save_dir": "/media", # 生成的 strm 文件保存的路径
|
||||||
|
"strm_url_host": "" # strm 文件内链接的主机地址
|
||||||
|
}
|
||||||
is_active = False
|
is_active = False
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
@ -26,54 +32,68 @@ 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.get_info():
|
||||||
options = {
|
|
||||||
"webdav_hostname": f"{self.url.rstrip('/')}/dav/",
|
|
||||||
"webdav_login": self.webdav_username,
|
|
||||||
"webdav_password": self.webdav_password,
|
|
||||||
"disable_check": True
|
|
||||||
}
|
|
||||||
self.client = Client(options)
|
|
||||||
|
|
||||||
if self.url and self.webdav_username and self.webdav_password and self.get_info():
|
|
||||||
self.is_active = True
|
self.is_active = True
|
||||||
|
|
||||||
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(self.quark_root_dir):
|
||||||
path = self._normalize_path(task["savepath"])
|
full_path = os.path.normpath(
|
||||||
self.refresh(path)
|
os.path.join(self.quark_root_path, task["savepath"].lstrip("/").lstrip(self.quark_root_dir))
|
||||||
|
).replace("\\", "/")
|
||||||
|
self.refresh(full_path)
|
||||||
|
|
||||||
def get_info(self):
|
def get_info(self):
|
||||||
|
url = f"{self.url}/api/admin/setting/list"
|
||||||
|
headers = {"Authorization": self.token}
|
||||||
|
querystring = {"group": "1"}
|
||||||
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')}")
|
response = response.json()
|
||||||
|
if response.get("code") == 200:
|
||||||
|
print(f"Alist-strm Lite: {response.get('data',[])[1].get('value','')} {response.get('data',[])[0].get('value','')}")
|
||||||
return True
|
return True
|
||||||
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.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)
|
full_path = f"{path}/{item.get('name')}".replace("//", "/")
|
||||||
return True
|
if item.get("is_dir"):
|
||||||
|
self.refresh(full_path)
|
||||||
|
else:
|
||||||
|
self.generate_strm(full_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"📺 生成STRM文件失败❌ {e}")
|
print(f"📺 获取 Alist 文件列表失败❌ {e}")
|
||||||
return False
|
|
||||||
|
def 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}{file_path.rstrip(ext)}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)):
|
||||||
@ -82,9 +102,3 @@ class Alist_strm_lite:
|
|||||||
host = self.strm_url_host.rstrip("/") if self.strm_url_host.strip() else self.url.rstrip("/")
|
host = self.strm_url_host.rstrip("/") if self.strm_url_host.strip() else self.url.rstrip("/")
|
||||||
strm_file.write(f"{host}/d{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):
|
|
||||||
"""标准化路径格式"""
|
|
||||||
if not path.startswith(self.path_prefix):
|
|
||||||
path = f"/{self.path_prefix}/{path.lstrip(self.quark_root_dir)}"
|
|
||||||
return re.sub(r"/{2,}", "/", path)
|
|
||||||
|
|||||||
@ -2,4 +2,3 @@ flask
|
|||||||
apscheduler
|
apscheduler
|
||||||
requests
|
requests
|
||||||
treelib
|
treelib
|
||||||
webdavclient3
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user