aboutsummaryrefslogtreecommitdiff
path: root/lib/getUserIndex.js
blob: 6288dc3bb915df0d0f41c702a08ad6b85c82ca78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict'

const Promise = require('bluebird')
const cheerio = require('cheerio')
const request = Promise.promisify(require('request'))

function getUserIndex () {
  return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`)
    .then(function (page) {
      page = page.body

      const $ = cheerio.load(page)
      const $script = $('script').eq(1)
      const scriptText = $script.text()

      const regexs = [/var classes = \[(.+)];/, /var teachers = \[(.+)];/, /var rooms = \[(.+)];/, /var students = \[(.+)];/]
      const items = regexs.map(function (regex) {
        return scriptText.match(regex)[1].split(',').map(function (item) {
          return item.replace(/"/g, '')
        })
      })

      return ([]
      .concat(items[0].map(function (item, index) {
        return {
          type: 'c',
          value: item,
          index: index
        }
      }))
      .concat(items[1].map(function (item, index) {
        return {
          type: 't',
          value: item,
          index: index
        }
      }))
      .concat(items[2].map(function (item, index) {
        return {
          type: 'r',
          value: item,
          index: index
        }
      }))
      .concat(items[3].map(function (item, index) {
        return {
          type: 's',
          value: item,
          index: index
        }
      })))
    })
}

module.exports = getUserIndex