Skip to content
Snippets Groups Projects
schedule.py 1.78 KiB
Newer Older
Noé Le Cam's avatar
Noé Le Cam committed
import re
import requests
import sqlite3
from ics import Calendar

database = sqlite3.connect("assets/database.sqlite")
url: str = "https://adecons.unistra.fr/jsp/custom/modules/plannings/anonymous_cal.jsp?resources=5242,5435,5448&projectId=8&calType=ical&nbWeeks=4"

rooms: list = [
    ('T01', 'tp', 0),
    ('T02', 'tp', 0),
    ('T03', 'tp', 0),
    ('T11', 'tp', 1),
    ('T20', 'tp', 2),
    ('T21', 'tp', 2),
    ('T22', 'tp', 2),
    ('T23', 'tp', 2),
    ('T24', 'tp', 2),
    ('C9', 'td', 0),
    ('C10', 'td', 0)
Noé Le Cam's avatar
Noé Le Cam committed
]


def main():
    setup_tables()
Noé Le Cam's avatar
Noé Le Cam committed
    import_schedule()


def import_schedule(reset: bool = True):
Noé Le Cam's avatar
Noé Le Cam committed
    cursor = database.cursor()
    
    if reset:
        cursor.execute("DELETE FROM schedule")

Noé Le Cam's avatar
Noé Le Cam committed
    cal = Calendar(requests.get(url, allow_redirects=True).text)
    events = [(re.match('([A-Z]\d+)', e.location).group(1),
               e.begin.format("YYYY-MM-DD"),
Noé Le Cam's avatar
Noé Le Cam committed
               e.begin.shift(hours=+2).format("HH:mm"),
               e.end.shift(hours=+2).format("HH:mm"))

              for e in cal.events]

    cursor.executemany(
        "INSERT OR IGNORE INTO schedule VALUES (?,?,?,?)", events)
Noé Le Cam's avatar
Noé Le Cam committed
    database.commit()


def setup_tables():
Noé Le Cam's avatar
Noé Le Cam committed
    cursor = database.cursor()

    # Create 'rooms' table
Noé Le Cam's avatar
Noé Le Cam committed
    cursor.execute("""CREATE TABLE IF NOT EXISTS rooms (
        id TEXT PRIMARY KEY,
        type TEXT NOT NULL,
Noé Le Cam's avatar
Noé Le Cam committed
        location INTEGER NOT NULL
    )""")

    # Populate 'rooms' table
    cursor.executemany("INSERT OR IGNORE INTO rooms VALUES (?,?,?)", rooms)

    # Create 'schedule' table
Noé Le Cam's avatar
Noé Le Cam committed
    cursor.execute("""CREATE TABLE IF NOT EXISTS schedule (
        id TEXT NOT NULL,
        date TEXT NOT NULL,
        start_time TEXT NOT NULL,
        end_time TEXT NOT NULL,
        FOREIGN KEY(id) REFERENCES rooms(id)
    )""")

    database.commit()


if __name__ == "__main__":
    main()