Skip to content

Commit

Permalink
✨ 支持查食物的图片版 (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
KimigaiiWuyi committed Apr 16, 2023
1 parent 74fc7c9 commit 84c70f0
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 10 deletions.
15 changes: 13 additions & 2 deletions GenshinUID/genshinuid_guide/get_abyss_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
from typing import Dict, List, Union

import aiofiles
from gsuid_core.logger import logger

from .abyss_history import history_data
Expand All @@ -10,10 +11,10 @@
get_abyss_review_raw,
)

REVIEW_PATH = Path(__file__).parent / "review.json"
REVIEW_PATH = Path(__file__).parent / 'review.json'


async def generate_data():
async def _generate_data():
raw_data = await get_abyss_review_raw()
result = {}
for version in history_data:
Expand All @@ -30,6 +31,16 @@ async def generate_data():
logger.info('[深渊预览] 数据已刷新!')


async def generate_data():
if REVIEW_PATH.exists():
async with aiofiles.open(REVIEW_PATH, 'r', encoding='UTF-8') as file:
data: Dict = json.loads(await file.read())
if list(data.keys())[0] != list(history_data.keys())[0]:
await _generate_data()
else:
await _generate_data()


async def get_review(version: Union[str, float]) -> Union[List, str]:
if not REVIEW_PATH.exists():
return '请等待数据加载完成...'
Expand Down
7 changes: 6 additions & 1 deletion GenshinUID/genshinuid_wikitext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from gsuid_core.models import Event
from gsuid_core.segment import MessageSegment

from .get_foods_pic import get_foods_wiki_img
from .get_weapons_pic import get_weapons_wiki_img
from ..genshinuid_config.gs_config import gsconfig
from .get_artifacts_pic import get_artifacts_wiki_img
Expand Down Expand Up @@ -32,7 +33,11 @@ async def send_enemies(bot: Bot, ev: Event):

@sv_wiki_text.on_prefix(('食物介绍', '食物资料', '查食物'))
async def send_food(bot: Bot, ev: Event):
await bot.send(await foods_wiki(ev.text))
if gsconfig.get_config('PicWiki').data:
im = await get_foods_wiki_img(ev.text)
else:
im = await foods_wiki(ev.text)
await bot.send(im)


@sv_wiki_text.on_prefix(('圣遗物介绍', '圣遗物资料', '查圣遗物'))
Expand Down
129 changes: 129 additions & 0 deletions GenshinUID/genshinuid_wikitext/get_foods_pic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
from typing import List, Union

import aiofiles
from PIL import Image, ImageDraw

from .path import TEXT_PATH
from ..utils.colors import white_color
from ..utils.error_reply import get_error
from ..gsuid_utils.api.minigg.models import Food
from ..utils.get_assets import get_assets_from_ambr
from ..utils.resource.RESOURCE_PATH import WIKI_FOOD_PATH
from ..utils.image.convert import convert_img, get_str_size
from ..gsuid_utils.api.minigg.request import get_others_info
from ..utils.image.image_tools import (
get_star_png,
get_simple_bg,
get_unknown_png,
)
from ..utils.fonts.genshin_fonts import (
gs_font_18,
gs_font_22,
gs_font_36,
gs_font_44,
)


async def get_foods_wiki_img(name: str) -> Union[str, bytes]:
data = await get_others_info('foods', name)
if isinstance(data, int):
return get_error(data)
elif isinstance(data, List):
return get_error(-400)
else:
food_name = data['name']
path = WIKI_FOOD_PATH / f'{food_name}.jpg'
if path.exists():
async with aiofiles.open(path, 'rb') as f:
return await f.read()
img = await draw_foods_wiki_img(data)
return img


async def draw_foods_wiki_img(data: Food):
gray_color = (230, 230, 230)
img_test = Image.new('RGBA', (1, 1))
img_test_draw = ImageDraw.Draw(img_test)
effect = data['effect']
desc = data['description']

effect = get_str_size(effect, gs_font_22, 440)
desc = get_str_size(desc, gs_font_22, 440)

_, _, _, y1 = img_test_draw.textbbox((0, 0), effect, gs_font_22)
_, _, _, y2 = img_test_draw.textbbox((0, 0), desc, gs_font_22)
w, h = 600, 750 + y1 + y2

star_pic = get_star_png(data['rarity'])
path = TEXT_PATH / f'UI_Buff_Item_{data["foodcategory"]}.png'
if path.exists():
type_pic = Image.open(path)
else:
type_pic = await get_assets_from_ambr(
f'UI_Buff_Item_{data["foodcategory"]}'
)
if type_pic is None:
type_pic = get_unknown_png()
type_pic = type_pic.convert('RGBA').resize((40, 40))

food_pic = await get_assets_from_ambr(data['images']['nameicon'])
if food_pic is None:
food_pic = Image.new('RGBA', (320, 320))
else:
food_pic = food_pic.resize((320, 320))

bg = Image.open(TEXT_PATH / 'wiki_weapon_bg.jpg')
img = await get_simple_bg(w, h, bg)
img_draw = ImageDraw.Draw(img)

img.paste(type_pic, (49, 38), type_pic)
img_draw.text((105, 59), data['name'], white_color, gs_font_44, 'lm')
img.paste(star_pic, (45, 83), star_pic)

btag = Image.open(TEXT_PATH / 'btag.png')
img.paste(btag, (50, 29), btag)

img.paste(food_pic, (140, 119), food_pic)
img_draw.text((45, 465), '食物类型', gray_color, gs_font_18, 'lm')
img_draw.text((45, 500), data['foodfilter'], white_color, gs_font_36, 'lm')

wiki_cost_tag = Image.open(TEXT_PATH / 'cost_tag.png')
img.paste(wiki_cost_tag, (25, 550), wiki_cost_tag)

wiki_desc_tag = Image.open(TEXT_PATH / 'desc_tag.png')
img.paste(wiki_desc_tag, (25, 570 + y1), wiki_desc_tag)

img_draw.text((90, 560), effect, gray_color, gs_font_22)
img_draw.text((90, 580 + y1), desc, gray_color, gs_font_22)

wiki_cost_bg = Image.open(TEXT_PATH / 'wiki_weapon_cost.png')
wiki_cost_draw = ImageDraw.Draw(wiki_cost_bg)

for index, cost in enumerate(data['ingredients']):
cost_name = cost['name']
material = await get_others_info('materials', cost_name)
if isinstance(material, int):
cost_pic = get_unknown_png()
else:
name_icon = material['images']['nameicon']
_cost_pic = await get_assets_from_ambr(name_icon)
if _cost_pic is None:
cost_pic = get_unknown_png()
else:
cost_pic = _cost_pic.resize((64, 64))

t = 100 * index
wiki_cost_bg.paste(cost_pic, (67 + t, 46), cost_pic)
val = str(cost['count'])
wiki_cost_draw.text((99 + t, 123), val, white_color, gs_font_18, 'mm')

img.paste(wiki_cost_bg, (0, 580 + y1 + y2), wiki_cost_bg)

img = img.convert('RGB')
img.save(
WIKI_FOOD_PATH / '{}.jpg'.format(data['name']),
format='JPEG',
quality=100,
subsampling=0,
)
return await convert_img(img)
12 changes: 11 additions & 1 deletion GenshinUID/genshinuid_wikitext/get_weapons_pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ async def get_weapons_wiki_img(name: str) -> Union[str, bytes]:


async def draw_weapons_wiki_img(data: Weapon, stats: WeaponStats):
spec_rank = ['孢囊晶尘', '荧光孢粉', '蕈兽孢子']
gray_color = (214, 214, 214)
img_test = Image.new('RGBA', (1, 1))
img_test_draw = ImageDraw.Draw(img_test)
Expand Down Expand Up @@ -153,8 +154,17 @@ async def draw_weapons_wiki_img(data: Weapon, stats: WeaponStats):
_i = name_temp[k].index(j['name'])
temp[k][_i] += j['count']
break
elif j['name'] in spec_rank:
if spec_rank[0] in temp:
if j['name'] not in name_temp[spec_rank[0]]:
name_temp[spec_rank[0]].append(j['name'])
temp[spec_rank[0]].append(j['count'])
else:
_i = name_temp[spec_rank[0]].index(j['name'])
temp[spec_rank[0]][_i] += j['count']
break
else:
name_temp[j['name']] = [j['count']]
name_temp[j['name']] = [j['name']]
temp[j['name']] = [j['count']]

if data['rarity'] == '5':
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions GenshinUID/utils/image/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ def get_str_size(
result = ''
line = ''
for i in r:
if i == '\n':
result += f'{line}\n'
line = ''
continue

line += i
size, _ = font.getsize(line)
if size >= limit:
Expand Down
8 changes: 8 additions & 0 deletions GenshinUID/utils/resource/RESOURCE_PATH.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
WIKI_REL_PATH = WIKI_PATH / 'artifacts'
CONSTELLATION_PATH = WIKI_PATH / 'constellation'
WIKI_WEAPON_PATH = WIKI_PATH / 'weapon'
WIKI_FOOD_PATH = WIKI_PATH / 'food'
WIKI_TALENT_PATH = WIKI_PATH / 'talent'
WIKI_ENEMY_PATH = WIKI_PATH / 'enemy'
WIKI_CHAR_PATH = WIKI_PATH / 'char'
TEXT2D_PATH = Path(__file__).parent / 'texture2d'

PLAYER_PATH = MAIN_PATH / 'players'
Expand Down Expand Up @@ -56,6 +60,10 @@ def init_dir():
WIKI_REL_PATH,
CONSTELLATION_PATH,
WIKI_WEAPON_PATH,
WIKI_FOOD_PATH,
WIKI_TALENT_PATH,
WIKI_ENEMY_PATH,
WIKI_CHAR_PATH,
]:
i.mkdir(parents=True, exist_ok=True)

Expand Down
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ certifi==2022.12.7 ; python_full_version >= "3.8.1" and python_full_version < "4
charset-normalizer==3.1.0 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
colorama==0.4.6 ; python_full_version >= "3.8.1" and python_version < "4.0" and platform_system == "Windows" or python_full_version >= "3.8.1" and python_version < "4.0" and sys_platform == "win32"
dnspython==2.3.0 ; python_full_version >= "3.8.1" and python_version < "4.0"
email-validator==1.3.1 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
email-validator==2.0.0 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
et-xmlfile==1.1.0 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
fastapi-amis-admin==0.5.4 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
fastapi-user-auth==0.5.0 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
Expand Down

0 comments on commit 84c70f0

Please sign in to comment.