Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@skyzh
Created January 17, 2020 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skyzh/f7f7bb3572c0b4a2b5e11cc30e69a8bd to your computer and use it in GitHub Desktop.
Save skyzh/f7f7bb3572c0b4a2b5e11cc30e69a8bd to your computer and use it in GitHub Desktop.
Generate .ics file from course table
const data = require('./data.json')
const debug = require('debug')('generator')
const _ = require('lodash')
const ics = require('ics')
const moment = require('moment')
const fs = require('fs')
const courses = data.kbList.filter(course => !course.ignore).map(course => ({
name: course.kcmc,
teacher: course.xm,
score: course.xf,
id: course.kch_id,
location: course.cdmc,
time: course.jcor,
day: course.xqj,
date: course.zcd
}))
debug(`${courses.length} courses loaded, generating calendar...`)
const time_at = ["8:00", "8:55", "10:00", "10:55", "12:00", "12:55", "14:00", "14:55", "16:00", "16:55", "18:00", "18:55", "19:35"]
const week_begin_at = "2020/02/24"
const parse_week_string = str => {
let res = str.match(/^(.*)-(.*)周$/)
if (res) {
return _.range(parseInt(res[1]), parseInt(res[2]) + 1)
} else {
return str.split(',').map(d => parseInt(_.dropRight(d, 1).join("")))
}
}
const events = []
courses.forEach(course => {
debug(`${course.id} ${course.name}`)
let _weeks = parse_week_string(course.date)
let _time = course.time.match(/^(.*)-(.*)$/)
let day = parseInt(course.day)
let time_start = parseInt(_time[1])
let time_end = parseInt(_time[2])
let message = ` ${course.location} ${course.teacher} ${course.score} 学分`
debug(message)
_weeks.forEach(week => {
let datetime_begin = moment(`${week_begin_at} ${time_at[time_start - 1]}`, "YYYY/MM/DD HH:mm:ss").add(week - 1, 'week').add(day - 1, 'day')
let datetime_end = moment(`${week_begin_at} ${time_at[time_end - 1]}`, "YYYY/MM/DD HH:mm:ss").add(week - 1, 'week').add(day - 1, 'day').add(45, 'minute')
debug(` 第 ${week} 周 ${datetime_begin}~${datetime_end}`)
const event = {
title: `${course.id} ${course.name}`,
start: datetime_begin.format('YYYY-M-D-HH-mm').split("-").map(d => parseInt(d)),
end: datetime_end.format('YYYY-M-D-HH-mm').split("-").map(d => parseInt(d)),
location: course.location,
description: `${course.id} ${course.name} 第${week}周 ${course.score}学分 ${course.teacher}`
}
events.push(event)
})
})
ics.createEvents(events, (err, res) => {
if (err) console.error(err);
fs.writeFileSync('result.ics', res);
debug("success")
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment