2020-12-21 14:59:49 +00:00
|
|
|
const moment = require('moment')
|
|
|
|
const nodeFetch = require('node-fetch')
|
|
|
|
const { CookieJar } = require('tough-cookie')
|
|
|
|
const fetchCookie = require('fetch-cookie/node-fetch')
|
2020-12-30 13:39:49 +00:00
|
|
|
const { writeFile } = require('fs/promises')
|
2020-12-21 14:59:49 +00:00
|
|
|
|
|
|
|
const init = require('./dist').default
|
|
|
|
|
|
|
|
const [, , personalNumber] = process.argv
|
|
|
|
|
|
|
|
if (!personalNumber) {
|
|
|
|
console.error('You must pass in a valid personal number, eg `node run 197001011111`')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2020-12-30 13:39:49 +00:00
|
|
|
const record = async (info, data) => {
|
|
|
|
const filename = `./record/${info.name}.json`
|
|
|
|
const content = {
|
|
|
|
url: info.url,
|
|
|
|
headers: info.headers,
|
|
|
|
status: info.status,
|
|
|
|
statusText: info.statusText,
|
|
|
|
}
|
|
|
|
switch (info.type) {
|
|
|
|
case 'json':
|
|
|
|
content.json = data
|
|
|
|
break
|
|
|
|
case 'text':
|
|
|
|
content.text = data
|
|
|
|
break
|
|
|
|
case 'blob':
|
|
|
|
const buffer = await data.arrayBuffer()
|
|
|
|
content.blob = Buffer.from(buffer).toString('base64')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
await writeFile(filename, JSON.stringify(content, null, 2))
|
|
|
|
}
|
|
|
|
|
2020-12-21 14:59:49 +00:00
|
|
|
async function run() {
|
|
|
|
const cookieJar = new CookieJar()
|
|
|
|
const fetch = fetchCookie(nodeFetch, cookieJar)
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
2020-12-30 13:39:49 +00:00
|
|
|
const api = init(fetch, () => cookieJar.removeAllCookies(), { record })
|
2020-12-21 14:59:49 +00:00
|
|
|
const status = await api.login(personalNumber)
|
|
|
|
status.on('PENDING', () => console.log('PENDING'))
|
|
|
|
status.on('USER_SIGN', () => console.log('USER_SIGN'))
|
|
|
|
status.on('ERROR', () => console.error('ERROR'))
|
|
|
|
status.on('OK', () => console.log('OK'))
|
|
|
|
|
|
|
|
api.on('login', async () => {
|
|
|
|
console.log('Logged in')
|
|
|
|
|
2020-12-21 19:09:21 +00:00
|
|
|
console.log(api.getSessionCookie())
|
|
|
|
|
2020-12-30 13:39:49 +00:00
|
|
|
console.log('user')
|
|
|
|
const user = await api.getUser()
|
2020-12-21 17:55:38 +00:00
|
|
|
// console.log(user)
|
2020-12-21 16:01:22 +00:00
|
|
|
|
2020-12-21 17:55:38 +00:00
|
|
|
console.log('children')
|
|
|
|
const children = await api.getChildren()
|
2020-12-21 15:32:02 +00:00
|
|
|
// console.log(children)
|
2020-12-21 14:59:49 +00:00
|
|
|
|
2020-12-30 13:39:49 +00:00
|
|
|
console.log('calendar')
|
|
|
|
const calendar = await api.getCalendar(children[0])
|
2020-12-21 15:32:02 +00:00
|
|
|
// console.log(calendar)
|
2020-12-21 14:59:49 +00:00
|
|
|
|
2020-12-30 13:39:49 +00:00
|
|
|
console.log('classmates')
|
|
|
|
const classmates = await api.getClassmates(children[0])
|
2020-12-21 15:32:02 +00:00
|
|
|
// console.log(classmates)
|
2020-12-21 14:59:49 +00:00
|
|
|
|
2020-12-30 13:39:49 +00:00
|
|
|
console.log('schedule')
|
|
|
|
const schedule = await api.getSchedule(children[0], moment().subtract(1, 'week'), moment())
|
2020-12-21 15:32:02 +00:00
|
|
|
// console.log(schedule)
|
2020-12-21 14:59:49 +00:00
|
|
|
|
2020-12-21 18:48:50 +00:00
|
|
|
console.log('news')
|
|
|
|
const news = await api.getNews(children[0])
|
2020-12-30 13:39:49 +00:00
|
|
|
// console.log(news)
|
2020-12-21 18:48:50 +00:00
|
|
|
|
2020-12-30 13:39:49 +00:00
|
|
|
console.log('image')
|
|
|
|
const blob = await api.getImage(news[0].imageUrl)
|
2020-12-21 19:09:21 +00:00
|
|
|
// console.log(blob)
|
2020-12-21 18:48:50 +00:00
|
|
|
|
|
|
|
// const arrayBuffer = await blob.arrayBuffer()
|
|
|
|
// console.log(`data:${blob.type};base64,${Buffer.from(arrayBuffer).toString('base64')}`)
|
2020-12-21 15:32:02 +00:00
|
|
|
|
2020-12-30 13:39:49 +00:00
|
|
|
console.log('menu')
|
|
|
|
const menu = await api.getMenu(children[0])
|
2020-12-21 16:01:22 +00:00
|
|
|
// console.log(menu)
|
2020-12-21 14:59:49 +00:00
|
|
|
|
2020-12-30 13:39:49 +00:00
|
|
|
console.log('notifications')
|
|
|
|
const notifications = await api.getNotifications(children[0])
|
2020-12-21 18:48:50 +00:00
|
|
|
// console.log(notifications)
|
2020-12-21 17:55:38 +00:00
|
|
|
|
2020-12-21 14:59:49 +00:00
|
|
|
await api.logout()
|
|
|
|
})
|
|
|
|
|
|
|
|
api.on('logout', () => {
|
|
|
|
console.log('Logged out')
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run()
|