-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bump to 0.2.1: Add action history to avoid making multiple posterity …
…comments per post
- Loading branch information
Showing
9 changed files
with
274 additions
and
3 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "rflying_tower_bot" | ||
version = "0.2.0" | ||
version = "0.2.1" | ||
description = "" | ||
authors = ["Kris Knigga <[email protected]>"] | ||
|
||
|
@@ -11,6 +11,7 @@ pydantic-yaml = "^1.3.0" | |
aiohttp = "^3.9.5" | ||
pydantic = "^2.8.2" | ||
python-dotenv = "^1.0.1" | ||
sqlalchemy = {extras = ["asyncio"], version = "^2.0.31"} | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
pre-commit = "^3.7.1" | ||
|
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""rflying_tower_bot package.""" | ||
|
||
__version__ = "0.2.0" | ||
__version__ = "0.2.1" |
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,98 @@ | ||
"""Keep track of action history.""" | ||
|
||
import datetime | ||
import logging | ||
|
||
from sqlalchemy import DateTime, String, func, insert, select | ||
from sqlalchemy.ext.asyncio import AsyncAttrs, async_sessionmaker, create_async_engine | ||
from sqlalchemy.orm import DeclarativeBase, mapped_column | ||
|
||
|
||
class Base(AsyncAttrs, DeclarativeBase): # noqa: D101 | ||
pass | ||
|
||
|
||
class HistoryTable(Base): | ||
"""Represents a table for storing action history.""" | ||
|
||
__tablename__ = "history" | ||
|
||
url = mapped_column(String, primary_key=True) | ||
action = mapped_column(String, primary_key=True) | ||
time = mapped_column(DateTime) | ||
|
||
|
||
class History: | ||
"""Keep track of action history.""" | ||
|
||
def __init__( | ||
self, db_connection_string: str = "sqlite+aiosqlite:///:memory:" | ||
) -> None: | ||
""" | ||
Initialize the History class. | ||
Args: | ||
---- | ||
db_connection_string (str, optional): The database connection string. Defaults to "sqlite+aiosqlite:///:memory:". | ||
""" | ||
self.log: logging.Logger = logging.getLogger( | ||
f"{__name__}.{self.__class__.__name__}" | ||
) | ||
|
||
self.db = create_async_engine(db_connection_string, echo=False) | ||
|
||
async def initialize_db(self) -> None: | ||
"""Initialize the database.""" | ||
self.log.info("Initializing database: %s", self.db.url) | ||
async with self.db.begin() as conn: | ||
await conn.run_sync(Base.metadata.create_all) | ||
|
||
async def check(self, url: str, action: str) -> int: | ||
""" | ||
Check the number of occurrences of a specific action for a given URL. | ||
Args: | ||
---- | ||
url (str): The URL to check. | ||
action (str): The action to check. | ||
Returns: | ||
------- | ||
int: The number of occurrences of the action for the URL. | ||
""" | ||
async_session = async_sessionmaker(self.db, expire_on_commit=False) | ||
async with async_session() as session: | ||
stmt = ( | ||
select(func.count(HistoryTable.url)) | ||
.where(HistoryTable.url == url) | ||
.where(HistoryTable.action == action) | ||
) | ||
n = await session.scalar(stmt) | ||
if n is not None: | ||
return n | ||
return 0 | ||
|
||
async def add(self, url: str, action: str) -> None: | ||
""" | ||
Add a new entry to the history. | ||
Args: | ||
---- | ||
url (str): The URL to add. | ||
action (str): The action to add. | ||
Returns: | ||
------- | ||
None | ||
""" | ||
self.log.debug('Inserting url "%s" into history', url) | ||
async_session = async_sessionmaker(self.db, expire_on_commit=False) | ||
async with async_session() as session: | ||
stmt = insert(HistoryTable).values( | ||
url=url, action=action, time=datetime.datetime.now() | ||
) | ||
await session.execute(stmt) | ||
await session.commit() |
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