修改srmrbug (#26)

* 修改srmrbug

* 修改srmrbug

* 修改srmrbug

* 修改月历上月数据画图

* 🚨 `pre-commit-ci`修复格式错误

* 修改bug

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
季落 2023-05-16 15:55:54 +08:00 committed by GitHub
parent bec0f16fdd
commit 8fd1216652
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 298 additions and 273 deletions

View File

@ -1,268 +1,276 @@
import json import json
from pathlib import Path from pathlib import Path
from typing import Union from typing import Union
from datetime import datetime from datetime import datetime
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
from gsuid_core.logger import logger from gsuid_core.logger import logger
from ..utils.mys_api import mys_api from ..utils.mys_api import mys_api
from ..utils.error_reply import get_error from ..utils.error_reply import get_error
from ..utils.image.convert import convert_img from ..utils.image.convert import convert_img
from ..utils.resource.RESOURCE_PATH import PLAYER_PATH from ..utils.resource.RESOURCE_PATH import PLAYER_PATH
from ..utils.fonts.starrail_fonts import sr_font_20, sr_font_28, sr_font_34 from ..utils.fonts.starrail_fonts import sr_font_20, sr_font_28, sr_font_34
TEXT_PATH = Path(__file__).parent / 'texture2d' TEXT_PATH = Path(__file__).parent / 'texture2d'
monthly_bg = Image.open(TEXT_PATH / 'monthly_bg.png') monthly_bg = Image.open(TEXT_PATH / 'monthly_bg.png')
avatar_default = Image.open(TEXT_PATH / '200101.png') avatar_default = Image.open(TEXT_PATH / '200101.png')
first_color = (29, 29, 29) first_color = (29, 29, 29)
second_color = (67, 61, 56) second_color = (67, 61, 56)
second_color2 = (98, 98, 98) second_color2 = (98, 98, 98)
black_color = (54, 54, 54) black_color = (54, 54, 54)
white_color = (213, 213, 213) white_color = (213, 213, 213)
COLOR_MAP = { COLOR_MAP = {
'每日活跃': (248, 227, 157), '每日活跃': (248, 227, 157),
'活动奖励': (99, 231, 176), '活动奖励': (99, 231, 176),
'冒险奖励': (114, 205, 251), '冒险奖励': (114, 205, 251),
'模拟宇宙奖励': (160, 149, 248), '模拟宇宙奖励': (160, 149, 248),
'忘却之庭奖励': (221, 119, 250), '忘却之庭奖励': (221, 119, 250),
'邮件奖励': (244, 110, 104), '邮件奖励': (244, 110, 104),
'其他': (255, 242, 200), '其他': (255, 242, 200),
'Daily Activity': (248, 227, 157), 'Daily Activity': (248, 227, 157),
'Events': (99, 231, 176), 'Events': (99, 231, 176),
'Adventure': (114, 205, 251), 'Adventure': (114, 205, 251),
'moni': (160, 149, 248), 'moni': (160, 149, 248),
'Spiral Abyss': (221, 119, 250), 'Spiral Abyss': (221, 119, 250),
'Quests': (244, 110, 104), 'Quests': (244, 110, 104),
'Other': (255, 242, 200), 'Other': (255, 242, 200),
} }
async def draw_note_img(sr_uid: str) -> Union[bytes, str]: async def draw_note_img(sr_uid: str) -> Union[bytes, str]:
path = PLAYER_PATH / str(sr_uid) path = PLAYER_PATH / str(sr_uid)
if not path.exists(): if not path.exists():
path.mkdir(parents=True, exist_ok=True) path.mkdir(parents=True, exist_ok=True)
# 获取当前时间 # 获取当前时间
now = datetime.now() now = datetime.now()
current_year_mon = now.strftime('%Y-%m') current_year_mon = now.strftime('%Y-%m')
add_month = ''
# 获取数据 if int(now.month) < 10:
data = await mys_api.get_award(sr_uid) add_month = '0'
if isinstance(data, int): now_month = str(now.year) + str(add_month) + str(now.month)
return get_error(data) print(now_month)
# 获取数据
# 保存数据 data = await mys_api.get_award(sr_uid, now_month)
with open( if isinstance(data, int):
path / f'monthly_{current_year_mon}.json', 'w', encoding='utf-8' return get_error(data)
) as f:
save_data = json.dumps( # 保存数据
{ with open(
'data_time': now.strftime('%Y-%m-%d %H:%M:%S'), path / f'monthly_{current_year_mon}.json', 'w', encoding='utf-8'
'data': data, ) as f:
}, save_data = json.dumps(
ensure_ascii=False, {
) 'data_time': now.strftime('%Y-%m-%d %H:%M:%S'),
f.write(save_data) 'data': data,
},
# 获取上月数据 ensure_ascii=False,
last_month = now.month - 1 )
last_year = now.year f.write(save_data)
if last_month == 0:
last_month = 12 # 获取上月数据
last_year -= 1 last_month = now.month - 1
last_year_mon = f'{last_year}-{last_month:02d}' last_year = now.year
last_monthly_path = path / f'monthly_{last_year_mon}.json' if last_month == 0:
if last_monthly_path.exists(): last_month = 12
with open(last_monthly_path, 'r', encoding='utf-8') as f: last_year -= 1
last_monthly_data = json.load(f) last_year_mon = f'{last_year}-{last_month:02d}'
else: last_monthly_path = path / f'monthly_{last_year_mon}.json'
last_monthly_data = None if last_monthly_path.exists():
with open(last_monthly_path, 'r', encoding='utf-8') as f:
# nickname and level last_monthly_data = json.load(f)
role_basic_info = await mys_api.get_role_basic_info(sr_uid) last_monthly_data = last_monthly_data['data']
if isinstance(role_basic_info, int): else:
return get_error(role_basic_info) add_month = ''
nickname = role_basic_info['nickname'] if int(last_month) < 10:
add_month = '0'
day_hcoin = data['day_data']['current_hcoin'] find_last_month = str(last_year) + str(add_month) + str(last_month)
day_rails_pass = data['day_data']['current_rails_pass'] print(find_last_month)
lastday_hcoin = 0 last_monthly_data = await mys_api.get_award(sr_uid, find_last_month)
lastday_rails_pass = 0
if int(sr_uid[0]) < 6: # nickname and level
lastday_hcoin = data['day_data']['last_hcoin'] role_basic_info = await mys_api.get_role_basic_info(sr_uid)
lastday_rails_pass = data['day_data']['last_rails_pass'] if isinstance(role_basic_info, int):
month_hcoin = data['month_data']['current_hcoin'] return get_error(role_basic_info)
month_rails_pass = data['month_data']['current_rails_pass'] nickname = role_basic_info['nickname']
lastmonth_hcoin = data['month_data']['last_hcoin']
lastmonth_rails_pass = data['month_data']['last_rails_pass'] day_hcoin = data['day_data']['current_hcoin']
day_rails_pass = data['day_data']['current_rails_pass']
day_hcoin_str = await int_carry(day_hcoin) lastday_hcoin = 0
day_rails_pass_str = await int_carry(day_rails_pass) lastday_rails_pass = 0
month_hcoin_str = await int_carry(month_hcoin) if int(sr_uid[0]) < 6:
month_rails_pass_str = await int_carry(month_rails_pass) lastday_hcoin = data['day_data']['last_hcoin']
lastday_hcoin_str = await int_carry(lastday_hcoin) lastday_rails_pass = data['day_data']['last_rails_pass']
lastday_rails_pass_str = await int_carry(lastday_rails_pass) month_hcoin = data['month_data']['current_hcoin']
lastmonth_hcoin_str = await int_carry(lastmonth_hcoin) month_rails_pass = data['month_data']['current_rails_pass']
lastmonth_rails_pass_str = await int_carry(lastmonth_rails_pass) lastmonth_hcoin = data['month_data']['last_hcoin']
lastmonth_rails_pass = data['month_data']['last_rails_pass']
img = monthly_bg.copy()
avatar_img = avatar_default.copy() day_hcoin_str = await int_carry(day_hcoin)
char_pic = avatar_img.convert('RGBA').resize( day_rails_pass_str = await int_carry(day_rails_pass)
(125, 125), Image.Resampling.LANCZOS # type: ignore month_hcoin_str = await int_carry(month_hcoin)
) month_rails_pass_str = await int_carry(month_rails_pass)
img.paste(char_pic, (115, 133), char_pic) lastday_hcoin_str = await int_carry(lastday_hcoin)
img_draw = ImageDraw.Draw(img) lastday_rails_pass_str = await int_carry(lastday_rails_pass)
lastmonth_hcoin_str = await int_carry(lastmonth_hcoin)
# 写Nickname lastmonth_rails_pass_str = await int_carry(lastmonth_rails_pass)
img_draw.text(
(310, 184), nickname, font=sr_font_34, fill=first_color, anchor='lm' img = monthly_bg.copy()
) avatar_img = avatar_default.copy()
char_pic = avatar_img.convert('RGBA').resize(
# 写UID (125, 125), Image.Resampling.LANCZOS # type: ignore
img_draw.text( )
(267, 219), img.paste(char_pic, (115, 133), char_pic)
f'UID {sr_uid}', img_draw = ImageDraw.Draw(img)
font=sr_font_20,
fill=second_color2, # 写Nickname
anchor='lm', img_draw.text(
) (310, 184), nickname, font=sr_font_34, fill=first_color, anchor='lm'
)
# 写本日星琼
img_draw.text( # 写UID
(283, 326), img_draw.text(
day_hcoin_str, (267, 219),
font=sr_font_28, f'UID {sr_uid}',
fill=white_color, font=sr_font_20,
anchor='lm', fill=second_color2,
) anchor='lm',
)
# 写本月星琼
img_draw.text( # 写本日星琼
(513, 326), img_draw.text(
month_hcoin_str, (283, 326),
font=sr_font_28, day_hcoin_str,
fill=white_color, font=sr_font_28,
anchor='lm', fill=white_color,
) anchor='lm',
)
# 写昨日星琼
img_draw.text( # 写本月星琼
(283, 366), img_draw.text(
lastday_hcoin_str, (513, 326),
font=sr_font_28, month_hcoin_str,
fill=black_color, font=sr_font_28,
anchor='lm', fill=white_color,
) anchor='lm',
)
# 写上月星琼
img_draw.text( # 写昨日星琼
(513, 366), img_draw.text(
lastmonth_hcoin_str, (283, 366),
font=sr_font_28, lastday_hcoin_str,
fill=black_color, font=sr_font_28,
anchor='lm', fill=black_color,
) anchor='lm',
)
# 写本日铁票
img_draw.text( # 写上月星琼
(283, 431), img_draw.text(
day_rails_pass_str, (513, 366),
font=sr_font_28, lastmonth_hcoin_str,
fill=white_color, font=sr_font_28,
anchor='lm', fill=black_color,
) anchor='lm',
)
# 写本月铁票
img_draw.text( # 写本日铁票
(513, 431), img_draw.text(
month_rails_pass_str, (283, 431),
font=sr_font_28, day_rails_pass_str,
fill=white_color, font=sr_font_28,
anchor='lm', fill=white_color,
) anchor='lm',
)
# 写昨日铁票
img_draw.text( # 写本月铁票
(283, 473), img_draw.text(
lastday_rails_pass_str, (513, 431),
font=sr_font_28, month_rails_pass_str,
fill=black_color, font=sr_font_28,
anchor='lm', fill=white_color,
) anchor='lm',
)
# 写上月铁票
img_draw.text( # 写昨日铁票
(513, 473), img_draw.text(
lastmonth_rails_pass_str, (283, 473),
font=sr_font_28, lastday_rails_pass_str,
fill=black_color, font=sr_font_28,
anchor='lm', fill=black_color,
) anchor='lm',
xy = ((0, 0), (2100, 2100)) )
temp = -90
if not data['month_data']['group_by']: # 写上月铁票
pie_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0)) img_draw.text(
pie_image_draw = ImageDraw.Draw(pie_image) (513, 473),
pie_image_draw.ellipse(xy, fill=(128, 128, 128)) lastmonth_rails_pass_str,
else: font=sr_font_28,
pie_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0)) fill=black_color,
pie_image_draw = ImageDraw.Draw(pie_image) anchor='lm',
for index, i in enumerate(data['month_data']['group_by']): )
pie_image_draw.pieslice( xy = ((0, 0), (2100, 2100))
xy, temp = -90
temp, if not data['month_data']['group_by']:
temp + (i['percent'] / 100) * 360, pie_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0))
COLOR_MAP[i['action_name']], pie_image_draw = ImageDraw.Draw(pie_image)
) pie_image_draw.ellipse(xy, fill=(128, 128, 128))
temp = temp + (i['percent'] / 100) * 360 else:
# 绘制蒙版圆形 pie_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0))
new_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0)) pie_image_draw = ImageDraw.Draw(pie_image)
pie_image_draw.ellipse((150, 150, 1950, 1950), fill=(255, 255, 255, 0)) for index, i in enumerate(data['month_data']['group_by']):
pie_image_draw.pieslice(
position = (1050, 1050) xy,
pie_image.paste(new_image, position, mask=new_image) temp,
result_pie = pie_image.resize((210, 210)) temp + (i['percent'] / 100) * 360,
img.paste(result_pie, (138, 618), result_pie) COLOR_MAP[i['action_name']],
)
if last_monthly_data: temp = temp + (i['percent'] / 100) * 360
pie_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0)) # 绘制蒙版圆形
pie_image_draw = ImageDraw.Draw(pie_image) new_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0))
for index, i in enumerate( pie_image_draw.ellipse((150, 150, 1950, 1950), fill=(255, 255, 255, 0))
last_monthly_data['data']['month_data']['group_by']
): position = (1050, 1050)
pie_image_draw.pieslice( pie_image.paste(new_image, position, mask=new_image)
xy, result_pie = pie_image.resize((210, 210))
temp, img.paste(result_pie, (138, 618), result_pie)
temp + (i['percent'] / 100) * 360,
COLOR_MAP[i['action_name']], if last_monthly_data:
) pie_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0))
temp = temp + (i['percent'] / 100) * 360 pie_image_draw = ImageDraw.Draw(pie_image)
else: for index, i in enumerate(last_monthly_data['month_data']['group_by']):
pie_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0)) pie_image_draw.pieslice(
pie_image_draw = ImageDraw.Draw(pie_image) xy,
pie_image_draw.ellipse(xy, fill=(128, 128, 128)) temp,
temp + (i['percent'] / 100) * 360,
# 绘制蒙版圆形 COLOR_MAP[i['action_name']],
new_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0)) )
pie_image_draw.ellipse((150, 150, 1950, 1950), fill=(255, 255, 255, 0)) temp = temp + (i['percent'] / 100) * 360
else:
position = (1050, 1050) pie_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0))
pie_image.paste(new_image, position, mask=new_image) pie_image_draw = ImageDraw.Draw(pie_image)
result_pie = pie_image.resize((210, 210)) pie_image_draw.ellipse(xy, fill=(128, 128, 128))
img.paste(result_pie, (138, 618 + 350), result_pie)
# 绘制蒙版圆形
img = await convert_img(img) new_image = Image.new("RGBA", (2100, 2100), color=(255, 255, 255, 0))
logger.info('[开拓月历] 图片绘制完成!等待发送...') pie_image_draw.ellipse((150, 150, 1950, 1950), fill=(255, 255, 255, 0))
return img
position = (1050, 1050)
pie_image.paste(new_image, position, mask=new_image)
async def int_carry(i: int) -> str: result_pie = pie_image.resize((210, 210))
if i >= 100000: img.paste(result_pie, (138, 618 + 350), result_pie)
i_str = '{:.1f}W'.format(i / 10000)
else: img = await convert_img(img)
i_str = str(i) logger.info('[开拓月历] 图片绘制完成!等待发送...')
return i_str return img
async def int_carry(i: int) -> str:
if i >= 100000:
i_str = '{:.1f}W'.format(i / 10000)
else:
i_str = str(i)
return i_str

View File

@ -9,7 +9,6 @@ from gsuid_core.logger import logger
from ..utils.api import get_sqla from ..utils.api import get_sqla
from ..utils.mys_api import mys_api from ..utils.mys_api import mys_api
from ..utils.error_reply import get_error
from ..utils.image.convert import convert_img from ..utils.image.convert import convert_img
from ..sruid_utils.api.mys.models import Expedition from ..sruid_utils.api.mys.models import Expedition
from ..utils.fonts.starrail_fonts import ( from ..utils.fonts.starrail_fonts import (
@ -24,6 +23,7 @@ TEXT_PATH = Path(__file__).parent / 'texture2D'
note_bg = Image.open(TEXT_PATH / 'note_bg.png') note_bg = Image.open(TEXT_PATH / 'note_bg.png')
note_travel_bg = Image.open(TEXT_PATH / 'note_travel_bg.png') note_travel_bg = Image.open(TEXT_PATH / 'note_travel_bg.png')
warn_pic = Image.open(TEXT_PATH / 'warn.png')
based_w = 700 based_w = 700
based_h = 1200 based_h = 1200
@ -148,7 +148,24 @@ async def draw_resin_img(sr_uid: str) -> Image.Image:
img = note_bg.copy() img = note_bg.copy()
if isinstance(daily_data, int): if isinstance(daily_data, int):
return get_error(daily_data) img_draw = ImageDraw.Draw(img)
img.paste(warn_pic, (0, 0), warn_pic)
# 写UID
img_draw.text(
(250, 553),
f'UID{sr_uid}',
font=sr_font_26,
fill=first_color,
anchor='mm',
)
img_draw.text(
(250, 518),
f'错误码 {daily_data}',
font=sr_font_26,
fill=red_color,
anchor='mm',
)
return img
# nickname and level # nickname and level
role_basic_info = await mys_api.get_role_basic_info(sr_uid) role_basic_info = await mys_api.get_role_basic_info(sr_uid)

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

View File

@ -206,7 +206,7 @@ class MysApi(_MysApi):
data = cast(MysSign, data['data']) data = cast(MysSign, data['data'])
return data return data
async def get_award(self, sr_uid) -> Union[MonthlyAward, int]: async def get_award(self, sr_uid, month) -> Union[MonthlyAward, int]:
server_id = RECOGNIZE_SERVER.get(str(sr_uid)[0]) server_id = RECOGNIZE_SERVER.get(str(sr_uid)[0])
ck = await self.get_ck(sr_uid, 'OWNER') ck = await self.get_ck(sr_uid, 'OWNER')
if ck is None: if ck is None:
@ -220,7 +220,7 @@ class MysApi(_MysApi):
url=_API['STAR_RAIL_MONTH_INFO_URL'], url=_API['STAR_RAIL_MONTH_INFO_URL'],
method='GET', method='GET',
header=HEADER, header=HEADER,
params={'uid': sr_uid, 'region': server_id, 'month': ''}, params={'uid': sr_uid, 'region': server_id, 'month': month},
) )
else: else:
HEADER = copy.deepcopy(self._HEADER_OS) HEADER = copy.deepcopy(self._HEADER_OS)
@ -231,7 +231,7 @@ class MysApi(_MysApi):
url=_API['STAR_RAIL_MONTH_INFO_URL'], url=_API['STAR_RAIL_MONTH_INFO_URL'],
method='GET', method='GET',
header=HEADER, header=HEADER,
params={'uid': sr_uid, 'region': server_id, 'month': ''}, params={'uid': sr_uid, 'region': server_id, 'month': month},
use_proxy=True, use_proxy=True,
) )
if isinstance(data, Dict): if isinstance(data, Dict):