Skip to content

Commit

Permalink
fix: /lezioni command (UNICT-DMI#269)
Browse files Browse the repository at this point in the history
* fix: /lezioni command

* chore: pylint errors

* docs: update CONTRIBUTORS.md

* chore: add check for page availability

* feat: cache sistem to /lezioni command

* chore: fix pylint warnings

* chore: config map usage instead of constants

---------

Co-authored-by: Stefano Borzì <[email protected]>
  • Loading branch information
Picred and Helias authored Oct 14, 2023
1 parent 7458720 commit 1b8a0ac
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ This project is made possible thanks to the contributions of:
- [Paolo Rabbito](https://github.com/IlRabbino)
- [Andrea Maugeri](https://github.com/v0lp3)
- [Simone Costanzo](https://github.com/scraccio)
- [Salvo Polizzi](https://github.com/salvo-polizzi)
- [Salvo Polizzi](https://github.com/salvo-polizzi)
- [Andrei](https://github.com/Picred)
6 changes: 6 additions & 0 deletions config/settings.yaml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ test:
token: ''
representatives_group: ''
dev_group_chatid: ''

lectures:
href_token: "sites/default"
dmi_link: "https://web.dmi.unict.it/"
file_orario_path: "./data/Orario.pdf"
expire_time: 86400
1 change: 1 addition & 0 deletions data/markdown/informatica_link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://web.dmi.unict.it/
8 changes: 4 additions & 4 deletions data/markdown/lezioni_link.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
L-31:http://web.dmi.unict.it/corsi/l-31/orario-lezioni
L-35:http://web.dmi.unict.it/corsi/l-35/orario-lezioni
LM-18:http://web.dmi.unict.it/corsi/lm-18/orario-lezioni
LM-40:http://web.dmi.unict.it/corsi/lm-40/orario-lezioni
L-31:https://web.dmi.unict.it/corsi/l-31/orario-delle-lezioni
L-35:http://web.dmi.unict.it/corsi/l-35/orario-delle-lezioni
LM-18:http://web.dmi.unict.it/corsi/lm-18/orario-delle-lezioni
LM-40:http://web.dmi.unict.it/corsi/lm-40/orario-delle-lezioni
78 changes: 74 additions & 4 deletions module/commands/lezioni.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,68 @@
"""/lezioni command"""
import logging
import re
import requests
import os
import time
import datetime
from bs4 import BeautifulSoup
from typing import Tuple, Optional
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, CallbackQuery
from telegram.ext import CallbackContext
from module.data import Lesson
from module.shared import check_log, send_message
from module.shared import check_log, send_message, read_md, config_map
from module.data.vars import TEXT_IDS, PLACE_HOLDER
from module.utils.multi_lang_utils import get_locale, get_locale_code


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)


def get_url(courses: str) -> str:
"""Extract the L-31 url from markdown file.
Args:
courses: all available courses
Returns:
link of the website
"""
course_index = courses.find("L-31")
if course_index != -1:
token_pos = courses.find(":", course_index)
endl_index = courses.find("\n", course_index)

if token_pos != -1:
main_link = courses[token_pos + 1:endl_index]
return main_link


def get_orario_file() -> Optional[bytes]:
"""Download pdf file from website.
Returns:
None or downloaded file
"""
main_link = get_url(read_md("lezioni_link"))
soup = BeautifulSoup(requests.get(main_link, timeout=10).content, "html.parser")

for item in soup.find_all("a"):
if config_map['lectures']['href_token'] in str(item):
item = item.get("href")
full_pdf_link = config_map['lectures']['dmi_link'] + item

response = requests.get(full_pdf_link, timeout=10).content
with open(config_map['lectures']['file_orario_path'], "wb") as file:
file.write(response)
return response

return None


def lezioni(update: Update, context: CallbackContext) -> None:
"""Called by the /lezioni command.
Shows the options available to execute a lesson query.
Sends a pdf file to user downloaded from website.
Args:
update: update event
Expand All @@ -39,8 +86,31 @@ def lezioni(update: Update, context: CallbackContext) -> None:
context.bot.sendMessage(chat_id=chat_id, text=get_locale(locale, TEXT_IDS.USE_WARNING_TEXT_ID).replace(PLACE_HOLDER, "/lezioni"))
context.bot.sendMessage(chat_id=user_id, text=get_locale(locale, TEXT_IDS.GROUP_WARNING_TEXT_ID).replace(PLACE_HOLDER, "/lezioni"))

message_text, inline_keyboard = get_lezioni_text_InLineKeyboard(locale, context)
context.bot.sendMessage(chat_id=user_id, text=message_text, reply_markup=inline_keyboard)
if os.path.exists(config_map['lectures']['file_orario_path']): # Exist local file
current_time = time.time()
file_modified_time = os.path.getmtime(config_map['lectures']['file_orario_path'])
time_difference = current_time - file_modified_time
formatted_time= datetime.datetime.fromtimestamp(file_modified_time).strftime('%d-%m-%Y %H:%M:%S')

if time_difference < config_map['lectures']['expire_time']: # File not expired
with open(config_map['lectures']['file_orario_path'], "rb") as file:
context.bot.sendDocument(chat_id=update.effective_chat.id, document=file)
context.bot.sendMessage(chat_id=chat_id, text=f"Ultimo aggiornamento: {formatted_time}")
return
else: # Not exists local file, so download it
pass

file = get_orario_file()

if file is None:
context.bot.sendMessage(chat_id=chat_id, text="Orario non disponibile attualmente. Ritenta più tardi")
if os.path.exists(config_map['lectures']['file_orario_path']):
with open(config_map['lectures']['file_orario_path'], "rb") as file:
context.bot.sendDocument(chat_id=update.effective_chat.id, document=file)
else:
with open(config_map['lectures']['file_orario_path'], "rb") as file:
context.bot.sendDocument(chat_id=update.effective_chat.id, document=file)
context.bot.sendMessage(chat_id=chat_id, text="Ultimo aggiornamento: oggi")


def lezioni_handler(update: Update, context: CallbackContext) -> None:
Expand Down

0 comments on commit 1b8a0ac

Please sign in to comment.