Skip to content

Commit

Permalink
update:将structure2dict、dict2structure封装入AlistUrlTreeUtils类
Browse files Browse the repository at this point in the history
  • Loading branch information
Akimio521 committed Jul 27, 2024
1 parent ef118f0 commit 7e8d265
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 69 deletions.
6 changes: 3 additions & 3 deletions app/modules/ani2alist/ani2alist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from urllib.parse import quote

from app.core import logger
from app.utils import structure_to_dict, dict_to_structure, retry
from app.utils import AlistUrlTreeUtils, retry
from app.modules.alist import AlistClient, AlistStorage

VIDEO_MINETYPE: Final = frozenset(("video/mp4", "video/x-matroska"))
Expand Down Expand Up @@ -146,7 +146,7 @@ def merge_dicts(target_dict: dict, source_dict: dict) -> dict:
if storage:
logger.debug(f"发现存储器{self.__target_dir},开始更新番剧")
addition_dict = storage.addition
url_dict = structure_to_dict(addition_dict.get("url_structure", {}))
url_dict = AlistUrlTreeUtils.structure2dict(addition_dict.get("url_structure", {}))

if url_dict.get(folder) is None:
url_dict[folder] = {}
Expand All @@ -155,7 +155,7 @@ def merge_dicts(target_dict: dict, source_dict: dict) -> dict:
url_dict[folder], anime_dict
)

addition_dict["url_structure"] = dict_to_structure(url_dict)
addition_dict["url_structure"] = AlistUrlTreeUtils.dict2structure(url_dict)
storage.change_addition(addition_dict)

await client.sync_api_admin_storage_update(storage)
Expand Down
2 changes: 1 addition & 1 deletion app/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from app.utils.urltree import structure_to_dict, dict_to_structure
from app.utils.alist_url_tree import AlistUrlTreeUtils
from app.utils.retry import retry
77 changes: 77 additions & 0 deletions app/utils/alist_url_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class AlistUrlTreeUtils:
""" "
Alist 地址树结构转换工具
"""

@staticmethod
def structure2dict(text: str) -> dict:
"""
将能够被 Alist 地址树识别的文本转换为字典,支持键值对中包含两个冒号的情况
"""
lines = text.strip().split("\n")

def parse_lines(
start_index: int = 0, indent_level: int = 0
) -> tuple[dict, int]:
result_dict = {}
i = start_index
while i < len(lines):
line = lines[i]
current_indent = len(line) - len(line.lstrip())

if current_indent > indent_level:
sub_dict, new_index = parse_lines(i, current_indent)
result_dict[current_folder] = sub_dict
i = new_index
continue

elif current_indent < indent_level:
break

else:
parts = line.strip().split(":")
if len(parts) == 4:
key, value1, value2 = (
parts[0].strip(),
parts[1].strip(),
":".join(parts[2:]).strip(),
)
result_dict[key] = [value1, value2]
elif len(parts) >= 3:
key, value = parts[0].strip(), ":".join(parts[1:]).strip()
result_dict[key] = value
else:
current_folder = parts[0]
result_dict[current_folder] = {}
i += 1

return result_dict, i

result_dict, _ = parse_lines()
return result_dict

@staticmethod
def dict2structure(dictionary: dict) -> str:
"""
将字典转换为能够被 Alist 地址树识别的文本
"""

def parse_dict(
sub_dictionary: dict[str, str | list[str] | dict], indent: int = 0
):
result_str = ""
for key, value in sub_dictionary.items():
if isinstance(value, str):
result_str += " " * indent + f"{key}:{value}\n"
elif isinstance(value, list):
result_str += " " * indent + f"{key}:{value[0]}:{value[1]}\n"
elif isinstance(value, dict):
result_str += " " * indent + f"{key}:\n"
result_str += parse_dict(value, indent + 2)

if indent == 0 and result_str.startswith(":"):
result_str = result_str.lstrip(":").strip()

return result_str

return parse_dict(dictionary)
65 changes: 0 additions & 65 deletions app/utils/urltree.py

This file was deleted.

0 comments on commit 7e8d265

Please sign in to comment.