Skip to content

Commit

Permalink
🐛 bugfix: url lacking image suffix when downloading from repo
Browse files Browse the repository at this point in the history
  • Loading branch information
KafCoppelia committed Oct 23, 2023
1 parent ca84b0f commit 8e9718f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions nonebot_plugin_tarot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .config import PluginConfig
from .data_source import tarot_manager

__plugin_version__ = "v0.5.0a1"
__plugin_version__ = "v0.5.0a2"
__plugin_usages__ = f"""
塔罗牌 {__plugin_version__}
[占卜] 随机选取牌阵进行占卜
Expand Down Expand Up @@ -75,7 +75,7 @@ async def _(matcher: Matcher, event: MessageEvent):
if "帮助" in arg[-2:]:
await matcher.finish(__plugin_usages__)

msg = await tarot_manager.onetime_divine()
msg = await tarot_manager.get_one_tarot()
await matcher.finish(msg)


Expand Down
23 changes: 12 additions & 11 deletions nonebot_plugin_tarot/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
except ModuleNotFoundError:
import json

from .config import get_tarot, ResourceError, tarot_config
from .config import download_tarot, ResourceError, tarot_config

CardInfoType = Dict[str, Union[str, Dict[str, str]]]
FormationInfoType = Dict[str, Dict[str, Union[int, bool, List[List[str]]]]]
Expand Down Expand Up @@ -142,8 +142,8 @@ async def divine_in_private(self, matcher: Matcher) -> None:
else:
await matcher.finish(msg_header + msg_body)

async def onetime_divine(self) -> Message:
"""One-time divination."""
async def get_one_tarot(self) -> Message:
"""Get one tarot."""
# 1. Pick a theme randomly.
theme = self._select_theme()

Expand Down Expand Up @@ -228,20 +228,21 @@ def _draw_n_cards(self, theme: str, n: int) -> List[CardInfoType]:
async def _get_text_and_image(
self, theme: str, card_info: CardInfoType
) -> Tuple[bool, Message]:
"""Get the tarot image & text based on `card_info`."""
"""Get the tarot image & text based on theme & `card_info`."""
_type: str = card_info.get("type") # type: ignore
_name: str = card_info.get("pic") # type: ignore
img_dir = tarot_config.tarot_path / theme / _type
img_name = ""
img_with_suffix = ""

# Consider the suffix of pictures, `.png` or `.jpg`, etc.
# Consider the suffix of picture, such as `.png` or `.jpg`.
for p in img_dir.glob(_name + ".*"):
img_name = p.name
img_with_suffix = p.name
break

if img_name == "":
# Not found in local directory, try to download from repo.
if img_with_suffix == "":
# Not found in the local directory, so try to download from repo.
if theme in TAROT_THEMES_IN_REPO:
data = await get_tarot(theme, _type, _name)
data = await download_tarot(theme, _type, _name, img_with_suffix)
if data is None:
return False, Message(MessageSegment.text("图片下载出错,请重试或将资源部署本地……"))

Expand All @@ -252,7 +253,7 @@ async def _get_text_and_image(
f"图片 {theme}/{_type}/{_name} 不存在!请确保自定义塔罗牌图片资源完整。"
)
else:
img = Image.open(img_dir / img_name)
img = Image.open(img_dir / img_with_suffix)

# Select whether the card is upright or reversed.
name_cn: str = card_info.get("name_cn") # type: ignore
Expand Down
4 changes: 3 additions & 1 deletion tests/test_tarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _get_divination_info() -> Tuple[DivinationInfo, str]:
return DivinationInfo(theme, cards_info, is_cut, representations), formation_name


def test_onetime_devine():
def test_get_one_tarot():
"""One-time divination."""
# 1. Pick a theme randomly.
theme = _select_theme()
Expand All @@ -143,6 +143,7 @@ def test_onetime_devine():
# Consider the suffix of pictures, `.png` or `.jpg`, etc.
for p in img_dir.glob(_name + ".*"):
img_name = p.name
break

assert img_name != ""

Expand All @@ -153,6 +154,7 @@ def test_onetime_devine():
# Consider the suffix of pictures, `.png` or `.jpg`, etc.
for p in img_dir.glob(_name + ".*"):
img_name = p.name
break

assert img_name == ""

Expand Down

0 comments on commit 8e9718f

Please sign in to comment.