Skip to content

Commit

Permalink
Merge pull request #145 from kolplattformen/fix/remove-errors-from-sc…
Browse files Browse the repository at this point in the history
…hema

fix: 🐛 Remove errors when fetching schedule and timetable
  • Loading branch information
viktorlarsson authored Sep 13, 2021
2 parents befb073 + 1e944ad commit db19d78
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
35 changes: 30 additions & 5 deletions devrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* - Support for proxy (i recommend Burp Suite https://portswigger.net/burp/communitydownload)
* - Saves sessionCoookie to a file and tries to use it again
*/

const { DateTime } = require('luxon')
const { inspect } = require('util')
const nodeFetch = require('node-fetch')
const { CookieJar } = require('tough-cookie')
const fetchCookie = require('fetch-cookie/node-fetch')
Expand All @@ -18,6 +21,7 @@ const HttpProxyAgent = require('https-proxy-agent')
const agentWrapper = require('./agentFetchWrapper')
const init = require('./dist').default


const [, , personalNumber] = process.argv
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
const cookieJar = new CookieJar()
Expand Down Expand Up @@ -61,11 +65,32 @@ async function run() {
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)
*/
try {
console.log('schedule')
const schedule = await api.getSchedule(children[1], DateTime.local(), DateTime.local().plus({ week: 1 }))
console.log(schedule)
} catch (error) {
console.error(error)
}

let skola24children
try {
skola24children = await api.getSkola24Children()
console.log(skola24children)
} catch (error) {
console.error(error)
}

try {
console.log('timetable')
const timetable = await api.getTimetable(skola24children[0], 15, 2021, "sv")
console.log(inspect(timetable, false, 1000, true))
} catch (error) {
console.error(error)
}

/*
console.log('news')
const news = await api.getNews(children[0])
*/
Expand Down
10 changes: 8 additions & 2 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
Skola24Child,
EtjanstChild,
SSOSystem,
TimetableEntry
} from './types'
import * as routes from './routes'
import * as parse from './parse/index'
Expand Down Expand Up @@ -259,7 +260,7 @@ export class Api extends EventEmitter {
): Promise<ScheduleItem[]> {
if (this.isFake) return fakeResponse(fake.schedule(child))

const url = routes.schedule(child.sdsId, from.toISODate(), to.toISODate())
const url = routes.schedule(child.id, from.toISODate(), to.toISODate())
const session = this.getRequestInit()
const response = await this.fetch('schedule', url, session)
const data = await response.json()
Expand Down Expand Up @@ -416,8 +417,13 @@ export class Api extends EventEmitter {
return key as string
}

public async getTimetable(child: Skola24Child, week: number, year: number, lang: Language): Promise<any> {
public async getTimetable(child: Skola24Child, week: number, year: number, lang: Language)
: Promise<TimetableEntry[]> {
if (this.isFake) return fakeResponse(fake.timetable(child))

if(!child.timetableID) {
return new Array<TimetableEntry>()
}

const url = routes.timetable
const renderKey = await this.getRenderKey()
Expand Down
20 changes: 18 additions & 2 deletions lib/parse/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,21 @@ export const scheduleItem = ({
oneDayEvent: isSameDay,
})

export const schedule = (data: any): ScheduleItem[] =>
etjanst(data).map(scheduleItem)
export const schedule = (data: any): ScheduleItem[] => {
try{
const scheduleData = etjanst(data)
const mapped = scheduleData.map(scheduleItem)
return mapped
}
catch(e){
if (e instanceof Error) {
// If this happens the child has no schedule
// It is the same on the official web
// Instead of retrying and spamming errors - lets return en empty array
if(e.message === 'A task was canceled.'){
return new Array<ScheduleItem>()
}
}
throw e
}
}

0 comments on commit db19d78

Please sign in to comment.