diff --git a/data b/data index 8105eda..454b7c9 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit 8105eda5d107fc7395d6c87a5bc0eddfb386b66e +Subproject commit 454b7c914ad1cd5b16885877b9fa4c2204939c43 diff --git a/data_model/actual_data/_story/story_part_auto.py b/data_model/actual_data/_story/story_part_auto.py index 58ec4ac..76dae6b 100644 --- a/data_model/actual_data/_story/story_part_auto.py +++ b/data_model/actual_data/_story/story_part_auto.py @@ -67,7 +67,8 @@ def __init__(self, data, story): self.data_special = { "flag": self.data["special"]["flag"], "track": None if self.data["special"]["track"] == "OST_0" else TrackInfo.get_instance( - self.data["special"]["track"]) + self.data["special"]["track"]), + "char": [] if "char" not in self.data["special"].keys() else self.data["special"]["char"] } def load(self): @@ -188,10 +189,14 @@ def to_json(self): "char_to_char": self.data["char_to_char"], "special": { "flag": self.data_special["flag"], - "track": None if self.data_special["track"] is None else self.data_special["track"].to_json_basic() + "track": None if self.data_special["track"] is None else self.data_special["track"].to_json_basic(), + "char": self.data_special["char"] } } + def to_json_basic(self): + return self.to_json() + @property def bgm_special(self): # see this traceback: @@ -209,4 +214,7 @@ def to_json_basic_tracks(self): # traceback: # File "F:\GitFile\BA_OST_Index_Parser\data_model\actual_data\story.py", line 355, in to_json # t["bgm_special"] = self.part.to_json_basic_tracks() - return [self.data_special["track"].to_json_basic()] + try: + return [self.data_special["track"].to_json_basic()] + except Exception: + return [] diff --git a/data_model/actual_data/_track/track_playback.py b/data_model/actual_data/_track/track_playback.py new file mode 100644 index 0000000..55be258 --- /dev/null +++ b/data_model/actual_data/_track/track_playback.py @@ -0,0 +1,83 @@ +import logging +from data_model.loader import i18n_translator +from data_model.tool.to_json import IToJson +from data_model.loader.manager_constant import constant_manager +from collections import UserList + + +class TrackPlaybackDataEntry(IToJson): + def __init__(self, nx_data: dict, iframe_data: dict): + self.nx_data = nx_data + self.iframe_data = iframe_data + + self.process() + + @staticmethod + def get_iframe_url(para1_type: int, para2_id: int, url: str = "https://music.163.com/outchain/player"): + if url == "": + url = "https://music.163.com/outchain/player" + return f"{url}?type={para1_type}&id={para2_id}" + + def process(self): + self.result = { + "id": self.nx_data["id"], + "path": self.nx_data["path"], + "type": i18n_translator.query(f'[TRACK_PLAYBACK_TYPE_{self.nx_data["type"]}]'), + "desc": i18n_translator.query(self.nx_data["desc"]), + "timestamp": self.nx_data["timestamp"] + } + + if self.iframe_data["iframe_parameter"]["url"] != "": + self.result["iframe_url"] = self.get_iframe_url(**self.iframe_data["iframe_parameter"]) + else: + self.result["iframe_url"] = self.iframe_data["url_detail"] + + def to_json(self): + return { + "id": self.result["id"], + "path": self.result["path"], + "type": self.result["type"].to_json(), + "desc": self.result["desc"].to_json(), + "timestamp": self.result["timestamp"], + "iframe_url": self.result["iframe_url"] + } + + def to_json_basic(self): + return self.to_json() + + +class TrackPlaybackDataListManager(UserList, IToJson): + def load(self, data: list): + for i in data: + self.append(TrackPlaybackDataEntry(i)) + + def to_json(self): + return [i.to_json_basic() for i in self] + + def to_json_basic(self): + return self.to_json() + + +class TrackPlayback(IToJson): + def __init__(self, instance_id: str): + self.instance_id = instance_id + self.all_data = TrackPlaybackDataListManager() + + self.process() + + def process(self): + try: + all_playback_data = constant_manager.query("track_playback", self.instance_id) + + for entry in all_playback_data: + iframe_data = constant_manager.query("track_playback_ref", entry["path"]) + + self.all_data.append(TrackPlaybackDataEntry(entry, iframe_data)) + except KeyError: + logging.warning(f"No TrackPlayback info for \"{self.instance_id}\"") + + def to_json(self): + return self.all_data.to_json() + + def to_json_basic(self): + return self.all_data.to_json_basic() diff --git a/data_model/actual_data/track.py b/data_model/actual_data/track.py index 5705a08..48597cb 100644 --- a/data_model/actual_data/track.py +++ b/data_model/actual_data/track.py @@ -13,6 +13,7 @@ from data_model.actual_data.used_by import BaseUsedBy, UsedByRegisterMixin, OrderedDictWithCounter, UsedByToJsonMixin from data_model.actual_data.related_to import BaseRelatedTo, RelatedToJsonMixin, RelatedToRegisterMixin from data_model.actual_data._track.track_version import * +from data_model.actual_data._track.track_playback import TrackPlayback from data_model.actual_data.tag import TagListManager from data_model.actual_data.composer import ComposerInfo @@ -369,6 +370,7 @@ def __init__(self, **kwargs): self.stats = TrackStats(data["stats"]) self.stats.second_init(self) + self.playback_info = TrackPlayback(self.instance_id) self.bond_chars = [] @@ -400,6 +402,7 @@ def to_json(self): "track_type": self.track_type, "duration": self.duration, "duration_splited": seconds_to_minutes(self.duration), + "playback_info": self.playback_info.to_json_basic(), "composer": self.composer.to_json_basic(), "tags": self.tags.to_json_basic(), diff --git a/data_model/loader/manager_constant.py b/data_model/loader/manager_constant.py index cdc9f08..032876e 100644 --- a/data_model/loader/manager_constant.py +++ b/data_model/loader/manager_constant.py @@ -19,20 +19,27 @@ def load(self): for key in self.filelist: with open(self.join_path(key), mode="r", encoding="UTF-8") as file: temp = json.load(file) - id_ = temp["file_id"] - del temp["file_id"], temp["filetype"] + try: + id_ = temp["file_id"] + del temp["file_id"], temp["filetype"] + except KeyError: + # 默认fallback策略 + id_ = os.path.splitext(key)[0] self.constant[id_] = temp def query(self, constant_id: str, value: str or int): result = self.constant[constant_id][str(value)] - if "en" in result.keys(): - # It's an LangStringModel object! - t = LangStringModel() - t.load(result) - return t + if isinstance(result, dict): + if "en" in result.keys(): + # It's an LangStringModel object! + t = LangStringModel() + t.load(result) + return t + else: + # Maybe just a normal dict, like in `composer.json` + return result else: - # Maybe just a normal dict, like in `composer.json` return result def query_by_constant_file(self, constant_id: str): diff --git a/main.py b/main.py index ae4745e..2861e06 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ import os -import json +import pickle import time import shutil from functools import partial @@ -29,7 +29,7 @@ BASE_EXPORT = "data_export" join_base = partial(os.path.join, BASE_EXPORT) -dump_json = partial(json.dump, ensure_ascii=False) +dump_json = partial(pickle.dump) # deleting old files start_time = time.time() @@ -51,7 +51,7 @@ def write_loader(target_loader): loader = target_loader.loader path = loader.get_path(filename=True) create_export_dir(loader) - with open(join_base(path), mode="w", encoding="UTF-8") as file: + with open(join_base(path), mode="wb") as file: dump_json(loader.to_json(), file) @@ -60,7 +60,7 @@ def write_loader2(target_loader): loader = target_loader path = loader.get_path(filename=False) os.makedirs(join_base(path), exist_ok=True) - with open(join_base(path, "_all.json"), mode="w", encoding="UTF-8") as file: + with open(join_base(path, "_all.json"), mode="wb") as file: dump_json(loader.to_json(), file) @@ -69,7 +69,7 @@ def write_loader3(target_loader): loader = target_loader path = loader.get_path(filename=False) os.makedirs(join_base(os.path.split(path)[0]), exist_ok=True) - with open(join_base(path), mode="w", encoding="UTF-8") as file: + with open(join_base(path), mode="wb") as file: dump_json(loader.to_json(), file) @@ -115,7 +115,7 @@ def write_loader3(target_loader): splited = path.split("/") path = "/".join([*splited, splited[-1] + ".json"]) - with open(join_base(path), mode="w", encoding="UTF-8") as file: + with open(join_base(path), mode="wb") as file: dump_json(loader.to_json(), file) if char.loader.filetype == -53: diff --git a/main_quick_test.py b/main_quick_test.py index ef44105..e179ee0 100644 --- a/main_quick_test.py +++ b/main_quick_test.py @@ -1,6 +1,5 @@ import time -import os -import json +import pickle from functools import partial from data_model.loader.loader_detect import get_loader_by_filepath @@ -25,7 +24,7 @@ PostExecutionManager.execute_pool("background_character_direct") print(f"Linking Stuff Together: {time.time() - start_time:0.2f}") -dump_json = partial(json.dumps, ensure_ascii=False) +dump_json = partial(pickle.dumps) def write_loader(target_loader):