From 0cfe6834fa08fe3c6d3cf0152c581fb61e644b98 Mon Sep 17 00:00:00 2001 From: Nightsky <050644zf@outlook.com> Date: Thu, 13 Feb 2025 18:19:07 +0800 Subject: [PATCH 1/4] have bug in 'decision' and main_14 processing --- codespace.sh | 13 +++- epub_convert.py | 172 ++++++++++++++++++++++++++++++++++++++++++++++++ func.py | 2 +- 3 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 epub_convert.py diff --git a/codespace.sh b/codespace.sh index 237c55b..e484502 100644 --- a/codespace.sh +++ b/codespace.sh @@ -1,15 +1,24 @@ # using light yellow color echo -e "\033[1;33m Initializing... \033[0m" pip install openpyxl > /dev/null 2>&1 -echo -e "\033[1;33m Downloading latest data... \033[0m" -git clone https://github.com/Kengxxiao/ArknightsGameData.git # using light green color echo -e "\033[1;32m Initialization complete. \033[0m" echo -e "\033[1;33m Available servers:\033[0m zh_CN, en_US, ko_KR, ja_JP, zh_TW" echo -e "Enter the server you want to use, press Enter to continue, Blank for zh_CN: " read server +echo -e "\033[1;33m Downloading latest data... \033[0m" if [ -z "$server" ]; then server="zh_CN" + git clone https://github.com/Kengxxiao/ArknightsGameData.git +else + server=$server + # only pull the $server folder from Kengxxiao/ArknightsGameData_YoStar + git clone --depth 1 --filter=blob:none --sparse https://github.com/Kengxxiao/ArknightsGameData_YoStar.git + cd ArknightsGameData_YoStar + git sparse-checkout init --cone + git sparse-checkout set $server + cd .. + mv ArknightsGameData_YoStar/$server ArknightsGameData fi echo -e "Current Server: \033[1;33m $server\033[0m" diff --git a/epub_convert.py b/epub_convert.py new file mode 100644 index 0000000..ad2e41a --- /dev/null +++ b/epub_convert.py @@ -0,0 +1,172 @@ +from ebooklib import epub +from pathlib import Path +from jsonconvert import reader +from typing import Iterable + +import json +import re + +import func + +EPUB_CHAPTER_TEMPLATE = """ +
+ +{name} {content}
" + +EPUB_BRANCH_TEMPLATE = "---End of Options---
'.format(index=index)) + else: + lines.append('---{option}---
'.format(index=index,option=attrs['references'].replace(';', '&'))) + + if prop == 'subtitle': + lines.append('{content}
{content}
'.format(index=index, content=parseContent(attrs.get('text', '')))) + + if prop == 'stickerclear': + lines.append('{content}
'.format(index=index, content=attrs.get('image', ''))) + + + return lines + + + + +DATA_PATH = Path(r"ArknightsStoryJson") + +lang = "zh_CN" +epub_lang = lang.replace("_", "-") +event_id = "main_0" +event: Iterable[func.Story] = func.Event(DATA_PATH, lang, event_id) + +file_name = f"{event.eventid}_{event.name}.epub" + +book = epub.EpubBook() +book.set_identifier(f"{event.eventid} {lang}") +book.set_title(event.name) +book.set_language(epub_lang) +book.add_author("HyperGryph") + +chapters = [] +for idx, story in enumerate(event): + chapter = epub.EpubHtml( + title=f"{story.storyCode} {story.storyName} {story.avgTag}", + file_name=f"{story.storyCode}_{story.storyName}_{story.avgTag}.xhtml", + lang=epub_lang, + ) + with open(story.storyTxt.with_suffix(".json"), encoding="utf-8") as f: + story_json = json.load(f) + + lines = parseHTML(story_json) + chapter.content = EPUB_CHAPTER_TEMPLATE.format(story=story, lines="".join(lines)) + book.add_item(chapter) + chapters.append(chapter) + +book.toc = chapters +book.add_item(epub.EpubNcx()) +book.add_item(epub.EpubNav()) + +style = ''' +@namespace epub "http://www.idpf.org/2007/ops"; + +body { + font-family: Cambria, Liberation Serif, Bitstream Vera Serif, Georgia, Times, Times New Roman, serif; +} + +h2 { + text-align: left; + text-transform: uppercase; + font-weight: 200; +} + +ol { + list-style-type: none; +} + +ol > li:first-child { + margin-top: 0.3em; +} + + +nav[epub|type~='toc'] > ol > li > ol { + list-style-type:square; +} + + +nav[epub|type~='toc'] > ol > li > ol > li { + margin-top: 0.3em; +} + +''' + +# add css file +nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style) +book.add_item(nav_css) + +book.spine = ["nav"] + chapters + +epub.write_epub(file_name, book, {}) diff --git a/func.py b/func.py index 5a6d029..c0177a2 100644 --- a/func.py +++ b/func.py @@ -26,7 +26,7 @@ def __init__(self, data_dir:Path, lang:str, eventid:str): self.storyList = self.review['infoUnlockDatas'] - def __getitem__(self, idx): + def __getitem__(self, idx)->'Story': return Story(self, self.storyList[idx]) def __len__(self): From 89f41a6bf58634c93776dca1de3359d313bca619 Mon Sep 17 00:00:00 2001 From: Nightsky <050644zf@outlook.com> Date: Sat, 15 Feb 2025 17:15:11 +0800 Subject: [PATCH 2/4] have bugs in main_14 --- .gitignore | 2 ++ epub_convert.py | 28 +++++++++++++++------------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 33b2087..47bee10 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ reader/dist/* *.ipynb ArknightsGameData_YoStar/ /ArknightsResource +/htmls +*.epub diff --git a/epub_convert.py b/epub_convert.py index ad2e41a..24dc44f 100644 --- a/epub_convert.py +++ b/epub_convert.py @@ -48,7 +48,7 @@ def parseHTML(story_json): if prop == "name": lines.append( EPUB_NAME_TEMPLATE.format( - index=index, + index=f'line{index}', name=attrs.get('name',''), content=parseContent(attrs.get("content", "")) ) ) @@ -56,7 +56,7 @@ def parseHTML(story_json): if prop == 'multiline': lines.append( EPUB_NAME_TEMPLATE.format( - index=index, + index=f'line{index}', name=attrs.get('name',''), content=parseContent(attrs.get("content", "")) ) ) @@ -65,30 +65,30 @@ def parseHTML(story_json): options = attrs['options'].split(';') values = attrs['values'].split(';') targetLines = line['targetLine'] - options = '' - for i, option in enumerate(options): - options += EPUB_BRANCH_TEMPLATE.format(index=targetLines[f'option{values}'], option=option) + options_html = [] + for i in range(min(len(values),len(options))): + options_html.append(EPUB_BRANCH_TEMPLATE.format(index=targetLines.get(f'option{values[i]}',f'{index+1}'), option=options[i])) lines.append( - '\n
---End of Options---
'.format(index=index)) + lines.append('---End of Options---
'.format(index=index)) else: - lines.append('---{option}---
'.format(index=index,option=attrs['references'].replace(';', '&'))) + lines.append('---{option}---
'.format(index=index,option=attrs['references'].replace(';', '&'))) if prop == 'subtitle': - lines.append('{content}
{content}
{content}
'.format(index=index, content=parseContent(attrs.get('text', '')))) + lines.append('{content}
'.format(index=index, content=parseContent(attrs.get('text', '')))) if prop == 'stickerclear': lines.append('{content}
'.format(index=index, content=attrs.get('image', ''))) + lines.append('{content}
'.format(index=index, content=attrs.get('image', ''))) return lines @@ -100,7 +100,7 @@ def parseHTML(story_json): lang = "zh_CN" epub_lang = lang.replace("_", "-") -event_id = "main_0" +event_id = "act40side" event: Iterable[func.Story] = func.Event(DATA_PATH, lang, event_id) file_name = f"{event.eventid}_{event.name}.epub" @@ -122,7 +122,9 @@ def parseHTML(story_json): story_json = json.load(f) lines = parseHTML(story_json) - chapter.content = EPUB_CHAPTER_TEMPLATE.format(story=story, lines="".join(lines)) + chapter.content = EPUB_CHAPTER_TEMPLATE.format(story=story, lines="\n".join(lines)) + with open(f'htmls/{chapter.file_name}', 'w', encoding='utf-8') as f: + f.write(chapter.content) book.add_item(chapter) chapters.append(chapter) From dedb2203267fedb185bf95e6fb5f40d6959b9a2f Mon Sep 17 00:00:00 2001 From: Nightsky <050644zf@outlook.com> Date: Tue, 18 Feb 2025 09:26:40 +0800 Subject: [PATCH 3/4] Fix html tag, add storyname strip --- epub_convert.py | 6 +++--- func.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/epub_convert.py b/epub_convert.py index 24dc44f..9c0efe9 100644 --- a/epub_convert.py +++ b/epub_convert.py @@ -20,9 +20,9 @@ DOCTOR = "Doctor" -EPUB_NAME_TEMPLATE = "{name} {content}
" +EPUB_NAME_TEMPLATE = '{name} {content}
' -EPUB_BRANCH_TEMPLATE = "Empty Chapter.
"] chapter.content = EPUB_CHAPTER_TEMPLATE.format(story=story, lines="\n".join(lines)) with open(f'htmls/{chapter.file_name}', 'w', encoding='utf-8') as f: f.write(chapter.content)