Skip to content

Commit

Permalink
Migrate to SQLite DB from JSON files (#20)
Browse files Browse the repository at this point in the history
* Remove .json files as migrate to async sqlite.db
* Add a backup strategy saving the db once a day
* Add async to other functions e.g. crawler
  • Loading branch information
ckreisl committed Jul 6, 2024
1 parent 5752a23 commit 72488d5
Show file tree
Hide file tree
Showing 25 changed files with 1,241 additions and 711 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ htmlcov

pytest.xml
coverage.xml

backups/
*.db
84 changes: 0 additions & 84 deletions cs2posts/bot/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,87 +24,3 @@ class Chat:
def from_json(cls, data: dict[str, str]) -> Chat:
last_activity = datetime.fromisoformat(data.pop('last_activity'))
return cls(**data, last_activity=last_activity)

def to_json(self) -> dict[str, str]:
return {
"chat_id": self.chat_id,
"chat_id_admin": self.chat_id_admin,
"strikes": self.strikes,
"is_running": self.is_running,
"is_banned": self.is_banned,
"is_removed_while_banned": self.is_removed_while_banned,
"is_news_interested": self.is_news_interested,
"is_update_interested": self.is_update_interested,
"is_external_news_interested": self.is_external_news_interested,
"last_activity": self.last_activity.isoformat()
}


class Chats:

def __init__(self, chats: list[Chat] = None) -> None:
if chats is None:
chats = []
self.__chats: dict[int, Chat] = {}
for chat in chats:
self.__chats[chat.chat_id] = chat

@property
def chats(self) -> list[Chat]:
return list(self.__chats.values())

def get(self, chat_id: int) -> Chat | None:
return self.__chats.get(chat_id)

def add(self, chat: Chat) -> None:
self.__chats[chat.chat_id] = chat

def create(self, chat_id: int) -> Chat:
return Chat(chat_id=chat_id)

def remove(self, chat: Chat) -> None:
self.__chats.pop(chat.chat_id, None)

def update(self, chat: Chat) -> None:
self.__chats[chat.chat_id] = chat

def contains(self, chat_id: int) -> bool:
return chat_id in self.__chats

def create_and_add(self, chat_id: int) -> Chat:
chat = self.create(chat_id)
self.add(chat)
return chat

def migrate(self, chat: Chat, new_chat_id: int) -> Chat:
self.remove(chat)
chat.chat_id = new_chat_id
self.add(chat)
return chat

def get_running_chats(self) -> list[Chat]:
return [chat for chat in self.__chats.values() if chat.is_running]

def get_interested_in_news(self) -> list[Chat]:
return [chat for chat in self.__chats.values() if chat.is_news_interested]

def get_interested_in_updates(self) -> list[Chat]:
return [chat for chat in self.__chats.values() if chat.is_update_interested]

def get_running_and_interested_in_news(self) -> list[Chat]:
return [chat for chat in self.__chats.values() if chat.is_running and chat.is_news_interested]

def get_running_and_interested_in_updates(self) -> list[Chat]:
return [chat for chat in self.__chats.values() if chat.is_running and chat.is_update_interested]

def get_running_and_interested_in_external_news(self) -> list[Chat]:
return [chat for chat in self.__chats.values() if chat.is_running and chat.is_external_news_interested]

def __contains__(self, chat: Chat) -> bool:
return chat.chat_id in self.__chats

def __iter__(self):
return iter(self.__chats.values())

def __len__(self) -> int:
return len(self.__chats)
Loading

1 comment on commit 72488d5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
__init__.py00100% 
crawler.py300100% 
cs2.py831087%30, 37, 42, 50, 68, 89, 105, 118–120
db.py2072189%52, 73–76, 155, 157–158, 161–166, 352, 354–355, 358–359, 361–362
post.py79297%21, 61
bot
   __init__.py00100% 
   chats.py210100% 
   constants.py40100% 
   content.py76692%28, 49, 62, 90, 100, 106
   cs2.py2928770%37–42, 44–46, 97–99, 101–102, 104–106, 108–109, 111, 114, 116–120, 122–125, 127, 136, 140–143, 192, 306–309, 312–313, 315–316, 318, 320, 323–325, 328–329, 331–332, 334, 336, 339–341, 344–345, 347, 349–350, 352, 354, 357–359, 369, 371–373, 375–377, 379, 422, 424–426, 432, 434–436, 439, 442
   decorators.py330%1, 3, 5
   message.py1633379%83–86, 99, 120, 122, 132, 134–135, 137, 139–141, 150–153, 155, 167–170, 210, 213, 220, 222, 225–226, 241, 248–249, 266
   options.py982079%34, 41–43, 48–50, 52, 69, 73–74, 78, 80, 82, 85–87, 89, 159–160
   settings.py170100% 
   spam.py710100% 
   utils.py240100% 
parser
   __init__.py00100% 
   parser.py14192%21
   steam2telegram_html.py260100% 
   steam_list.py520100% 
   steam_news_image.py160100% 
   steam_news_youtube.py120100% 
   steam_update_heading.py210100% 
TOTAL130918386% 

Tests Skipped Failures Errors Time
137 0 💤 0 ❌ 0 🔥 3.950s ⏱️

Please sign in to comment.