bjornregnell / kapten-alloc-web

Find your slot in space and time.
https://cs.lth.se/pgk/kaptenalloc
39 stars 6 forks source link

[Feature request] Generate iCal link from currently shown schedule #3

Closed DagHemberg closed 1 year ago

DagHemberg commented 3 years ago

Since this project was made using scalajs, it should be possible to add a button that uses https://www.npmjs.com/package/ical-generator to generate iCal events for the events that are currently shown on KaptenAlloc in order to then serve those on a link that can be copied into various different calendar applications, similar to what you can do on TimeEdit.

I've made a proof-of-concept myself using pure js (unfortunately on my desktop computer, meaning I can't share the code right now since I don't have access to it at the moment), which did work, but since I'm not very versed in web development I wasn't able to generate the copy-paste-able link. Simply using the http.createServer(...).listen(...) feature listed on the npm package link creates a link that downloads the ical file and adds everything in it as separate events, meaning any modifications have to be made to all of the events separately.

bjornregnell commented 3 years ago

Would be convenient with some kind of simple Timeedit integration. @theolundqvist had the same idea, perhaps you two could discuss together with focus on simplest possible viable solution?

bjornregnell commented 3 years ago

Perhaps we could have another route kaptenalloc/timeedit so we dont even need buttons?

DagHemberg commented 3 years ago

I've never worked with the TimeEdit API before, so this was the simplest thing I could come up with. I wasn't able to test it fully, but in theory it shouldn't require any major modifications in order to be integrated into Kapten Alloc.

// Kräver att dessa 2 paket är installerade (via typ browserify? vet inte riktigt hur man gör med node på webben):
//
// csv2json: https://www.npmjs.com/package/csvjson-csv2json
//
// ical-generator: https://www.npmjs.com/package/ical-generator
// vilken även inkluderar dokumentation via https://sebbo2002.github.io/ical-generator/develop/reference/

import ical from 'ical-generator'
const csv2json = require('csvjson-csv2json')

// temp för node.js
import * as fs from 'fs'

interface TAEvent {
    "kurs": string,
    "datum": string,
    "dag": string,
    "kl": string,
    "typ": string,
    "grupp": string,
    "rum": string,
    "handledare": string
}

function getText() {
    // templösning för node, behöver ändras för användning i webbläsare
    return fs.readFileSync('./events.txt', 'utf8')
}

function getSearchString() {
    // templösning för node
    return "DHe"
}

// regexen kan behöva modifieras på webbläsare / andra operativsystem, windows inkluderar \r vid radbrytningar men tror inget annat gör så 
const formattedCSV = getText().replace(/(-{2,}\r\n)| /g, "").replace(/\|/g, ";")
const allEvents: TAEvent[] = csv2json(formattedCSV)

const filterString = getSearchString()
const calendar = ical({name: `Tillfällen ${filterString}`})
const filteredEvents = allEvents.filter(function (event) {
    var values: string[] = []
    for (let [key, val] of Object.entries(event)) values.push(val)
    return values.some(value => value.match(filterString))
})

filteredEvents.forEach(event =>
    {
        const date = new Date(`${event.datum} ${event.kl}`)
        calendar.createEvent({
            summary: `${event.typ} ${event.kurs}`,
            start: date,
            end: new Date(date.getTime() + 6_300_000), //1h45min senare
            location: event.rum,
            description: `Grupp: ${event.grupp}
Handledare: ${event.handledare}
Sigrid: http://bjornix.cs.lth.se:8091/sigrid
SAM: https://sam.cs.lth.se/`
            // fler attribut?
        })
    }
)

// mer temp
console.log(calendar.events())

// nedan ska endast fungera i browsers enligt dokumentationen (blob-apin, vad det nu är, finns inte på node så kan inte testa exakt hur det funkar), men osäker på hur man går till väga då
// console.log(calendar.toURL())
stagrim commented 2 years ago

4 Adds feature to generate ICS files from selection

bjornregnell commented 2 years ago

@stagrim Is all fixed now so we can close this? @DagHemberg or what remains?

bjornregnell commented 1 year ago

closed by #4