aboutsummaryrefslogtreecommitdiff
path: root/public/javascripts/getUsers.js
blob: fb54659a2285e396d3ffdecdaa09254aceba1c09 (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
const Promise = require('bluebird')
const cheerio = require('cheerio')
const request = Promise.promisify(require('request'))

module.exports = function () {
  return new Promise((resolve, reject) => {
    request(`http://${window.location.host}/meetingpointProxy/Roosters-AL%2Fdoc%2Fdagroosters%2Fframes%2Fnavbar.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(regex => scriptText.match(regex)[1].split(',').map(item => item.replace(/"/g, '')))

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