Skip to content

Commit

Permalink
[feat] 修改装备数据来源为Start2
Browse files Browse the repository at this point in the history
  • Loading branch information
mikualpha committed Nov 27, 2024
1 parent 57cff17 commit 2c22969
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 54 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
run: |
mkdir output
cp improve_data.json output/improve_data.json
cp api_start2.json output/api_start2.json
# echo bot.kcwiki.moe > output/CNAME

- name: Deploy
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea/
__pycache__/

improve_data.json
*.json
22 changes: 12 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests
from bs4 import BeautifulSoup
from utils import ImprovementCache, ItemDataHelper, ShipInfoCache
from utils import ImprovementCache, Start2DataHelper, ShipInfoCache

REQUEST_WEBSITE = 'https://akashi-list.me/'

Expand Down Expand Up @@ -56,7 +56,7 @@ def get_equip_detail(equip_id):
.replace('Wiki WikiEn', '').strip())
# 装备图标ID
try:
equip_icon_id = ItemDataHelper.get_icon_id_by_typename(equip_typename, equip_id)
equip_icon_id = Start2DataHelper.get_slotitem_type_id_by_typename(equip_typename, equip_id)
except KeyError:
raise ValueError(f'TypeName not found: {equip_name} -> {equip_typename}')

Expand Down Expand Up @@ -218,7 +218,7 @@ def get_equip_detail(equip_id):
improve_info['upgrade']['level'] = target_equip[2]
improve_info['upgrade']['id'] = target_equip[0]
improve_info['upgrade']['name'] = target_equip[1]
improve_info['upgrade']['icon'] = ItemDataHelper.get_icon_id(target_equip[0]) # TODO: 暂时没办法拿到
improve_info['upgrade']['icon'] = Start2DataHelper.get_slotitem_type_id(target_equip[0]) # TODO: 暂时没办法拿到

# 秘书舰信息
ship_req_dict = {} # 所有的秘书舰信息 {weekday_hash: req}
Expand All @@ -228,13 +228,13 @@ def get_equip_detail(equip_id):
ship_weekday_index[support_ship_id] = weekday_hash

if weekday_hash in ship_req_dict:
ship_req_dict[weekday_hash]['secretary'].append(ShipInfoCache.get_name(support_ship_id))
ship_req_dict[weekday_hash]['secretary'].append(Start2DataHelper.get_ship_name_by_id(support_ship_id))
ship_req_dict[weekday_hash]['secretaryIds'].append(support_ship_id)
continue

ship_req_dict[weekday_hash] = {
"day": weekday_enable,
"secretary": [ShipInfoCache.get_name(support_ship_id)],
"secretary": [Start2DataHelper.get_ship_name_by_id(support_ship_id)],
"secretaryIds": [support_ship_id],
}

Expand All @@ -251,7 +251,7 @@ def get_equip_detail(equip_id):
"development": [improve_low_dev_cost, improve_low_dev_cost_sure],
"improvement": [improve_low_screw_cost, improve_low_screw_cost_sure],
"item": {
"icon": ItemDataHelper.get_icon_id(improve_low_equip_cost_id),
"icon": Start2DataHelper.get_slotitem_type_id(improve_low_equip_cost_id),
"name": improve_low_equip_cost_name,
"id": improve_low_equip_cost_id,
"count": improve_low_equip_cost_num,
Expand All @@ -261,7 +261,7 @@ def get_equip_detail(equip_id):
"development": [improve_high_dev_cost, improve_high_dev_cost_sure],
"improvement": [improve_high_screw_cost, improve_high_screw_cost_sure],
"item": {
"icon": ItemDataHelper.get_icon_id(improve_high_equip_cost_id),
"icon": Start2DataHelper.get_slotitem_type_id(improve_high_equip_cost_id),
"name": improve_high_equip_cost_name,
"id": improve_high_equip_cost_id,
"count": improve_high_equip_cost_num,
Expand All @@ -280,7 +280,7 @@ def get_equip_detail(equip_id):
"development": [improve_upgrade_dev_cost, improve_upgrade_dev_cost_sure],
"improvement": [improve_upgrade_screw_cost, improve_upgrade_screw_cost_sure],
"item": {
"icon": ItemDataHelper.get_icon_id(improve_upgrade_equip_cost_id),
"icon": Start2DataHelper.get_slotitem_type_id(improve_upgrade_equip_cost_id),
"name": improve_upgrade_equip_cost_name,
"id": improve_upgrade_equip_cost_id,
"count": improve_upgrade_equip_cost_num,
Expand Down Expand Up @@ -343,7 +343,7 @@ def get_weekday_hash(weekday_enable):
def fill_useitem(useitem_info, cost_id, cost_name, cost_num):
for a in range(len(cost_id)):
useitem_info.append({
"icon": ItemDataHelper.get_icon_id(cost_id[a]),
"icon": Start2DataHelper.get_slotitem_type_id(cost_id[a]),
"name": cost_name[a],
"id": cost_id[a],
"count": cost_num[a],
Expand Down Expand Up @@ -412,7 +412,7 @@ def get_improve_cost(equip_id, item_cost_id_list, item_cost_name_list, item_cost
# print('Subitem:', sub_item)
try:
if 'title' in sub_item:
item_cost_id_list.append(ItemDataHelper.get_icon_id_by_typename(sub_item['title']))
item_cost_id_list.append(Start2DataHelper.get_slotitem_type_id_by_typename(sub_item['title']))
item_cost_name_list.append(sub_item['title'])
item_cost_num_list.append(int(sub_item.get_text().replace(u'×', '').strip()))

Expand Down Expand Up @@ -452,3 +452,5 @@ def get_improve_cost(equip_id, item_cost_id_list, item_cost_name_list, item_cost
ImprovementCache.read_file()
soup_obj = get_equip_tree()
get_item_info(soup_obj)

Start2DataHelper.dump_start2_json()
98 changes: 55 additions & 43 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,50 +41,45 @@ def Printf(*args, color_str=''):
print('\033[0m')


class ItemDataHelper:
ITEM_DATA_URL = 'https://bot.kcwiki.moe/json/items.json'

EQUIP_ICON_ID = { # 该列表可能更新不及时,请尽量避免使用icon字段
'小口径主砲': 1,
'中口径主砲': 2,
'大口径主砲': 3,
'艦上戦闘機': 6,
'艦上爆撃機': 7,
'艦上攻撃機': 8,
'艦上偵察機': 9,
'水上偵察機': 10,
'上陸用舟艇': 20,
'対潜哨戒機': 22,
'司令部施設': 34,
'熟練搭乗員': 70,
'新型航空兵装資材': 77,
}

json_obj = json.loads(requests.get(ITEM_DATA_URL).content)
class Start2DataHelper:
START2_DATA_URL = 'https://api.kcwiki.moe/start2'
START2_FILENAME = './api_start2.json'
ICON_ID_INDEX = 2 # 装备类型所在的位置

json_obj = json.loads(requests.get(START2_DATA_URL).content)

@classmethod
def get_icon_id(cls, equip_id: str | int, default_val: str | int | None = 0):
def get_slotitem_type_id(cls, equip_id: str | int, default_val: str | int | None = 0):
if equip_id == 0:
return 0

if type(equip_id) is int:
equip_id = str(equip_id)
if type(equip_id) is str:
equip_id = int(equip_id)

equip_id = equip_id.rjust(3, '0')
target_equip = next((x for x in cls.json_obj['api_mst_slotitem'] if x["api_id"] == equip_id), None)

if equip_id not in cls.json_obj:
if not target_equip:
Printf(f'[Warning] Equip id {equip_id} not in json_obj, length: {len(cls.json_obj)}', color_str='35m')
return default_val

return cls.json_obj[equip_id]['类别'][2]
return target_equip['api_type'][cls.ICON_ID_INDEX]

@classmethod
def get_icon_id_by_typename(cls, typename: str, equip_id: str = '', default_val=0):
if typename in cls.EQUIP_ICON_ID:
return cls.EQUIP_ICON_ID[typename]
def get_slotitem_type_id_by_typename(cls, typename: str, equip_id: str = ''):
for equip_type in cls.json_obj.get('api_mst_slotitem_equiptype', []):
if equip_type['api_name'] == typename:
return equip_type['api_id']

for useitem in cls.json_obj.get('api_mst_useitem', []):
if useitem['api_name'] == typename:
return useitem['api_id']

# for slotitem in cls.json_obj.get('api_mst_slotitem', []):
# if slotitem['api_name'] == typename:
# return slotitem['api_id']

if equip_id:
icon_id = cls.get_icon_id(equip_id, None)
icon_id = cls.get_slotitem_type_id(equip_id, None)
if icon_id is not None:
return icon_id

Expand All @@ -93,30 +88,47 @@ def get_icon_id_by_typename(cls, typename: str, equip_id: str = '', default_val=
# traceback.print_stack()
# return default_val

@classmethod
def get_ship_id_by_name(cls, ship_name: str) -> int:
for ship_info in cls.json_obj['api_mst_ship']:
if ship_info['api_name'] == ship_name:
return ship_info['api_id']

return -1

@classmethod
def get_ship_name_by_id(cls, ship_id: int) -> str:
for ship_info in cls.json_obj['api_mst_ship']:
if ship_info['api_id'] == ship_id:
return ship_info['api_name']

return ''

@classmethod
def dump_start2_json(cls):
with open(cls.START2_FILENAME, 'w', encoding='utf-8') as f:
f.write(json.dumps(cls.json_obj, ensure_ascii=False, separators=(',', ':')))


# 虽然有Start2了但是仍旧用这个存取name_cache以保证一些奇怪的名字可以hold住(比如"日本艦")
class ShipInfoCache:
name_cache = {} # {ship_name: ship_id}
id_cache = {} # {ship_id: ship_name}

@classmethod
def get_id(cls, ship_name: str, default_val=None):
if ship_name not in cls.name_cache:
return default_val
return cls.name_cache[ship_name]
if ship_name in cls.name_cache:
return cls.name_cache[ship_name]

if ship_start2_id := Start2DataHelper.get_ship_id_by_name(ship_name):
return ship_start2_id

@classmethod
def get_name(cls, ship_id, default_val=''):
if ship_id == 0:
return '-'

if ship_id not in cls.id_cache:
return default_val
return cls.id_cache[ship_id]
Printf(f'[Warning] Ship name {ship_name} not in name_cache, length: {len(cls.name_cache)}', color_str='35m')
return default_val

@classmethod
def set_id(cls, ship_name: str, ship_id: int):
# Printf(f'Debug: Set {ship_name} ==> {ship_id}', color_str='90m')
cls.name_cache[ship_name] = ship_id
cls.id_cache[ship_id] = ship_name

@classmethod
def set_id_by_img_url(cls, ship_name: str, url: str):
Expand Down

0 comments on commit 2c22969

Please sign in to comment.