Skip to content

Commit

Permalink
feat: new version
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuNatsu committed Aug 8, 2024
1 parent 03b67f8 commit 932c012
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 55 deletions.
Binary file removed nonebot_plugin_deer_pipe/MiSans_VF.ttf
Binary file not shown.
72 changes: 18 additions & 54 deletions nonebot_plugin_deer_pipe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import calendar
import json
import os
import secrets

from PIL import Image, ImageDraw, ImageFont
from .contants import USERDATA_PATH
from .image import generate_image
from datetime import datetime
from nonebot.plugin import PluginMetadata, require
from typing import Any, Sequence
Expand All @@ -16,17 +15,26 @@

# Plugin meta
__plugin_meta__: PluginMetadata = PluginMetadata(
name="nonebot-plugin-deer-pipe",
name="🦌管签到",
description="一个🦌管签到插件",
usage="发送🦌以进行签到",
type="application",
homepage="https://github.com/SamuNatsu/nonebot-plugin-deer-pipe"
homepage="https://github.com/SamuNatsu/nonebot-plugin-deer-pipe",
supported_adapters={
"~console",
"~discord",
"~dodo",
"~feishu",
"~kaiheila",
"~onebot.v11",
"~onebot.v12",
"~qq",
"~red",
"~satori",
"~telegram",
}
)

# Constants
PLUGIN_PATH: str = os.path.dirname(os.path.realpath(__file__))
USERDATA_PATH: str = f"{PLUGIN_PATH}/userdata.json"

# Attendance
def attendance(now: datetime, user_id: str) -> tuple[bool, Sequence[int]]:
if not os.path.exists(USERDATA_PATH):
Expand Down Expand Up @@ -55,50 +63,6 @@ def attendance(now: datetime, user_id: str) -> tuple[bool, Sequence[int]]:
json.dump(data, f)
return (True, userdata["deer"])

# Generate image
def gen_img(now: datetime, title: str, deer: Sequence[int]) -> bytes:
font = ImageFont.truetype(f"{PLUGIN_PATH}/MiSans_VF.ttf", 20)
caln = calendar.monthcalendar(now.year, now.month)

img_w, img_h = 700, 100 * (len(caln) + 1)
ret_img = Image.new("RGBA", (img_w, img_h), "white")
draw = ImageDraw.Draw(ret_img)

box_w, box_h = 100, 100

deer_img = Image.open(f"{PLUGIN_PATH}/deerpipe.jpg")
deer_w, deer_h = deer_img.size
deer_s: float = 100 / max(deer_w, deer_h)
deer_img = deer_img.resize((int(deer_w * deer_s), int(deer_h * deer_s)))

check_img = Image.open(f"{PLUGIN_PATH}/check.png")
check_w, check_h = check_img.size
check_s: float = 100 / max(check_w, check_h)
check_img = check_img.resize((int(check_w * check_s), int(check_h * check_s)))

for week_num, week in enumerate(caln):
for day_num, day in enumerate(week):
x0 = day_num * box_w
y0 = (week_num + 1) * box_h
if day != 0:
ret_img.paste(deer_img, (x0, y0))
draw.text((x0 + 5, y0 + box_h - 35), str(day), fill='black', font=font)
if day in deer:
ret_img.paste(check_img, (x0, y0), check_img)

font = ImageFont.truetype(f"{PLUGIN_PATH}/MiSans_VF.ttf", 40)
draw.text((5, 5), f"{now.year}-{now.month:02} 签到", fill="black", font=font)
draw.text((5, 50), title, fill="black", font=font)

img_path = f"{PLUGIN_PATH}/{secrets.token_hex()}.png"
ret_img.save(img_path)

with open(img_path, "rb") as f:
ret_raw = f.read()
os.remove(img_path)

return ret_raw

# Matchers
deer_matcher = on_alconna("🦌")

Expand All @@ -113,7 +77,7 @@ async def handle(user_info: UserInfo = EventUserInfo()) -> None:

now: datetime = datetime.now()
ok, deer = attendance(now, user_info.user_id)
img: bytes = gen_img(now, name, deer)
img: bytes = generate_image(now, name, deer)

await UniMessage.text(
f"{name} 刚刚🦌了" if ok else f"{name} 今天已经🦌过了"
Expand Down
Binary file not shown.
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.
Binary file removed nonebot_plugin_deer_pipe/check.png
Binary file not shown.
24 changes: 24 additions & 0 deletions nonebot_plugin_deer_pipe/contants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from PIL import Image, ImageFont
from PIL.ImageFile import ImageFile
from PIL.ImageFont import FreeTypeFont
from nonebot.plugin import require
from pathlib import Path

require("nonebot_plugin_localstore")
import nonebot_plugin_localstore as store


PLUGIN_PATH: Path = Path(os.path.dirname(os.path.realpath(__file__)))
ASSETS_PATH: Path = PLUGIN_PATH / "assets"

CHECK_IMG: ImageFile = Image.open(ASSETS_PATH / "[email protected]")
DEERPIPE_IMG: ImageFile = Image.open(ASSETS_PATH / "[email protected]")

MISANS_FONT: FreeTypeFont = ImageFont.truetype(
ASSETS_PATH / "MiSans-Regular.ttf",
25
)

USERDATA_PATH: Path = store.get_plugin_data_file("userdata.json")
Binary file removed nonebot_plugin_deer_pipe/deerpipe.jpg
Binary file not shown.
41 changes: 41 additions & 0 deletions nonebot_plugin_deer_pipe/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import calendar
import os
import secrets

from .contants import CHECK_IMG, DEERPIPE_IMG, MISANS_FONT, PLUGIN_PATH
from PIL import Image, ImageDraw
from datetime import datetime
from pathlib import Path
from typing import Sequence


def generate_image(now: datetime, name: str, deer: Sequence[int]) -> bytes:
cal: list[list[int]] = calendar.monthcalendar(now.year, now.month)

IMG_W, IMG_H = 700, 100 * (len(cal) + 1)
BOX_W, BOX_H = 100, 100

img: Image.Image = Image.new("RGBA", (IMG_W, IMG_H), "white")
drw: ImageDraw.ImageDraw = ImageDraw.Draw(img)

for week_idx, week in enumerate(cal):
for day_idx, day in enumerate(week):
x0: int = day_idx * BOX_W
y0: int = (week_idx + 1) * BOX_H
if day != 0:
img.paste(DEERPIPE_IMG, (x0, y0))
drw.text((x0 + 5, y0 + BOX_H - 35), str(day), fill='black', font=MISANS_FONT)
if day in deer:
img.paste(CHECK_IMG, (x0, y0), CHECK_IMG)

drw.text((5, 5), f"{now.year}-{now.month:02} 签到", fill="black", font=MISANS_FONT)
drw.text((5, 50), name, fill="black", font=MISANS_FONT)

img_path: Path = PLUGIN_PATH / f"{secrets.token_hex()}.png"
img.save(img_path)

with open(img_path, "rb") as f:
raw: bytes = f.read()
os.remove(img_path)

return raw
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "nonebot-plugin-deer-pipe"
version = "0.0.4"
version = "0.0.5"
authors = [
{ name="SNRainiar", email="[email protected]" },
]
Expand All @@ -21,6 +21,7 @@ dependencies = [
"nonebot2",
"nonebot-adapter-onebot",
"nonebot-plugin-alconna",
"nonebot-plugin-localstore",
"nonebot_plugin_userinfo"
]

Expand Down

0 comments on commit 932c012

Please sign in to comment.