-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
record meme generation & add label hot
- Loading branch information
Showing
8 changed files
with
755 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""init_db | ||
迁移 ID: a82b5f32c60f | ||
父迁移: | ||
创建时间: 2024-08-12 22:16:13.840860 | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Sequence | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
revision: str = "a82b5f32c60f" | ||
down_revision: str | Sequence[str] | None = None | ||
branch_labels: str | Sequence[str] | None = ("nonebot_plugin_memes",) | ||
depends_on: str | Sequence[str] | None = "fff55366306e" | ||
|
||
|
||
def upgrade(name: str = "") -> None: | ||
if name: | ||
return | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"nonebot_plugin_memes_memegenerationrecord", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("session_persist_id", sa.Integer(), nullable=False), | ||
sa.Column("time", sa.DateTime(), nullable=False), | ||
sa.Column("meme_key", sa.String(length=64), nullable=False), | ||
sa.PrimaryKeyConstraint( | ||
"id", name=op.f("pk_nonebot_plugin_memes_memegenerationrecord") | ||
), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(name: str = "") -> None: | ||
if name: | ||
return | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table("nonebot_plugin_memes_memegenerationrecord") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from datetime import datetime, timedelta, timezone | ||
|
||
from nonebot_plugin_orm import Model, get_session | ||
from nonebot_plugin_session import Session, SessionIdType | ||
from nonebot_plugin_session_orm import SessionModel, get_session_persist_id | ||
from sqlalchemy import String, select | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
|
||
|
||
class MemeGenerationRecord(Model): | ||
"""表情调用记录""" | ||
|
||
__table_args__ = {"extend_existing": True} | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
session_persist_id: Mapped[int] | ||
""" 会话持久化id """ | ||
time: Mapped[datetime] | ||
""" 调用时间\n\n存放 UTC 时间 """ | ||
meme_key: Mapped[str] = mapped_column(String(64)) | ||
""" 表情名 """ | ||
|
||
|
||
async def record_meme_generation(session: Session, meme_key: str): | ||
session_persist_id = await get_session_persist_id(session) | ||
|
||
record = MemeGenerationRecord( | ||
session_persist_id=session_persist_id, | ||
time=datetime.now(timezone.utc), | ||
meme_key=meme_key, | ||
) | ||
async with get_session() as db_session: | ||
db_session.add(record) | ||
await db_session.commit() | ||
|
||
|
||
async def get_meme_generation_keys( | ||
session: Session, id_type: SessionIdType, time_delta: timedelta | ||
) -> list[str]: | ||
whereclause = SessionModel.filter_statement( | ||
session, id_type, include_bot_type=False | ||
) | ||
whereclause.append( | ||
MemeGenerationRecord.time >= (datetime.now(timezone.utc) - time_delta) | ||
) | ||
statement = ( | ||
select(MemeGenerationRecord.meme_key) | ||
.where(*whereclause) | ||
.join(SessionModel, SessionModel.id == MemeGenerationRecord.session_persist_id) | ||
) | ||
async with get_session() as db_session: | ||
result = (await db_session.scalars(statement)).all() | ||
return list(result) |
Oops, something went wrong.