From 47bb6585d1e7f97907c2a1797bb0b6b4782a1428 Mon Sep 17 00:00:00 2001 From: Valentijn Date: Wed, 18 Mar 2026 19:57:30 +0100 Subject: [PATCH] added comments --- API.py | 1 + DB.py | 4 ++++ PMT.py | 22 +++++++++++++++++----- main.py | 9 +++++++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/API.py b/API.py index e69de29..f87f5c1 100644 --- a/API.py +++ b/API.py @@ -0,0 +1 @@ +# TODO \ No newline at end of file diff --git a/DB.py b/DB.py index 429328b..c463752 100644 --- a/DB.py +++ b/DB.py @@ -9,6 +9,7 @@ class Database: def _setup_tables(self): + # create the shifts table. cur = self._con.cursor() cur.execute(''' CREATE TABLE IF NOT EXISTS shifts ( @@ -27,10 +28,12 @@ class Database: def insert_shifts(self, shifts): + # start db connection cur = self._con.cursor() inserted = 0 for shift in shifts: + # parse all information from the shift start = shift.get('start_datetime', 'N/A') end = shift.get('end_datetime', 'N/A') @@ -38,6 +41,7 @@ class Database: duration = shift.get('duration', 'N/A') description = shift.get('shift_remark', shift.get('description', 'N/A')) + # insert it into the db. cur.execute(""" INSERT OR IGNORE INTO shifts (shift_start, shift_end, department, duration, description) diff --git a/PMT.py b/PMT.py index 062d721..b61a2b0 100644 --- a/PMT.py +++ b/PMT.py @@ -22,12 +22,13 @@ class PMT: return target_monday.strftime("%Y-%m-%d") def login(self): + self.session = requests.Session() login_url = f"{self.base_url}/login" resp = self.session.get(login_url) - # Step 2: SSO login POST + # SSO login POST sso_data = { "username": self._username, "password": self._password, @@ -43,22 +44,29 @@ class PMT: json=sso_data, headers={'Content-Type': 'application/json'}) + + # Check if it succeeded. if sso_resp.status_code != 200 or not sso_resp.json().get('result', {}).get('authenticated'): print("Login failed!") print(sso_resp.text) sys.exit(1) + # Store the useful information in some class fields login_data = sso_resp.json()['result'] self.context_token = login_data['context_token'] self.user_token = login_data['user_token'] - print(f"āœ… Logged in as {self._username} (store AH 8541 Rhoon)") + # return session info + print(f"āœ… Logged in as {self._username}") return self.session, self.context_token, self.user_token def get_shifts(self, days_ahead=14): + # raise if not logged in. if not self.session or not self.context_token or not self.user_token: raise ValueError("Must call login() first!") - + + + # define date ranges shifts = [] from_date = datetime.now().strftime("%Y-%m-%d") @@ -66,6 +74,7 @@ class PMT: print(f"Fetching shifts from {from_date} to {to_date}...") + # send request for shifts headers = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0', 'Accept': 'application/json, text/plain, */*', @@ -88,7 +97,9 @@ class PMT: } resp = self.session.get(shifts_url, headers=headers, params=params) - + + + # check if request succeeded if resp.status_code == 200: data = resp.json() week_shifts = data.get('result', {}).get('shift_instances', []) @@ -98,6 +109,7 @@ class PMT: print(f"āŒ Failed: {resp.status_code}") print(resp.text[:500]) + # return all shifts fetched return shifts @classmethod @@ -106,7 +118,7 @@ class PMT: print("No upcoming shifts found.") return - print("\nšŸ“… UPCOMING SHIFTS:\n") + print("\nUPCOMING SHIFTS:\n") print("Date | Time | Dept | Duration | Description") print("-" * 80) diff --git a/main.py b/main.py index 1f7c151..3ebe55d 100644 --- a/main.py +++ b/main.py @@ -4,21 +4,26 @@ from dotenv import load_dotenv import os def main(): + # Load the environment load_dotenv(override=True) + + # Set up database, webserver and PMT connection db = Database() pmt = PMT() + # fetch PMT shifts pmt.login() days_ahead = int(os.environ.get("DAYS_TO_FETCH")) shifts = pmt.get_shifts(days_ahead) PMT.print_shifts(shifts) + + # Insert those shifts into the database db.insert_shifts(shifts) - print(f"\nFound {len(shifts)} shifts total.") - + if __name__ == "__main__": main()