Skip to content

Commit

Permalink
story source added
Browse files Browse the repository at this point in the history
  • Loading branch information
sctop committed Dec 9, 2023
1 parent 9618c48 commit db9374c
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 129 deletions.
2 changes: 1 addition & 1 deletion data
27 changes: 6 additions & 21 deletions data_model/actual_data/_story/story_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from data_model.actual_data.character import CharacterListManager
from data_model.constant.file_type import FLAG_STORY_BATTLE
from collections import UserList
from .story_source_part import StoryInfoPartVideo
from .story_source_part import StoryInfoPartSource


class StoryInfoPartSegment(IToJson):
Expand All @@ -22,22 +22,6 @@ def __init__(self, data: dict):
self.character.load(self.data["character"])
self.background.load(self.data["background"])

# self.extra_register()

def extra_register(self):
for char in self.character.character:
for track in self.track.track:
char.register(track, False)
track.register(char, False)

for background in self.background.background:
for track in self.track.track:
track.register(background, False)
background.register(track, False)
for character in self.character.character:
character.register(background, False)
background.register(character, False)

def to_json(self):
return {
"desc": self.desc.to_json(),
Expand Down Expand Up @@ -78,8 +62,7 @@ def __init__(self, data: dict, story_obj):
else:
self.segments.load(data["data"])

# TODO: StoryInfoPartSource
# self.video = StoryInfoPartVideo(data.get("video", {}), story_obj)
self.source = StoryInfoPartSource(data.get("source", {}), story_obj.source)

self._story_obj = story_obj

Expand All @@ -95,12 +78,14 @@ def __init__(self, data: dict, story_obj):

def to_json_basic(self):
d = {"name": self.name.to_json_basic(),
"data": [{"desc": i.desc.to_json_basic()} for i in self.segments]}
"data": [{"desc": i.desc.to_json_basic()} for i in self.segments],
"source": self.source.to_json_basic()}
return d

def to_json(self):
d = {"name": self.name.to_json(),
"data": self.segments.to_json()}
"data": self.segments.to_json(),
"source": self.source.to_json()}

if "is_battle" in self.data.keys():
d["is_battle"] = self.is_battle
Expand Down
102 changes: 20 additions & 82 deletions data_model/actual_data/_story/story_source_all.py
Original file line number Diff line number Diff line change
@@ -1,101 +1,39 @@
from data_model.types.url import UrlModel
from data_model.loader import i18n_translator
from data_model.tool.to_json import IToJson
from data_model.constant.platform import BILIBILI, YOUTUBE
from collections import UserList


class StoryVideoInfo(IToJson):
"""兼容 UrlModel 模型"""
def __init__(self, data):
"""
data = {"url": "https://youtube.com/watch?v=Fsdfjisdjfio", "short_desc": "fjsdifajio"}
"""
self.data = data

self.value = data.get("value", "")
self.platform = self._detect_platform(self.value)
self.short_desc = i18n_translator.query(data.get("short_desc", ""))

@staticmethod
def _detect_platform(url):
if "bilibili.com" in url:
return BILIBILI
elif "youtube.com" in url or "youtu.be" in url:
return YOUTUBE

raise ValueError

def to_json(self):
return {
"value": self.url,
"platform": int(self.platform),
"short_desc": self.short_desc.to_json()
}

def to_json_basic(self):
return self.to_json()


class StoryVideoLanguageInfo(IToJson):
class StoryInfoSourceList(UserList, IToJson):
def __init__(self, data: list):
self.data = [StoryVideoInfo(i) for i in data]

def to_json(self):
return [i.to_json() for i in self.data]

def to_json_basic(self):
return self.to_json()

def __len__(self):
return len(self.data)

def __iter__(self):
return iter(self.data)

def __getitem__(self, key):
return self.data[key]




class StoryInfoVideoType(IToJson):
_component = []

def __init__(self, data: dict):
for i in self._component:
setattr(self, i, StoryVideoLanguageInfo(data.get(i, [])))
super().__init__()
for i in data:
obj = UrlModel()
obj.load(i)
self.append(obj)

def to_json(self):
return [getattr(self, i).to_json() for i in self._component]
return [i.to_json() for i in self]

def to_json_basic(self):
return self.to_json()


class StoryInfoVideoTypeOfficial(StoryInfoVideoType):
_component = ["zh_cn_cn", "zh_tw", "en"]

def __init__(self, data: dict):
super().__init__(data)
return [i.to_json_basic() for i in self]


class StoryInfoVideoTypeUnofficial(StoryInfoVideoType):
_component = ["zh_cn_jp", "en"]
class StoryInfoSource(IToJson):
_component = ["en", "zh_tw", "zh_cn_cn", "zh_cn_jp"]

def __init__(self, data: dict):
super().__init__(data)


class StoryInfoVideo(IToJson):
def __init__(self, data):
self.data = data
self.video_official = StoryInfoVideoTypeOfficial(data.get("official", {}))
self.video_unofficial = StoryInfoVideoTypeUnofficial(data.get("unofficial", {}))
self.en = StoryInfoSourceList(data.get("en", []))
self.zh_tw = StoryInfoSourceList(data.get("zh_tw", []))
self.zh_cn_cn = StoryInfoSourceList(data.get("zh_cn_cn", []))
self.zh_cn_jp = StoryInfoSourceList(data.get("zh_cn_jp", []))

def to_json(self):
return {
"official": self.video_official.to_json(),
"unofficial": self.video_unofficial.to_json()
}
d = {}
for i in self._component:
d[i] = getattr(self, i).to_json()
return d

def to_json_basic(self):
return self.to_json()
88 changes: 82 additions & 6 deletions data_model/actual_data/_story/story_source_part.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,89 @@
from data_model.loader import i18n_translator
from data_model.types.url import UrlModel
from data_model.loader import i18n_translator, constant_manager
from data_model.tool.to_json import IToJson
from data_model.constant.platform import BILIBILI, YOUTUBE
from data_model.constant.platform import BILIBILI, YOUTUBE, BLUEARCHIVE_IO
from .story_source_all import StoryInfoSource, StoryInfoSourceList
from collections import UserList
from urllib.parse import urlparse, parse_qs, urlunparse, urlencode


class StoryInfoPartVideo(IToJson):
def __init__(self, data, story_obj):
self.part_video = data
self.story_video = story_obj.video
class StoryInfoPartSourceEntry(IToJson):
VIDEO_SITES = [BILIBILI, YOUTUBE]

def __init__(self, part_data: dict, story_data: UrlModel):
self.part_data = part_data
self.story_data = story_data

try:
self.base_url = part_data["value"]
self.platform = part_data["platform"]
self.desc = i18n_translator.query(part_data["short_desc"])
except KeyError:
self.base_url = story_data.value
self.platform = story_data.platform
self.desc = story_data.short_desc

self.url = self.get_url()

def get_url(self):
parsed_url = urlparse(self.base_url)
parameters = parse_qs(parsed_url.query)

if str(self.platform) in self.VIDEO_SITES:
parameters["t"] = self.part_data["timestamp"]
elif str(self.platform) == BLUEARCHIVE_IO:
parameters["changeIndex"] = self.part_data["script_index"]
else:
raise ValueError

modified_query = urlencode(parameters, doseq=True)

modified_url = urlunparse((parsed_url.scheme, parsed_url.netloc, parsed_url.path, parsed_url.params,
modified_query, parsed_url.fragment))
return modified_url

def to_json(self):
t = {
"platform": constant_manager.query("platform", self.platform).to_json(),
"value": self.url,
"short_desc": self.desc.to_json()
}
return t

def to_json_basic(self):
return self.to_json()


class StoryInfoPartSourceList(UserList, IToJson):
def __init__(self, part_data: list, story_data: StoryInfoSourceList):
super().__init__()
for i, j in zip(part_data, story_data):
self.append(StoryInfoPartSourceEntry(i, j))

def to_json(self):
return [i.to_json() for i in self]

def to_json_basic(self):
return self.to_json()


class StoryInfoPartSource(IToJson):
_component = ["en", "zh_tw", "zh_cn_cn", "zh_cn_jp"]

def __init__(self, part_data: dict, story_data: StoryInfoSource):
self.part_data = part_data
self.story_data = story_data

self.en = StoryInfoPartSourceList(part_data.get("en", []), story_data.en)
self.zh_tw = StoryInfoPartSourceList(part_data.get("zh_tw", []), story_data.zh_tw)
self.zh_cn_cn = StoryInfoPartSourceList(part_data.get("zh_cn_cn", []), story_data.zh_cn_cn)
self.zh_cn_jp = StoryInfoPartSourceList(part_data.get("zh_cn_jp", []), story_data.zh_cn_jp)

def to_json(self):
d = {}
for i in self._component:
d[i] = getattr(self, i).to_json()
return d

def to_json_basic(self):
return self.to_json()
Loading

0 comments on commit db9374c

Please sign in to comment.