Skip to content

Commit

Permalink
✨ 展示今日编码时间
Browse files Browse the repository at this point in the history
  • Loading branch information
KomoriDev committed Oct 5, 2024
1 parent ff198a6 commit c98189a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
13 changes: 10 additions & 3 deletions nonebot_plugin_wakatime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,16 @@ async def _(user_session: UserSession, target: Match[At | int]):
try:
user_info_task = API.get_user_info(target_id)
stats_info_task = API.get_user_stats(target_id)
stats_bar_info_task = API.get_user_stats_bar(target_id)
all_time_since_today_task = API.get_all_time_since_today(target_id)

user_info, stats_info, all_time_since_today = await asyncio.gather(
user_info_task, stats_info_task, all_time_since_today_task
user_info, stats_info, stats_bar_info, all_time_since_today = (
await asyncio.gather(
user_info_task,
stats_info_task,
stats_bar_info_task,
all_time_since_today_task,
)
)
except UserUnboundException:
await UniMessage.text(
Expand All @@ -125,6 +131,7 @@ async def _(user_session: UserSession, target: Match[At | int]):
result = WakaTime(
user=user_info,
stats=stats_info,
stats_bar=stats_bar_info,
all_time_since_today=all_time_since_today,
background_image=str(background_image),
)
Expand Down Expand Up @@ -161,7 +168,7 @@ async def _(
"client_id": client_id,
"response_type": "code",
"redirect_uri": redirect_uri,
"scope": "read_stats",
"scope": "read_stats,read_summaries",
"state": state,
}
)
Expand Down
24 changes: 23 additions & 1 deletion nonebot_plugin_wakatime/apis/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..models import User
from ..config import config
from ..bootstrap import driver
from ..schema import Stats, Users
from ..schema import Stats, Users, StatsBar
from ..exception import BindUserException, UserUnboundException

api_url = config.api_url
Expand Down Expand Up @@ -134,6 +134,28 @@ async def get_user_stats(
assert response.content
return Stats(**(json.loads(response.content)["data"]))

@classmethod
async def get_user_stats_bar(cls, user_id: int) -> StatsBar | None:
"""Get user’s coding activity today.
Args:
user_id: user id
"""
assess_token = await cls.get_access_token(user_id)
try:
async with driver.get_session() as session:
response = await session.request(
Request(
"GET",
f"{api_url}/users/current/status_bar/today",
headers={"Authorization": f"Bearer {assess_token}"},
)
)
assert response.content
return StatsBar(**(json.loads(response.content)["data"]))
except KeyError:
return None

@classmethod
async def get_all_time_since_today(cls, user_id: int) -> str:
"""Get the total time logged since account created."""
Expand Down
1 change: 1 addition & 0 deletions nonebot_plugin_wakatime/render_pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async def render(data: WakaTime) -> bytes:
templates={
"user": data["user"],
"background_image": data["background_image"],
"stats_bar": data["stats_bar"],
"insights": {
"data": data["stats"],
"last_week": calc_work_time_percentage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<div class="content">
<p class="title">注册时间<p class="mx-1 font-noto">📅</p></p><p class="result">{{ user["created_at"] }}</p>
</div>
{% if stats_bar %}
<div class="content">
<p class="title">今日编码时间<p class="mx-1 font-noto">⏳</p></p><p class="result">{{ stats_bar["grand_total"]["text"] }}</p>
</div>
{% endif %}
{% if operating_systems %}
<div class="content">
<p class="title">常用设备<p class="mx-1 font-noto">💻</p></p><p class="result">{{ operating_systems[0]["name"] }}</p>
Expand Down
2 changes: 2 additions & 0 deletions nonebot_plugin_wakatime/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from .stats import Stats as Stats
from .users import Users as Users
from .stats import StatsBar as StatsBar


class WakaTime(TypedDict):
user: Users
stats: Stats
stats_bar: StatsBar | None
all_time_since_today: str
background_image: str
18 changes: 18 additions & 0 deletions nonebot_plugin_wakatime/schema/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ class OperatingSystems(TypedDict):
minutes: int


class GrandTotal(TypedDict):
decimal: str
digital: str
hours: int
minutes: int
text: str
total_seconds: float


class Stats(TypedDict):

human_readable_total: str
Expand All @@ -75,3 +84,12 @@ class Stats(TypedDict):
operating_systems: list[OperatingSystems] | None
user_id: str
username: str


class StatsBar(TypedDict):
grand_total: GrandTotal
categories: list[Categories] | None
projects: list[Project] | None
editors: list[Editors] | None
languages: list[Languages] | None
operating_systems: list[OperatingSystems] | None

0 comments on commit c98189a

Please sign in to comment.