Skip to content

Commit a9adc3e

Browse files
committed
Update fetcher.py
1 parent 3879323 commit a9adc3e

1 file changed

Lines changed: 53 additions & 56 deletions

File tree

fetcher.py

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,65 +24,62 @@
2424
}
2525
DEPRECATED_ID = set(DEPRECATED_GENSHIN_ID.keys())
2626

27-
def fetch_genshin_impact_update():
28-
target_host = "https://raw.githubusercontent.com/UIGF-Org/GenshinData/main/"
29-
avatar_config_file = "AvatarExcelConfigData.json"
30-
weapon_config_file = "WeaponExcelConfigData.json"
31-
resp = {}
27+
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
3228

33-
avatar_excel_config_data = json.loads(httpx.get(target_host + avatar_config_file).text)
34-
weapon_excel_config_data = json.loads(httpx.get(target_host + weapon_config_file).text)
35-
chs_dict = json.loads(httpx.get(target_host + "TextMap/TextMapCHS.json").text)
36-
cht_dict = json.loads(httpx.get(target_host + "TextMap/TextMapCHT.json").text)
37-
de_dict = json.loads(httpx.get(target_host + "TextMap/TextMapDE.json").text)
38-
en_dict = json.loads(httpx.get(target_host + "TextMap/TextMapEN.json").text)
39-
es_dict = json.loads(httpx.get(target_host + "TextMap/TextMapES.json").text)
40-
fr_dict = json.loads(httpx.get(target_host + "TextMap/TextMapFR.json").text)
41-
id_dict = json.loads(httpx.get(target_host + "TextMap/TextMapID.json").text)
42-
jp_dict = json.loads(httpx.get(target_host + "TextMap/TextMapJP.json").text)
43-
kr_dict = json.loads(httpx.get(target_host + "TextMap/TextMapKR.json").text)
44-
pt_dict = json.loads(httpx.get(target_host + "TextMap/TextMapPT.json").text)
45-
ru_dict = json.loads(httpx.get(target_host + "TextMap/TextMapRU.json").text)
46-
th_dict = json.loads(httpx.get(target_host + "TextMap/TextMapTH.json").text)
47-
vi_dict = json.loads(httpx.get(target_host + "TextMap/TextMapVI.json").text)
48-
dict_list = [chs_dict, cht_dict, de_dict, en_dict, es_dict, fr_dict, id_dict,
49-
jp_dict, kr_dict, pt_dict, ru_dict, th_dict, vi_dict]
50-
item_list = avatar_excel_config_data + weapon_excel_config_data
5129

52-
try:
53-
for file in os.listdir("dict/genshin"):
54-
os.remove("dict/genshin/" + file)
55-
except FileNotFoundError:
56-
pass
57-
58-
# Item list has weapon list and character list
59-
60-
for item in item_list:
61-
this_name_hash_id = str(item["NameTextMapHash"])
62-
this_item_id = int(item["id"])
63-
if this_item_id in DEPRECATED_ID:
64-
logger.warning(f"Item ID {this_item_id} is deprecated, skipping...")
30+
def fetch_genshin_impact_update():
31+
if GITHUB_TOKEN:
32+
headers = {"Authorization": f"Bearer {GITHUB_TOKEN}"}
33+
gh_client = httpx.Client(headers=headers)
34+
else:
35+
raise ValueError("GITHUB_TOKEN is not set in environment variables")
36+
snap_metadata_lang = ["CHS", "CHT", "DE", "EN", "ES", "FR", "ID", "IT", "JP", "KR", "PT", "RU", "TH", "TR", "VI"]
37+
target_host = "https://raw.githubusercontent.com/DGP-Studio/Snap.Metadata/main"
38+
name_to_id = {}
39+
id_to_name = {}
40+
resp = {}
41+
for lang in snap_metadata_lang:
42+
this_lang_name_to_id = {}
43+
this_lang_id_to_name = {}
44+
# Weapon
45+
weapon_config_file = f"{target_host}/Genshin/{lang}/Weapon.json"
46+
weapon_data = gh_client.get(weapon_config_file).json()
47+
for weapon in weapon_data:
48+
this_weapon_name = weapon.get("Name", "")
49+
this_weapon_id = weapon.get("Id", 0)
50+
if this_weapon_name and this_weapon_id:
51+
this_lang_name_to_id[this_weapon_name] = this_weapon_id
52+
this_lang_id_to_name[this_weapon_id] = this_weapon_name
53+
54+
# Character
55+
meta_file = f"{target_host}/Genshin/{lang}/Meta.json"
56+
meta_data = gh_client.get(meta_file).json()
57+
avatar_file_index = [index for index in meta_data.keys() if index.startswith("Avatar/")]
58+
for avtar_indx in avatar_file_index:
59+
avatar_file_url = f"{target_host}/Genshin/{lang}/{avtar_indx}.json"
60+
avatar_data = gh_client.get(avatar_file_url).json()
61+
avatar_name = avatar_data.get("Name", "")
62+
avatar_id = avatar_data.get("Id", 0)
63+
if avatar_name and avatar_id:
64+
this_lang_name_to_id[avatar_name] = avatar_id
65+
this_lang_id_to_name[avatar_id] = avatar_name
66+
67+
name_to_id[lang] = this_lang_name_to_id
68+
id_to_name[lang] = this_lang_id_to_name
69+
70+
all_item_ids = id_to_name["CHS"].keys()
71+
logger.info(f"Fetched total {len(all_item_ids)} unique item IDs from Genshin Impact Snap.Metadata")
72+
73+
for item_id in all_item_ids:
74+
if item_id in DEPRECATED_ID:
75+
logger.warning(f"Item ID {item_id} is deprecated, skipping...")
6576
continue
66-
name_list = [
67-
lang_dict[this_name_hash_id] if this_name_hash_id in lang_dict.keys() else "" for
68-
lang_dict in dict_list
69-
]
70-
lang_dict = {
71-
"chs": name_list[0],
72-
"cht": name_list[1],
73-
"de": name_list[2],
74-
"en": name_list[3],
75-
"es": name_list[4],
76-
"fr": name_list[5],
77-
"id": name_list[6],
78-
"jp": name_list[7],
79-
"kr": name_list[8],
80-
"pt": name_list[9],
81-
"ru": name_list[10],
82-
"th": name_list[11],
83-
"vi": name_list[12]
84-
}
85-
resp[this_item_id] = lang_dict
77+
lang_dict = {}
78+
for lang in snap_metadata_lang:
79+
item_name = id_to_name[lang].get(item_id, "")
80+
lang_code = lang.lower()
81+
lang_dict[lang_code] = item_name
82+
resp[item_id] = lang_dict
8683
return resp
8784

8885

0 commit comments

Comments
 (0)