Browse Source

work.

master
Yorick van Pelt 4 years ago
parent
commit
c36f8019b1
  1. 65
      index.js
  2. 5
      package.json

65
index.js

@ -1,11 +1,58 @@
var request = require('request');
var cheerio = require('cheerio');
"use strict"
const cheerio = require('cheerio');
const ical = require('ical-generator')
const rp = require('request-promise');
request('http://www.ru.nl/radboudreflects/agenda/aankomende-verdiepende-lezingen-debatten-iedereen/', function (error, response, html) {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
Array.from($(".ipx-pt-evenement.type-entry")).forEach(evt => {
function translate_date(datestr) {
// 13 januari 2013 -> 2013,0,13
let [d,m,y] = datestr.split(' ')
d = +d; y = +y
m = "januari februari maart april mei juni juli augustus september oktober november december".split(' ').indexOf(m.toLowerCase())
return new Date(y, m, d)
}
function pprint_map(x) {
return Array.from(x.entries()).map(([k,v]) => `${k}: ${v}`).join('\n')
}
rp({uri: 'http://www.ru.nl/radboudreflects/agenda/openbare-lezingen/',
transform: cheerio.load.bind(cheerio)
})
.then(($) =>
Array.from($(".ipx-pt-evenement.type-entry")).map(evt => {
const title = $('h2', evt).text()
const url = $('h2 > a', evt).attr('href')
const fromDT = sel => new Map(Array.from(sel).map(x => [$(x).attr('class').split(' ')[1], $(x).text().trim()]))
const labels = fromDT($('dt.label', evt))
const values = fromDT($('dd.value', evt))
const uid = $(evt).attr('data-ipx-id')
//console.log(uid, values)
return ({labels, values, uid, title, url})
})
)
.then(events => {
const cal = ical({
domain: 'pub.yori.cc',
name: "Radboud Reflects Lezingen",
url: "https://pub.yori.cc/reflects.ical",
timezone: "Europe/Amsterdam",
})
events.forEach(event => {
const {values, url, title, uid} = event
const [stime, etime] = values.get('starttijd').split(' t/m ').map(x => x.split(':').map(y=>+y))
const start = translate_date(values.get('startdatum'))
start.setHours(...stime)
const end = new Date(start)
end.setHours(...etime)
if (end < start) end.setDate(end.getDate()+1)
const cal_evt = cal.createEvent({
summary: title,
uid, start, end, url,
location: values.get('locatie'),
description: `<a href="${url}">${values.get('type')} by ${values.get('spreker')}</a>
${pprint_map(values)}`,
})
cal_evt.htmlDescription(`<a href="${url}">${values.get('type')} by ${values.get('spreker')}</a>
${pprint_map(values)}`)
})
}
});
console.log(cal.toString())
})

5
package.json

@ -5,8 +5,9 @@
"main": "index.js",
"dependencies": {
"cheerio": "^1.0.0-rc.1",
"ical-generator": "^0.2.9",
"request": "^2.81.0"
"ical-generator": "^0.2.10",
"request": "^2.81.0",
"request-promise": "^4.2.2"
},
"devDependencies": {},
"scripts": {

Loading…
Cancel
Save