Last active
May 22, 2024 08:08
-
-
Save swablueme/4f2cb4a1fa6af3d5eb8b20b73695ac3d to your computer and use it in GitHub Desktop.
Maa Assistant depot export parse json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import csv | |
import json | |
import requests | |
from os import path | |
from typing import Iterator | |
sys.stdin.reconfigure(encoding='utf-8') | |
sys.stdout.reconfigure(encoding='utf-8') | |
translated_names = [] | |
OPERATORS_MD_URL = "https://raw.githubusercontent.com/MaaAssistantArknights/MaaAssistantArknights/dev/docs/glossary/operators.md" | |
OPERATORS_MD_FILENAME = path.basename(OPERATORS_MD_URL) | |
CURRENTLY_OWNED_OPERATORS = "currentlyowned.json" | |
KROOSTER_FILE = "krooster.json" | |
class CurrentlyOwnedOperators: | |
def __init__(self): | |
operators_md = Operators() | |
krooster = json.loads( | |
open(KROOSTER_FILE, "r", encoding="utf-8").read()) | |
with open(CURRENTLY_OWNED_OPERATORS, "r", encoding="utf-8") as owned_characters: | |
operators_owned = json.loads(owned_characters.read()) | |
operators_owned = sorted(operators_owned, key=sort_operators) | |
for operator in operators_owned: | |
operator_id = operator["id"] | |
english_name = operators_md.lookup_eng_operator(operator["id"]) | |
elite_level = operator["elite"] | |
operator_level = operator["level"] | |
potential_level = operator["potential"] | |
print(operators_md.lookup_eng_operator(operator["id"]), | |
"E" + str(operator["elite"]), "level", operator["level"], "pot", operator["potential"]) | |
krooster[operator_id]["level"] = operator_level | |
krooster[operator_id]["potential"] = potential_level | |
krooster[operator_id]["promotion"] = elite_level | |
krooster[operator_id]["owned"] = True | |
with open("saved_krooster.json", "w+", encoding="utf-8") as f: | |
f.write(json.dumps(krooster)) | |
class Operators: | |
SEPERATOR = "|" | |
LANGUAGE_EN_KEY = "English" | |
LANGUAGE_CN_KEY = "中文" | |
OPERATOR_ID_KEY = "ID" | |
def __init__(self): | |
self.operators_str = Operators.download_operators_md() | |
self.data = self.parse_md() | |
@staticmethod | |
def download_operators_md() -> str: | |
operators_md_file = "" | |
response = requests.get(OPERATORS_MD_URL) | |
if response.status_code == 200: | |
operators_md_file = response.text | |
else: | |
raise Exception("Unable to get response with Code : %d " % | |
(response.status_code)) | |
return operators_md_file | |
def parse_md(self) -> Iterator: | |
csv_reader = csv.DictReader( | |
self.operators_str.split("\n"), delimiter='|') | |
return [line for line in csv_reader] | |
def lookup_eng_operator(self, operator_id) -> str: | |
for op in self.data: | |
if op[Operators.OPERATOR_ID_KEY].lower() == operator_id.lower(): | |
return op[Operators.LANGUAGE_EN_KEY] | |
def lookup_eng_operator_from_cn(self, operator_en_name) -> str: | |
for op in self.data: | |
if op[Operators.LANGUAGE_CN_KEY] == operator_en_name.lower(): | |
return op[Operators.LANGUAGE_EN_KEY] | |
print("No operator found for:", operator_en_name) | |
def sort_operators(k): | |
return (-k['elite'], -k['level']) | |
if __name__ == '__main__': | |
CurrentlyOwnedOperators() | |
# ops = Operators() | |
# for name in "煌/能天使/风笛/伊芙利特/星熊/闪灵/黑/塞雷娅/安洁莉娜/推进之王/陈/阿".split("/"): | |
# print(ops.lookup_eng_operator_from_cn(name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment