init
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
from aiogram.filters import BaseFilter
|
||||
from aiogram.types import Message
|
||||
|
||||
|
||||
class AllowedUserFilter(BaseFilter):
|
||||
def __init__(self, allowed_user_ids: set[int]) -> None:
|
||||
self.allowed_user_ids = allowed_user_ids
|
||||
|
||||
async def __call__(self, message: Message) -> bool:
|
||||
return message.from_user is not None and message.from_user.id in self.allowed_user_ids
|
||||
@@ -0,0 +1,55 @@
|
||||
import asyncio
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class Journal:
|
||||
def __init__(
|
||||
self,
|
||||
vault_path: Path,
|
||||
daily_notes_dir: str,
|
||||
note_path_format: str,
|
||||
message_time_format: str,
|
||||
) -> None:
|
||||
self.vault_path = vault_path
|
||||
self.notes_path = vault_path / daily_notes_dir
|
||||
self.note_path_format = note_path_format
|
||||
self.message_time_format = message_time_format
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def append(self, text: str) -> None:
|
||||
async with self._lock:
|
||||
await asyncio.to_thread(self._append_and_sync, text)
|
||||
|
||||
def _append_and_sync(self, text: str) -> None:
|
||||
self._git("pull", "--rebase")
|
||||
|
||||
now = datetime.now().astimezone()
|
||||
note_path = self.notes_path / now.strftime(self.note_path_format)
|
||||
note_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
entry = f"- {now.strftime(self.message_time_format)} {text.strip()}\n"
|
||||
with note_path.open("a", encoding="utf-8") as file:
|
||||
file.write(entry)
|
||||
|
||||
relative_note_path = note_path.relative_to(self.vault_path)
|
||||
self._git("add", str(relative_note_path))
|
||||
if not self._has_staged_changes():
|
||||
return
|
||||
|
||||
self._git("commit", "-m", f"Update daily note {note_path.stem}")
|
||||
self._git("push")
|
||||
|
||||
def _has_staged_changes(self) -> bool:
|
||||
process = self._git("diff", "--cached", "--quiet", check=False)
|
||||
return process.returncode == 1
|
||||
|
||||
def _git(self, *args: str, check: bool = True) -> subprocess.CompletedProcess[str]:
|
||||
return subprocess.run(
|
||||
("git", *args),
|
||||
cwd=self.vault_path,
|
||||
check=check,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
)
|
||||
@@ -0,0 +1,32 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import random
|
||||
|
||||
from aiogram import Bot
|
||||
|
||||
|
||||
QUESTIONS = (
|
||||
"Как дела?",
|
||||
"Что нового?",
|
||||
"Что сейчас происходит?",
|
||||
"Чем занимаешься?",
|
||||
"Есть что записать?",
|
||||
"Как проходит день?",
|
||||
)
|
||||
|
||||
|
||||
async def send_reminders(
|
||||
bot: Bot,
|
||||
user_ids: set[int],
|
||||
min_seconds: int,
|
||||
max_seconds: int,
|
||||
) -> None:
|
||||
while True:
|
||||
await asyncio.sleep(random.randint(min_seconds, max_seconds))
|
||||
question = random.choice(QUESTIONS)
|
||||
|
||||
for user_id in user_ids:
|
||||
try:
|
||||
await bot.send_message(user_id, question)
|
||||
except Exception:
|
||||
logging.exception("Failed to send reminder to user_id=%s", user_id)
|
||||
Reference in New Issue
Block a user