Skip to content

Commit

Permalink
base version bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Samoed committed Sep 17, 2024
1 parent a78e946 commit cd23548
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
14 changes: 14 additions & 0 deletions courses_processor/compare_course_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json

with open("courses.json") as f:
api_courses = json.load(f)

with open("total_courses.json") as f:
parsed_courses = set(json.load(f))

api_courses_list = {course["name"].strip() for course in api_courses}

print(parsed_courses - api_courses_list)

with open("courses_diff.json", "w") as f:
json.dump(list(parsed_courses - api_courses_list), f)
35 changes: 26 additions & 9 deletions src/itmo_ai_timetable/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

import pytz
from logger import get_logger
from repositories.repository import Repository
from schedule_parser import ScheduleParser
from settings import Settings
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import (
Expand All @@ -16,6 +13,11 @@
ContextTypes,
)

from itmo_ai_timetable.repositories.calendar import CalendarRepository
from itmo_ai_timetable.repositories.db import DBRepository
from itmo_ai_timetable.schedule_parser import ScheduleParser
from itmo_ai_timetable.settings import Settings

logger = get_logger(__name__)
settings = Settings()

Expand All @@ -26,21 +28,36 @@ async def sync_courses_table(context: ContextTypes.DEFAULT_TYPE) -> None:
logger.info(f"Start sync {list_name}")
parser = ScheduleParser(excel_url, list_name)
pairs = parser.parse()
not_found = await Repository.add_classes(pairs)
not_found = await DBRepository.add_classes(pairs)
if not_found:
logger.warning(f"Classes not found: {not_found}")
not_found_str = [f"- {pair.start_time} {pair.end_time} {pair.name}\n" for pair in not_found]
not_found_str = [f"- {pair}\n" for pair in not_found]
await context.bot.send_message(
settings.admin_chat_id,
f"Classes not found: {not_found_str}\nКурс: {i}",
)
return
continue
logger.info(f"End sync {list_name}")
await context.bot.send_message(settings.admin_chat_id, "Sync finished table")


async def update_classes_calendar(context: ContextTypes.DEFAULT_TYPE) -> None:
pass
async def update_classes_calendar(context: ContextTypes.DEFAULT_TYPE) -> None: # noqa: ARG001
courses = await DBRepository.get_courses()
calendar_repo = CalendarRepository()

for course in courses:
if course.name not in ["Этика искусственного интеллекта", "Продвинутый курс научных исследований"]:
continue
if course.timetable_id is None:
course.timetable_id = calendar_repo.get_or_create_calendar(course.name)
await DBRepository.update_courses([course])
classes = await DBRepository.get_unsynced_classes_for_course(course)
for class_ in classes:
class_.gcal_event_id = calendar_repo.add_class_to_calendar(
course.timetable_id, course.name, class_.start_time, class_.end_time
)
class_.class_status_id = 5
await DBRepository.update_classes(classes)


async def ping(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: # noqa: ARG001
Expand Down Expand Up @@ -80,7 +97,7 @@ def add_jobs(application: Application, time_zone_str: str) -> None:
if application.job_queue is None:
raise ValueError("Job queue is None")
application.job_queue.run_daily(sync_courses_table, time=time(8, tzinfo=time_zone))
application.job_queue.run_daily(update_classes_calendar, time=time(hour=8, minute=5, tzinfo=time_zone))
application.job_queue.run_repeating(update_classes_calendar, interval=60)


def main() -> None:
Expand Down
14 changes: 14 additions & 0 deletions src/itmo_ai_timetable/repositories/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ async def update_courses(courses: list[Course], *, session: AsyncSession) -> Non
session.add_all(courses)
await session.commit()

@staticmethod
@with_async_session
async def update_classes(classes: list[Class], *, session: AsyncSession) -> None:
session.add_all(classes)
await session.commit()

@staticmethod
async def get_existing_classes(
course_id: int,
Expand Down Expand Up @@ -132,3 +138,11 @@ async def set_gcal_id_to_course(course_name: str, gcal_id: str, *, session: Asyn
course = await DBRepository.get_course(course_name, session)
course.gcal_id = gcal_id
await session.commit()

@staticmethod
@with_async_session
async def get_unsynced_classes_for_course(course: Course, *, session: AsyncSession) -> Sequence[Class]:
synced_status = await DBRepository.get_class_status_by_name(ClassStatus.synced, session=session)
query = select(Class).filter(and_(Class.course_id == course.id, Class.class_status != synced_status))
result = await session.execute(query)
return result.scalars().all()
6 changes: 3 additions & 3 deletions src/itmo_ai_timetable/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class Settings(BaseSettings):

courses_to_skip: list[str] = Field(["Выходной", "Demoday 12:00-15:30"], description="Courses to skip")

course_1_excel_calendar_id: HttpUrl = Field(description="Link to course 1 calendar")
course_1_excel_calendar_id: str = Field(description="Link to course 1 calendar")
course_1_list_name: str = Field("Расписание", description="Name of course 1 list")
course_2_excel_calendar_id: HttpUrl = Field(description="Link to course 2 calendar")
course_2_excel_calendar_id: str = Field(description="Link to course 2 calendar")
course_2_list_name: str = Field("Расписание", description="Name of course 2 list")

google_credentials_path: FilePath = Field(
Expand All @@ -35,7 +35,7 @@ class Settings(BaseSettings):
google_token_path: FilePath = Field(description="Path to google token file")

tg_bot_token: str = Field(description="Telegram bot token")
admin_chat_id: int = Field(description="Admin chat id")
admin_chat_id: str = Field(description="Admin chat id")

course_info_url: HttpUrl = Field(description="Course info url")

Expand Down

0 comments on commit cd23548

Please sign in to comment.