Skip to content

Commit

Permalink
Fix dining login check
Browse files Browse the repository at this point in the history
  • Loading branch information
mhbahmani committed Oct 5, 2023
1 parent dd4d704 commit e9c27de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
35 changes: 20 additions & 15 deletions src/dining.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, student_id: str, password: str) -> None:
self.csrf = None
self.remainCredit = 0
if not self.__setad_login():
raise (Exception(ErrorHandler.NOT_ALLOWED_TO_RESERVATION_PAGE_ERROR))
raise (Exception(ErrorHandler.INVALID_DINING_CREDENTIALS_ERROR))

def reserve_food(self, place_id: int, foods: dict, choosed_food_indices: dict) -> bool:
"""
Expand Down Expand Up @@ -185,7 +185,9 @@ def __setad_login(self) -> bool:
script = bs(response.content, "html.parser").find("script", {"type": "text/javascript"}).next.strip()
self.csrf = re.match(r".*X-CSRF-TOKEN' : '(?P<csrf>.*)'}.*", script).group("csrf")

if response.status_code != HTTPStatus.OK:
if response.status_code != HTTPStatus.OK \
or str(response.text).find('ورود به سامانه سماد') != -1 \
or str(response.text).find('نام کاربری یا رمز عبور اشتباه است') != -1:
logging.debug("Login failed")
return False
return True
Expand Down Expand Up @@ -218,24 +220,27 @@ def __login(self) -> bool:
return True

def check_username_and_password(username: str, password: str) -> bool:
logging.debug("Checking username and password for user %s", username)
logging.debug("Making session")
session = requests.Session()
logging.debug("Get login page")
site = session.get(Dining.SIGN_IN_URL)
content = bs(site.content, "html.parser")
authenticity_token = content.find("input", {"name": "authenticity_token"}).get('value')
login_data = {
'authenticity_token': authenticity_token,
'student[student_identifier]': username,
'student[password]': password,
'commit': 'ورود به حساب کاربری'

reserve_page = session.get(Dining.RESERVE_PAGE_URL)
csrf = bs(reserve_page.content, "html.parser").find("input", {"name": "_csrf"}).get("value")
data = {
'_csrf': csrf,
'username': username,
'password': password,
'login': 'ورود',
}
response = session.post(Dining.SIGN_IN_URL, login_data)
if bs(response.content, "html.parser").find("div", {"class": "card-alert alert alert-warning mb-0"}):
logging.debug("Login failed")
return False
logging.debug("Login successful")

response = session.post('https://setad.dining.sharif.edu/j_security_check', data=data)

if response.status_code != HTTPStatus.OK \
or str(response.text).find('ورود به سامانه سماد') != -1 \
or str(response.text).find('نام کاربری یا رمز عبور اشتباه است') != -1:
logging.debug("Username or password was wrong")
return False
return True

def __load_food_table(self, place_id: str, week: int = 0) -> requests.Response:
Expand Down
5 changes: 3 additions & 2 deletions src/error_handlers/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class ErrorHandler:
NOT_ALLOWED_TO_RESERVATION_PAGE_ERROR = "User is not allowed to access reservation page"
INVALID_DINING_CREDENTIALS_ERROR = "Invalid credentials"

def __init__(self, admin_ids=set(), sentry_dsn: str = None, environment: str = "development") -> None:
self.admin_ids = admin_ids
Expand Down Expand Up @@ -74,8 +75,8 @@ def send_error_message_to_user(self, update, context) -> None:
chat_id = None
if update.callback_query: chat_id = update.callback_query.message.chat_id
elif update.message: chat_id = update.message.chat_id
if context.error.args[0] == ErrorHandler.NOT_ALLOWED_TO_RESERVATION_PAGE_ERROR:
if context.error.args[0] == ErrorHandler.INVALID_DINING_CREDENTIALS_ERROR:
context.bot.send_message(
text=messages.not_allowed_to_reserve_message,
text=messages.invalid_credentials_message,
chat_id=chat_id
)
1 change: 1 addition & 0 deletions src/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
send_to_all_done_message = "پیامت برای همه با موفقیت ارسال شد :)"

not_allowed_to_reserve_message = "داینینگ بهت امکان رزرو غذا رو نداده. چک کن که بخش رزرو غذا توی داینینگ برات باز باشه"
invalid_credentials_message = "نام کاربری یا رمز عبورت اشتباهه. یه چک بکن و دوباره امتحان کن"

duplicate_forget_code_message = "این کد قبلا وارد شده و امکان ۲ بار وارد کردن یه کد یکسان وجود نداره."

Expand Down

0 comments on commit e9c27de

Please sign in to comment.