Skip to content

Commit

Permalink
✨ 支持获取背景图 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
KomoriDev authored Aug 15, 2024
1 parent 17907c5 commit 0cad6ce
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,4 @@ cython_debug/
data/
wakatime.db
bot.py
shortcut.db.**
19 changes: 17 additions & 2 deletions nonebot_plugin_wakatime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

require("nonebot_plugin_orm")
require("nonebot_plugin_user")
require("nonebot_plugin_argot")
require("nonebot_plugin_alconna")
require("nonebot_plugin_htmlrender")
require("nonebot_plugin_localstore")
Expand All @@ -25,6 +26,7 @@
from .config import Config
from .schema import WakaTime
from .render_pic import render
from .utils import get_background_image
from .mount import State, WaitingRecord, waiting_codes
from .exception import BindUserException, UserUnboundException
from .bootstrap import client_id, mountable, redirect_uri, plugin_enable
Expand Down Expand Up @@ -102,11 +104,24 @@ async def _(user_session: UserSession, target: Match[At | int]):
.finish(at_sender=True, fallback=FallbackStrategy.ignore)
)

background_image = await get_background_image()

result = WakaTime(
user=user_info, stats=stats_info, all_time_since_today=all_time_since_today
user=user_info,
stats=stats_info,
all_time_since_today=all_time_since_today,
background_image=str(background_image),
)
image = await render(result)
await UniMessage.image(raw=image).finish(at_sender=True)
await UniMessage.image(raw=image).finish(
at_sender=True,
argot={
"name": "background",
"command": "background",
"content": str(background_image),
"expire": 300,
},
)


@wakatime.assign("bind")
Expand Down
2 changes: 1 addition & 1 deletion nonebot_plugin_wakatime/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class User(Model):

id: Mapped[int] = mapped_column(primary_key=True)
"""User ID"""
access_token: Mapped[str] = mapped_column(Text())
access_token: Mapped[str] = mapped_column(Text)
"""Wakatime Access Token"""
18 changes: 3 additions & 15 deletions nonebot_plugin_wakatime/render_pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from nonebot_plugin_htmlrender import template_to_pic, template_to_html

from .schema import WakaTime
from .config import RESOURCES_DIR, TEMPLATES_DIR, CustomSource, config
from .utils import image_to_base64, get_lolicon_image, calc_work_time_percentage
from .config import TEMPLATES_DIR
from .utils import calc_work_time_percentage


async def render(data: WakaTime) -> bytes:
Expand All @@ -13,24 +13,12 @@ async def render(data: WakaTime) -> bytes:
data["user"]["created_at"], "%Y-%m-%dT%H:%M:%SZ"
).strftime("%b %d %Y")

default_background = RESOURCES_DIR / "images" / "background.png"

match config.background_source:
case "default":
background_image = image_to_base64(default_background)
case "LoliAPI":
background_image = "https://www.loliapi.com/acg/pe/"
case "Lolicon":
background_image = await get_lolicon_image()
case CustomSource() as cs:
background_image = cs.to_uri()

return await template_to_pic(
template_path=str(TEMPLATES_DIR),
template_name="profile.html",
templates={
"user": data["user"],
"background_image": background_image,
"background_image": data["background_image"],
"insights": {
"data": data["stats"],
"last_week": calc_work_time_percentage(
Expand Down
1 change: 1 addition & 0 deletions nonebot_plugin_wakatime/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ class WakaTime(TypedDict):
user: Users
stats: Stats
all_time_since_today: str
background_image: str
22 changes: 22 additions & 0 deletions nonebot_plugin_wakatime/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from typing import Literal

import httpx
from pydantic_core import Url

from .config import RESOURCES_DIR, CustomSource, config


def image_to_base64(image_path: Path) -> str:
Expand All @@ -19,6 +22,25 @@ async def get_lolicon_image() -> str:
return response.json()["data"][0]["urls"]["original"]


async def get_background_image() -> str | Url:

default_background = RESOURCES_DIR / "images" / "background.png"

match config.background_source:
case "default":
background_image = image_to_base64(default_background)
case "LoliAPI":
background_image = "https://www.loliapi.com/acg/pe/"
case "Lolicon":
background_image = await get_lolicon_image()
case CustomSource() as cs:
background_image = cs.to_uri()
case _:
background_image = image_to_base64(default_background)

return background_image


def parse_time(work_time: str):

patterns = {
Expand Down
Loading

0 comments on commit 0cad6ce

Please sign in to comment.