Skip to content

Commit

Permalink
feat: Packaged script in container
Browse files Browse the repository at this point in the history
  • Loading branch information
mfloto committed Sep 16, 2024
1 parent cb6a6ad commit 3db0032
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
grades.csv
grades.html
venv/
__pycache__/
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.12-slim-bookworm
WORKDIR /notifier

COPY dualis_notifier.py config.py requirements.txt entrypoint.sh /notifier/
RUN chmod 0755 /notifier/entrypoint.sh

RUN pip install --no-cache-dir --compile -r requirements.txt

RUN apt update
RUN apt install cron -y

RUN echo "* * * * * /usr/local/bin/python3 /notifier/dualis_notifier.py >> /var/log/cron.log 2>&1" | crontab -
CMD ["/notifier/entrypoint.sh"]
22 changes: 17 additions & 5 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
user = ""
passwd = ""
semester_id = ""
hook_url = ""
user_agent = "Dualis Notifier"
import os


def load_config():
user = os.getenv("DUALIS_USER")
passwd = os.getenv("DUALIS_PASSWD")
semester_id = os.getenv("SEMESTER_ID")
hook_url = os.getenv("DISCORD_WEBHOOK")
user_agent = os.getenv("AGENT_NAME", "Dualis Notifier")

return {
"user": user,
"passwd": passwd,
"semester_id": semester_id,
"hook_url": hook_url,
"user_agent": user_agent,
}
41 changes: 25 additions & 16 deletions dualis_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import re
import os

import config
from config import load_config

config = load_config()


def get_session(user, passwd) -> dict:
url = "https://dualis.dhbw.de/scripts/mgrqispi.dll"

payload = f'usrname={user}%40student.dhbw-mannheim.de&pass={passwd}&APPNAME=CampusNet&PRGNAME=LOGINCHECK&ARGUMENTS=clino%2Cusrname%2Cpass%2Cmenuno%2Cmenu_type%2Cbrowser%2Cplatform&clino=000000000000001&menuno=000324&menu_type=classic&browser=&platform='
payload = f"usrname={user}%40student.dhbw-mannheim.de&pass={passwd}&APPNAME=CampusNet&PRGNAME=LOGINCHECK&ARGUMENTS=clino%2Cusrname%2Cpass%2Cmenuno%2Cmenu_type%2Cbrowser%2Cplatform&clino=000000000000001&menuno=000324&menu_type=classic&browser=&platform="
headers = {
'User-Agent': config.user_agent,
'Content-Type': 'application/x-www-form-urlencoded',
"User-Agent": config["user_agent"],
"Content-Type": "application/x-www-form-urlencoded",
}

response = requests.request("POST", url, headers=headers, data=payload)
Expand All @@ -21,16 +23,18 @@ def get_session(user, passwd) -> dict:

return {
"cookie": response.headers["Set-Cookie"].split(";")[0].replace(" ", ""),
"session": re.search(regex_pattern_session, response.headers["REFRESH"]).group(1)
"session": re.search(regex_pattern_session, response.headers["REFRESH"]).group(
1
),
}


def get_grades(cookie, session, semester_id) -> str:
url = f"https://dualis.dhbw.de/scripts/mgrqispi.dll?APPNAME=CampusNet&PRGNAME=COURSERESULTS&ARGUMENTS={session},-N000307,{semester_id}"

headers = {
'User-Agent': config.user_agent,
'Cookie': f'{cookie}; {cookie}',
"User-Agent": config["user_agent"],
"Cookie": f"{cookie}; {cookie}",
}

response = requests.get(url, headers=headers)
Expand All @@ -44,13 +48,15 @@ def get_grades(cookie, session, semester_id) -> str:
def extract_data_from_html(raw_html) -> pd.DataFrame:
tables = pd.read_html(raw_html)
df = tables[0]
df.drop(df.columns[df.columns.str.contains(
'unnamed', case=False)], axis=1, inplace=True)
df.drop(
df.columns[df.columns.str.contains("unnamed", case=False)], axis=1, inplace=True
)
return df


def notify_server(name, hook_url) -> None:
data = {
print("Server notification sim!")
"""data = {
"username": "DUALIS"
}
data["embeds"] = [
Expand All @@ -60,17 +66,20 @@ def notify_server(name, hook_url) -> None:
"url": "https://dualis.dhbw.de"
}
]
requests.post(hook_url, json=data)
requests.post(hook_url, json=data)"""


if __name__ == "__main__":
creds = get_session(config.user, config.passwd)
creds = get_session(config["user"], config["passwd"])
raw_html = get_grades(
cookie=creds["cookie"], session=creds["session"], semester_id=config.semester_id)
cookie=creds["cookie"],
session=creds["session"],
semester_id=config["semester_id"],
)
table = extract_data_from_html(raw_html)

if os.path.exists('grades.csv'):
old_grades = pd.read_csv('grades.csv', index_col=0)
if os.path.exists("grades.csv"):
old_grades = pd.read_csv("grades.csv", index_col=0)
res = table.compare(old_grades)
index_list = res.index.to_list()

Expand All @@ -83,7 +92,7 @@ def notify_server(name, hook_url) -> None:
if name == "Semester-GPA":
continue
print(f"I: New grades for {name}")
notify_server(name, config.hook_url)
notify_server(name, config["hook_url"])

table.to_csv("grades.csv")
else:
Expand Down
5 changes: 5 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
echo "Starting notifier..."

printenv | grep -v "no_proxy" >> /etc/environment
cron -f
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
requests==2.32.3
pandas==2.2.2
lxml==5.3.0

0 comments on commit 3db0032

Please sign in to comment.