feat: login and get children

This commit is contained in:
William Ryder 2023-09-22 11:00:37 +02:00
parent 63dc2eb664
commit 3f469de3d5
5 changed files with 128 additions and 21 deletions

View File

@ -24,7 +24,7 @@ const init = isAdmentum ? initAdmentum : initSkolplattformen
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
const cookieJar = new CookieJar()
let bankIdUsed = false
let bankIdUsed = true
const recordFolder = `${__dirname}/record`
async function run() {
@ -45,12 +45,12 @@ async function run() {
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')
/*console.log('classmates')
const classmates = await api.getClassmates(children[0])
console.log(classmates)

View File

@ -144,7 +144,7 @@ export class ApiAdmentum extends EventEmitter implements Api {
async getUser(): Promise<User> {
console.log('fetching user')
const userId = '437302'
const userId = '437236'
const currentUserResponse = await this.fetch(
'current-user',
apiUrls.user(userId)
@ -162,11 +162,10 @@ export class ApiAdmentum extends EventEmitter implements Api {
if (!this.isLoggedIn) {
throw new Error('Not logged in...')
}
console.log("get no children")
return []
const testUserId = '436838'
console.log("get children")
const testUserId = '437236'
const fetchUrl = apiUrls.user(testUserId)
console.log('v3.3 fetching children for user id', testUserId, 'from', fetchUrl)
console.log('v3.4 fetching children for user id', testUserId, 'from', fetchUrl)
const currentUserResponse = await this.fetch('current-user', fetchUrl, {
method: 'GET',
headers: {
@ -174,7 +173,6 @@ export class ApiAdmentum extends EventEmitter implements Api {
},
})
if (currentUserResponse.status !== 200) {
console.error('Error headers', currentUserResponse.headers)
throw new Error('Could not fetch children. Response code: ' + currentUserResponse.status)
@ -191,7 +189,15 @@ export class ApiAdmentum extends EventEmitter implements Api {
}
async getCalendar(child: EtjanstChild): Promise<CalendarItem[]> {
return Promise.resolve([])
if (!this.isLoggedIn) {
throw new Error('Not logged in...')
}
const [year, week] = new DateTime().toISOWeekDate().split('-')
const isoWeek = week.replace('W','')
const fetchUrl = apiUrls.schedule(year, isoWeek)
const calendarResponse = await this.fetch('get-calendar', fetchUrl)
return calendarResponse.map(parseCalendarItem)
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars

View File

@ -46,13 +46,113 @@ export function extractAuthGbgLoginRequestBody(signatureResponseText: string) {
return authGbgLoginBody
}
export const parseCalendarItem = (x: html.HTMLElement): { id: number; title: string; startDate: string; endDate: string } => {
const info = Array.from(x.querySelectorAll('a'))
// TODO: the identifier is realy on this format: '\d+:\d+' currently we only take the first part so Id will clash between items
const id = info[0].getAttribute("onClick")?.replace(new RegExp("return viewEvent\\('(\\d+).+"), "$1") || NaN
const day = info[1].textContent
const timeSpan = info[2].textContent
const [startTime, endTime] = timeSpan.replace(".", ":").split("-")
export const parseCalendarItem = (jsonRow: any): any => {
return {}
}
return { id: +id, title: info[0].textContent, startDate: `${day} ${startTime}`, endDate: `${day} ${endTime}` }
}
/*
{
"week_number": 40,
"days": [
{
"date": "2023-10-02",
"formated_date": "2 okt",
"name": "Måndag",
"lessons": [
{
"title": "BI",
"tooltip_title": "Biologi",
"subject_name": "Biologi",
"subject_code": "BI",
"teachers": "FCa",
"intervals": 3.0,
"overlaps": 1,
"start_pos": 4.0,
"color": "#e97f23",
"time": "10:00 - 11:30",
"room": "",
"groups": "6 A BI",
"tooltip": "10:00 - 11:30<br>6 A BI",
"lesson_id": 14998270,
"lesson_info": "",
"lesson_groups": "6 A BI",
"body": "BI",
"information": null
}
],
"breaks": [],
"events": []
},
{
"date": "2023-10-03",
"formated_date": "3 okt",
"name": "Tisdag",
"lessons": [],
"breaks": [],
"events": []
},
{
"date": "2023-10-04",
"formated_date": "4 okt",
"name": "Onsdag",
"lessons": [],
"breaks": [],
"events": []
},
{
"date": "2023-10-05",
"formated_date": "5 okt",
"name": "Torsdag",
"lessons": [],
"breaks": [],
"events": []
},
{
"date": "2023-10-06",
"formated_date": "6 okt",
"name": "Fredag",
"lessons": [],
"breaks": [],
"events": []
}
],
"query": "week=40&user_id=437235",
"time_range": [
"8:00",
"8:30",
"9:00",
"9:30",
"10:00",
"10:30",
"11:00",
"11:30",
"12:00",
"12:30",
"13:00",
"13:30",
"14:00",
"14:30",
"15:00",
"15:30",
"16:00",
"16:30",
"17:00"
],
"section_count": 18,
"breaks": [],
"schedule_event_instances": [],
"schedule": {
"id": 4385,
"start_week": 31,
"start_year": 2023,
"end_week": 22,
"end_year": 2024
},
"next_week": 41,
"prev_week": 39,
"weeks_amount": 52,
"break_week": 27
}
*/

View File

@ -33,7 +33,8 @@ export const apiUrls = {
baseUrl + 'schedule_group_teacher_enrollments',
schedule_groups: baseUrl + 'schedule_groups',
schedules: baseUrl + 'schedules',
school_enrollments: baseUrl + 'school_enrollments',
schedule: (year: string, week: string) => baseUrl + `schedule?week=${week}&year=${year}`,
school_enrollments: `${baseUrl}school_enrollments`,
school_years: baseUrl + 'school_years',
schools: baseUrl + 'schools',
sickness: baseUrl + 'sickness',

View File

@ -7,7 +7,7 @@
"dist/**/*"
],
"repository": "git@github.com:kolplattformen/skolplattformen.git",
"author": "Erik Eng <erik@eng.se>",
"author": "Christian Landgren, William Ryder <info@iteam.se>",
"license": "Apache-2.0",
"private": false,
"scripts": {