aboutsummaryrefslogtreecommitdiff
path: root/lib/getUserIndex.js
blob: 07b914a0e62b8e8627703462af4f0f172a6a0a00 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict'

const Promise = require('bluebird')
const cheerio = require('cheerio')
const request = Promise.promisify(require('request'))
const getURLOfUsers = require('../lib/getURLOfUsers')
const encoding = require('encoding')

exports = {}
module.exports = exports

function getStandardUsers () {
  return new Promise(function (resolve, reject) {
    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, '')
        })
      })

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

function addExtendedUsers (standardUsers) {
  const standardUsersLength = standardUsers.length
  for (let i = 0; i < standardUsersLength; i++) {
  // for (let i = 200; i < 220; i++) {
    request(getURLOfUsers(0, standardUsers[i].type, standardUsers[i].index + 1))
    .then(page => page.body)
    .then(page => encoding.convert(page, 'UTF-8', 'Windows-1252'))
    .then(function (page) {
      const $ = cheerio.load(page)
      let extendedName = $('font').eq(2).text().trim()

      if (standardUsers[i].type === 's' || standardUsers[i].type === 't') {
        extendedName = extendedName.split(/\s+/).reverse().join(' ')
      }

      if (extendedName.indexOf(standardUsers[i].value) === -1 &&
      extendedName.indexOf(standardUsers[i].value.substring(2, standardUsers[i].value.length))) {
        console.log(`added ${extendedName}: ${standardUsers[i].value}`)
        standardUsers[i].other = extendedName
        standardUsers.push({
          type: standardUsers[i].type,
          index: standardUsers[i].index,
          value: extendedName,
          other: standardUsers[i].value,
          isID: false
        })
      }
    })
  }
}

getStandardUsers().then(users => {
  addExtendedUsers(users)
  exports.users = users
})