e-caste / reti-quiz

A script and a Telegram bot to help prepare the Computer Networks exam

Home Page:https://t.me/networks_quiz_bot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Format file after downloading and saving to variable

e-caste opened this issue · comments

The file can be changed by students unknowingly in a way that breaks the file parsing. This issue is to fix this as much as possible after downloading the file, and not during execution.

A possible error traceback:

2020-04-22 11:04:39,982 - telegram.ext.dispatcher - ERROR - An error was raised while processing the update and an unc
aught error was raised while handling the error with an error_handler
Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 403, in process_update
    self.dispatch_error(update, e)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 528, in dispatch_error
    callback(update, CallbackContext.from_error(update, error, self))
  File "/home/ubuntu/.local/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 390, in process_update
    handler.handle_update(update, self, check, context)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/telegram/ext/conversationhandler.py", line 381, in handle_upda
te
    new_state = handler.handle_update(update, dispatcher, check_result, context)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/telegram/ext/handler.py", line 117, in handle_update
    return self.callback(update, context)
  File "/home/ubuntu/reti-quiz/reti_quiz_telegram_bot.py", line 255, in quiz_ans
    quiz_question(update, context)
  File "/home/ubuntu/reti-quiz/reti_quiz_telegram_bot.py", line 232, in quiz_question
    __send_question(update, context)
  File "/home/ubuntu/reti-quiz/reti_quiz_telegram_bot.py", line 159, in __send_question
    q, a, comment = __get_q_and_a(context)
  File "/home/ubuntu/reti-quiz/reti_quiz_telegram_bot.py", line 154, in __get_q_and_a
    context.user_data['questions'][context.user_data['q_counter']])
  File "/home/ubuntu/reti-quiz/quiz.py", line 89, in get_q_n_a
    q_and_a = text.split("Esercizio " + str(q_n) + ". ")[1].split("\n\n")[0]

I'll leave this open, but since the file format is very simple, the parsing should work most of the times. Also, this issue means a lot more refactoring and rewriting than I had anticipated, so I'll mark it as low priority.