Compare commits
14 Commits
47bb6585d1
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e4bd1bbc40 | |||
| 1fa8b21882 | |||
| 61f6070729 | |||
|
|
5f841ddbf9 | ||
|
|
11a91eb213 | ||
|
|
8d80985d79 | ||
|
|
069a2d2c63 | ||
|
|
243bdbb685 | ||
|
|
994aa1dab7 | ||
|
|
74e7f73c38 | ||
|
|
76cab3c296 | ||
|
|
79f9e99e74 | ||
|
|
48b5ff9bbc | ||
|
|
175390fda7 |
7
.dockerignore
Normal file
7
.dockerignore
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
*.pyo
|
||||||
|
*.pyd
|
||||||
|
.env
|
||||||
|
venv/
|
||||||
|
.git/
|
||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,4 @@
|
|||||||
.env
|
.env
|
||||||
shifts.db
|
shifts.db
|
||||||
|
|
||||||
|
__pycache__
|
||||||
162
API.py
162
API.py
@@ -1 +1,161 @@
|
|||||||
# TODO
|
from flask import Flask, Response
|
||||||
|
from icalendar import Calendar, Event
|
||||||
|
from datetime import datetime
|
||||||
|
import pytz
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
|
class Webserver:
|
||||||
|
def __init__(self, ip, port, db_instance):
|
||||||
|
self.ip = ip
|
||||||
|
self.port = port
|
||||||
|
self.db = db_instance
|
||||||
|
|
||||||
|
self.app = Flask(__name__)
|
||||||
|
self.tz = pytz.timezone("Europe/Amsterdam")
|
||||||
|
|
||||||
|
self._register_routes()
|
||||||
|
|
||||||
|
def _register_routes(self):
|
||||||
|
self.app.add_url_rule("/", "index", lambda: "Calendar server running ✅")
|
||||||
|
self.app.add_url_rule("/calendar.ics", "calendar", self._calendar_endpoint)
|
||||||
|
|
||||||
|
def _fetch_shifts(self):
|
||||||
|
con = self.db._get_connection()
|
||||||
|
cur = con.cursor()
|
||||||
|
|
||||||
|
cur.execute("""
|
||||||
|
SELECT shift_start, shift_end, department, description
|
||||||
|
FROM shifts
|
||||||
|
ORDER BY shift_start ASC
|
||||||
|
""")
|
||||||
|
|
||||||
|
rows = cur.fetchall()
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
shifts = []
|
||||||
|
for row in rows:
|
||||||
|
start_str, end_str, department, description = row
|
||||||
|
|
||||||
|
# Convert string → datetime
|
||||||
|
start = self._parse_datetime(start_str)
|
||||||
|
end = self._parse_datetime(end_str)
|
||||||
|
|
||||||
|
shifts.append({
|
||||||
|
"start": start,
|
||||||
|
"end": end,
|
||||||
|
"description": f"{description} ",
|
||||||
|
"department": f"{department} "
|
||||||
|
})
|
||||||
|
|
||||||
|
return shifts
|
||||||
|
|
||||||
|
def _parse_datetime(self, dt_str):
|
||||||
|
dt = datetime.fromisoformat(dt_str)
|
||||||
|
if dt.tzinfo is None:
|
||||||
|
dt = self.tz.localize(dt)
|
||||||
|
return dt
|
||||||
|
|
||||||
|
def _calendar_endpoint(self):
|
||||||
|
cal = Calendar()
|
||||||
|
cal.add('prodid', '-//Shift Calendar//example//')
|
||||||
|
cal.add('version', '2.0')
|
||||||
|
|
||||||
|
shifts = self._fetch_shifts()
|
||||||
|
|
||||||
|
for shift in shifts:
|
||||||
|
event = Event()
|
||||||
|
event.add('uid', str(uuid.uuid4()))
|
||||||
|
event.add('dtstart', shift["start"])
|
||||||
|
event.add('dtend', shift["end"])
|
||||||
|
event.add('summary', shift["department"])
|
||||||
|
event.add('description', shift["description"])
|
||||||
|
event.add('dtstamp', datetime.now(tz=self.tz))
|
||||||
|
|
||||||
|
cal.add_component(event)
|
||||||
|
|
||||||
|
return Response(cal.to_ical(), mimetype="text/calendar")
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
print(f"🚀 Starting server on {self.ip}:{self.port}")
|
||||||
|
from flask import Flask, Response
|
||||||
|
from icalendar import Calendar, Event
|
||||||
|
from datetime import datetime
|
||||||
|
import pytz
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
|
class Webserver:
|
||||||
|
def __init__(self, ip, port, db_instance):
|
||||||
|
self.ip = ip
|
||||||
|
self.port = port
|
||||||
|
self.db = db_instance
|
||||||
|
|
||||||
|
self.app = Flask(__name__)
|
||||||
|
self.tz = pytz.timezone("Europe/Amsterdam")
|
||||||
|
|
||||||
|
self._register_routes()
|
||||||
|
|
||||||
|
def _register_routes(self):
|
||||||
|
self.app.add_url_rule("/", "index", lambda: "Calendar server running ✅")
|
||||||
|
self.app.add_url_rule("/calendar.ics", "calendar", self._calendar_endpoint)
|
||||||
|
|
||||||
|
def _fetch_shifts(self):
|
||||||
|
con = self.db._get_connection()
|
||||||
|
cur = con.cursor()
|
||||||
|
|
||||||
|
cur.execute("""
|
||||||
|
SELECT shift_start, shift_end, department, description
|
||||||
|
FROM shifts
|
||||||
|
ORDER BY shift_start ASC
|
||||||
|
""")
|
||||||
|
|
||||||
|
rows = cur.fetchall()
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
shifts = []
|
||||||
|
for row in rows:
|
||||||
|
start_str, end_str, department, description = row
|
||||||
|
|
||||||
|
# Convert string → datetime
|
||||||
|
start = self._parse_datetime(start_str)
|
||||||
|
end = self._parse_datetime(end_str)
|
||||||
|
|
||||||
|
shifts.append({
|
||||||
|
"start": start,
|
||||||
|
"end": end,
|
||||||
|
"description": f"{description} ",
|
||||||
|
"department": f"{department} "
|
||||||
|
})
|
||||||
|
|
||||||
|
return shifts
|
||||||
|
|
||||||
|
def _parse_datetime(self, dt_str):
|
||||||
|
dt = datetime.fromisoformat(dt_str)
|
||||||
|
if dt.tzinfo is None:
|
||||||
|
dt = self.tz.localize(dt)
|
||||||
|
return dt
|
||||||
|
|
||||||
|
def _calendar_endpoint(self):
|
||||||
|
cal = Calendar()
|
||||||
|
cal.add('prodid', '-//Shift Calendar//example//')
|
||||||
|
cal.add('version', '2.0')
|
||||||
|
|
||||||
|
shifts = self._fetch_shifts()
|
||||||
|
|
||||||
|
for shift in shifts:
|
||||||
|
event = Event()
|
||||||
|
event.add('uid', str(uuid.uuid4()))
|
||||||
|
event.add('dtstart', shift["start"])
|
||||||
|
event.add('dtend', shift["end"])
|
||||||
|
event.add('summary', shift["department"])
|
||||||
|
event.add('description', shift["description"])
|
||||||
|
event.add('dtstamp', datetime.now(tz=self.tz))
|
||||||
|
|
||||||
|
cal.add_component(event)
|
||||||
|
|
||||||
|
return Response(cal.to_ical(), mimetype="text/calendar")
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
print(f"🚀 Starting server on {self.ip}:{self.port}")
|
||||||
|
self.app.run(host=self.ip, port=self.port, threaded=True)
|
||||||
25
DB.py
25
DB.py
@@ -3,14 +3,17 @@ import sqlite3
|
|||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._con = sqlite3.connect("shifts.db")
|
self.db_path = "data/shifts.db"
|
||||||
self._setup_tables()
|
self._setup_tables()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def _get_connection(self):
|
||||||
|
return sqlite3.connect(self.db_path)
|
||||||
|
|
||||||
def _setup_tables(self):
|
def _setup_tables(self):
|
||||||
# create the shifts table.
|
# create the shifts table.
|
||||||
cur = self._con.cursor()
|
con = self._get_connection()
|
||||||
|
cur = con.cursor()
|
||||||
cur.execute('''
|
cur.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS shifts (
|
CREATE TABLE IF NOT EXISTS shifts (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -24,12 +27,14 @@ class Database:
|
|||||||
UNIQUE(shift_start, department) -- No duplicates
|
UNIQUE(shift_start, department) -- No duplicates
|
||||||
);
|
);
|
||||||
''')
|
''')
|
||||||
self._con.commit()
|
con.commit()
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
|
||||||
def insert_shifts(self, shifts):
|
def insert_shifts(self, shifts):
|
||||||
# start db connection
|
# start db connection
|
||||||
cur = self._con.cursor()
|
con = self._get_connection()
|
||||||
|
cur = con.cursor()
|
||||||
inserted = 0
|
inserted = 0
|
||||||
|
|
||||||
for shift in shifts:
|
for shift in shifts:
|
||||||
@@ -51,6 +56,14 @@ class Database:
|
|||||||
if cur.rowcount > 0:
|
if cur.rowcount > 0:
|
||||||
inserted += 1
|
inserted += 1
|
||||||
|
|
||||||
self._con.commit()
|
con.commit()
|
||||||
|
con.close()
|
||||||
print(f"✅ Inserted {inserted}/{len(shifts)} new shifts")
|
print(f"✅ Inserted {inserted}/{len(shifts)} new shifts")
|
||||||
|
|
||||||
|
def delete_future_shifts(self):
|
||||||
|
con = self._get_connection()
|
||||||
|
cur = con.cursor()
|
||||||
|
cur.execute("DELETE FROM shifts WHERE shift_start > current_timestamp")
|
||||||
|
con.commit()
|
||||||
|
con.close()
|
||||||
|
print(f"✅ Deleted all future shifts")
|
||||||
|
|||||||
19
Dockerfile
Normal file
19
Dockerfile
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
FROM python:3.13-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get install -y \
|
||||||
|
gcc \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
CMD ["python", "main.py"]
|
||||||
Binary file not shown.
Binary file not shown.
17
docker-compose.yml
Normal file
17
docker-compose.yml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
services:
|
||||||
|
app:
|
||||||
|
build: .
|
||||||
|
container_name: pmt
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
restart: unless-stopped
|
||||||
|
networks:
|
||||||
|
- proxy-network
|
||||||
|
volumes:
|
||||||
|
- "./data:/app/data"
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
|
||||||
|
networks:
|
||||||
|
proxy-network:
|
||||||
|
external: true
|
||||||
46
main.py
46
main.py
@@ -1,7 +1,29 @@
|
|||||||
from PMT import PMT
|
from PMT import PMT
|
||||||
from DB import Database
|
from DB import Database
|
||||||
|
from API import Webserver
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from time import sleep
|
||||||
import os
|
import os
|
||||||
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
|
|
||||||
|
def update_shifts(pmt, db):
|
||||||
|
try:
|
||||||
|
print("🔄 Updating shifts...")
|
||||||
|
|
||||||
|
pmt.login()
|
||||||
|
|
||||||
|
days_ahead = int(os.environ.get("DAYS_TO_FETCH", 14))
|
||||||
|
shifts = pmt.get_shifts(days_ahead)
|
||||||
|
|
||||||
|
db.delete_future_shifts()
|
||||||
|
db.insert_shifts(shifts)
|
||||||
|
|
||||||
|
print("✅ Shift update complete")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error updating shifts: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Load the environment
|
# Load the environment
|
||||||
@@ -11,19 +33,23 @@ def main():
|
|||||||
# Set up database, webserver and PMT connection
|
# Set up database, webserver and PMT connection
|
||||||
db = Database()
|
db = Database()
|
||||||
pmt = PMT()
|
pmt = PMT()
|
||||||
|
webserver = Webserver("0.0.0.0", 8080, db)
|
||||||
|
|
||||||
|
# update the shifts on boot
|
||||||
|
update_shifts(pmt, db)
|
||||||
|
|
||||||
|
|
||||||
# fetch PMT shifts
|
# Every hour at minute 0
|
||||||
pmt.login()
|
scheduler = BackgroundScheduler(timezone="Europe/Amsterdam")
|
||||||
|
scheduler.add_job(
|
||||||
|
lambda: update_shifts(pmt, db),
|
||||||
|
trigger="interval",
|
||||||
|
hours=1,
|
||||||
|
next_run_time=datetime.now()
|
||||||
|
)
|
||||||
|
scheduler.start()
|
||||||
|
|
||||||
days_ahead = int(os.environ.get("DAYS_TO_FETCH"))
|
webserver.run()
|
||||||
shifts = pmt.get_shifts(days_ahead)
|
|
||||||
PMT.print_shifts(shifts)
|
|
||||||
|
|
||||||
|
|
||||||
# Insert those shifts into the database
|
|
||||||
db.insert_shifts(shifts)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
6
requirements.txt
Normal file
6
requirements.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
Flask
|
||||||
|
icalendar
|
||||||
|
pytz
|
||||||
|
python-dotenv
|
||||||
|
APScheduler
|
||||||
|
requests
|
||||||
Reference in New Issue
Block a user