Skip to content
Snippets Groups Projects
schedule.py 2.41 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")
rooms: list = [
    ('T01', 'tp', 0),
    ('T02', 'tp', 0),
Arch de Noé's avatar
Arch de Noé committed
    ('T03', 'tp', 4),
    ('T11', 'tp', 1),
    ('T20', 'tp', 2),
    ('T21', 'tp', 2),
    ('T22', 'tp', 2),
    ('T23', 'tp', 2),
    ('T24', 'tp', 2),
Arch de Noé's avatar
Arch de Noé committed
    ('C1', 'td', 0),
    ('C4', 'td', 0),
    ('C5', 'td', 0),
    ('C6', 'td', 0),
    ('C7', 'td', 0),
    ('C8', 'td', 0),
    ('C9', 'td', 0),
Arch de Noé's avatar
Arch de Noé committed
    ('C10', 'td', 0),
    ('C11', 'td', 1),
    ('C13', 'td', 1),
    ('C14', 'td', 1),
    ('C15', 'td', 1),
Noé Le Cam's avatar
Noé Le Cam committed
]


def main():
    cursor = database.cursor()
    cursor.execute("SELECT value FROM config WHERE key='upstream_url'")
    
    if ('init' in sys.argv):
        print("Importing schedule... (this may take a while)")
        import_schedule(cursor.fetchone()[0])
        print("Succefuly imported schedule")
Arch de Noé's avatar
Arch de Noé committed
    except :
        print("Couldn't import schedule", file=sys.stderr)
Arch de Noé's avatar
Arch de Noé committed
        sys.exit(1)
    
    sys.exit(0)
def import_schedule(url: str, reset: bool = True):
Noé Le Cam's avatar
Noé Le Cam committed
    cursor = database.cursor()
Arch de Noé's avatar
Arch de Noé committed
    print("Purging old schedule")
    if reset:
        cursor.execute("DELETE FROM schedule")

Arch de Noé's avatar
Arch de Noé committed
    print("Downloading new schedule")
Noé Le Cam's avatar
Noé Le Cam committed
    cal = Calendar(requests.get(url, allow_redirects=True).text)
Arch de Noé's avatar
Arch de Noé committed

    print("Parsing downloaded schedule")
Noé Le Cam's avatar
Noé Le Cam committed
    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]

Arch de Noé's avatar
Arch de Noé committed
    print("Saving parsed schedule")
    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()