-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrun.js
158 lines (136 loc) · 4.25 KB
/
run.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function requestLogger(httpModule) {
var original = httpModule.request
httpModule.request = function (options, callback) {
console.log('-----------------------------------------------')
console.log(
options.href || options.proto + '://' + options.host + options.path,
options.method
)
console.log(options.headers)
console.log('-----------------------------------------------')
return original(options, callback)
}
}
requestLogger(require('http'))
requestLogger(require('https'))
const { DateTime } = require('luxon')
const nodeFetch = require('node-fetch')
const { CookieJar } = require('tough-cookie')
const fetchCookie = require('fetch-cookie/node-fetch')
const { writeFile } = require('fs/promises')
const path = require('path')
const fs = require('fs')
const { inspect } = require('util')
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)
}
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath)
if (fs.existsSync(dirname)) {
return true
}
ensureDirectoryExistence(dirname)
fs.mkdirSync(dirname)
}
const record = async (info, data) => {
const name = info.error ? `${info.name}_error` : info.name
const filename = `./record/${name}.json`
ensureDirectoryExistence(filename)
const content = {
url: info.url,
headers: info.headers,
status: info.status,
statusText: info.statusText,
}
if (data) {
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
}
} else if (info.error) {
const { message, stack } = info.error
content.error = {
message,
stack,
}
}
await writeFile(filename, JSON.stringify(content, null, 2))
}
async function run() {
const cookieJar = new CookieJar()
const fetch = fetchCookie(nodeFetch, cookieJar)
try {
const api = init(fetch, cookieJar, { record })
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'))
status.on('CANCELLED', () => {
console.log('User cancelled login')
process.exit(0)
})
api.on('login', async () => {
console.log('Logged in')
// console.log('user')
// const user = await api.getUser()
// console.log(user)
console.log('children')
const children = await api.getChildren()
console.log(children)
console.log('calendar')
const calendar = await api.getCalendar(children[0])
console.log(calendar)
/*
console.log('classmates')
const classmates = await api.getClassmates(children[0])
console.log(classmates)
console.log('schedule')
const schedule = await api.getSchedule(children[0], DateTime.local(), DateTime.local().plus({ week: 1 }))
console.log(schedule)
console.log('news')
const news = await api.getNews(children[0])
*/
/* console.log('news details')
const newsItems = await Promise.all(
news.map((newsItem) =>
api.getNewsDetails(children[0], newsItem)
.catch((err) => { console.error(newsItem.id, err) })
)
)
console.log(newsItems) */
/* console.log('menu')
const menu = await api.getMenu(children[0])
console.log(menu) */
// console.log('notifications')
// const notifications = await api.getNotifications(children[0])
// console.log(notifications)
const skola24children = await api.getSkola24Children()
console.log(skola24children)
console.log('timetable')
const timetable = await api.getTimetable(skola24children[0], 15, 2021)
console.log(inspect(timetable, false, 1000, true))
await api.logout()
})
api.on('logout', () => {
console.log('Logged out')
process.exit(0)
})
} catch (err) {
console.error(err)
}
}
run()