diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index be22615..db100e4 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -26,7 +26,8 @@ jobs: run: | export FILE_PATH="${{ vars.FILE_PATH }}" python main.py - cp calendar.json Resources/calendar.json + python achievements.py + cp src/calendar.json Resources/calendar.json - name: Commit changes uses: EndBug/add-and-commit@v9 diff --git a/achievements.py b/achievements.py new file mode 100644 index 0000000..b162226 --- /dev/null +++ b/achievements.py @@ -0,0 +1,94 @@ +""" +Achievements data extractor + +src: +achievements.xlsx https://docs.qq.com/sheet/DS01hbnZwZm5KVnBB?tab=BB08J2 +achievements.py https://github.com/KimigaiiWuyi/GenshinUID/blob/main/GenshinUID/tools/get_achievement_json.py +""" + +import json +import warnings +from pathlib import Path + +from openpyxl import Workbook, load_workbook +from openpyxl.worksheet.worksheet import Worksheet +from pydantic import BaseModel + +warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl') +import_path = "src/achievements.xlsx" +export_path = Path("Resources") + +wb: Workbook = load_workbook(import_path) +ws_daily: Worksheet = wb['成就相关每日委托'] +ws_all: Worksheet = wb['正式服成就汇总'] + +result_daily = [] +result_all = [] + + +class BaseAchievement(BaseModel): + name: str + desc: str + guide: str + link: str + + +class Achievement(BaseAchievement): + book: str + + +class TaskAchievement(BaseAchievement): + task: str + + +def load_daily_achievements(): + is_first = False + for row in range(3, 100): + ach = TaskAchievement( + task=ws_daily.cell(row, 3).value or "", + name=ws_daily.cell(row, 4).value or "", + desc=ws_daily.cell(row, 5).value or "", + guide=ws_daily.cell(row, 6).value or "", + link=ws_daily.cell(row, 6).hyperlink.target if ws_daily.cell(row, 6).hyperlink else '', + ) + if not ach.task: + if is_first: + break + is_first = True + continue + else: + is_first = False + task_list = ach.task.split('\n') + for t in task_list: + if t.startswith('('): + continue + ach.task = t + result_daily.append(ach.dict()) + + +def load_all_achievements(book: Worksheet, loop: int, bn: int, an: int, dn: int, gn: int): + for row in range(3, loop): + ach = Achievement( + book=book.cell(row, bn).value or "", + name=book.cell(row, an).value or "", + desc=book.cell(row, dn).value or "", + guide=book.cell(row, gn).value or "", + link=book.cell(row, gn).hyperlink.target if book.cell(row, gn).hyperlink else '', + ) + if not ach.book: + break + result_all.append(ach.dict()) + + +def save_achievements(): + export_path.mkdir(parents=True, exist_ok=True) + with open(export_path / 'achievements_daily.json', 'w', encoding='utf-8') as f: + json.dump(result_daily, f, indent=4, ensure_ascii=False) + with open(export_path / 'achievements_all.json', 'w', encoding='utf-8') as f: + json.dump(result_all, f, indent=4, ensure_ascii=False) + + +if __name__ == '__main__': + load_all_achievements(ws_all, 1000, 5, 6, 7, 11) + load_daily_achievements() + save_achievements() diff --git a/main.py b/main.py index 2cc0352..a783d61 100644 --- a/main.py +++ b/main.py @@ -36,7 +36,7 @@ async def save_file(file_name: str) -> None: async def save_file_list() -> None: - async with aiofiles.open("files.txt", "r", encoding="utf-8") as f: + async with aiofiles.open("src/files.txt", "r", encoding="utf-8") as f: file_list = (await f.read()).splitlines() tasks = [save_file(file) for file in file_list] diff --git a/requirements.txt b/requirements.txt index f6cf568..6435a7c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ httpx==0.23.3 python-dotenv==0.21.1 aiofiles==23.1.0 +openpyxl==3.1.1 +pydantic==1.10.5 diff --git a/src/achievements.xlsx b/src/achievements.xlsx new file mode 100644 index 0000000..5923cc5 Binary files /dev/null and b/src/achievements.xlsx differ diff --git a/calendar.json b/src/calendar.json similarity index 100% rename from calendar.json rename to src/calendar.json diff --git a/files.txt b/src/files.txt similarity index 100% rename from files.txt rename to src/files.txt