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/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..a132bba --- /dev/null +++ b/epub_convert.py @@ -0,0 +1,183 @@ +from ebooklib import epub +from pathlib import Path +from jsonconvert import reader +from typing import Iterable + +import json +import re + +import func + +import argparse + +EPUB_CHAPTER_TEMPLATE = """ +
+ +{name} {content}
' + +EPUB_BRANCH_TEMPLATE = '\n
---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 + +parser = argparse.ArgumentParser() +parser.add_argument("event_id", help="Event ID") +parser.add_argument("-l", "--lang", help="Language", default="zh_CN") +args = parser.parse_args() + + + +DATA_PATH = Path(r"ArknightsStoryJson") + +lang = args.lang +epub_lang = lang.replace("_", "-") +event_id = args.event_id +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): + print(f"Processing {story.storyCode} {story.storyName} {story.avgTag}") + 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) + if len(lines) == 0: + lines = ["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) + 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..844dcb4 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): @@ -45,7 +45,7 @@ def __init__(self, event:Event, storyData:dict): self.storyData = storyData self.storyCode = storyData['storyCode'] self.avgTag = storyData['avgTag'] if storyData['avgTag'] else '' - self.storyName = storyData['storyName'] + self.storyName = storyData['storyName'].strip() self.storyInfo = self.root_dir/'gamedata/story/[uc]{}.txt'.format(storyData['storyInfo']) if storyData['storyInfo'] else None self.storyTxt = self.root_dir/'gamedata/story/{}.txt'.format(storyData['storyTxt']) self.f = storyData['storyTxt']