From 47643d670aa4020ac7cc41b985c7ae8f72b9ae96 Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Fri, 10 Sep 2021 22:49:01 +0200 Subject: [PATCH 1/4] If child has no timetableId they have no timetable and we should not ask server for one --- lib/api.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/api.ts b/lib/api.ts index 8c964cc..fec7a8c 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -416,8 +416,13 @@ export class Api extends EventEmitter { return key as string } - public async getTimetable(child: Skola24Child, week: number, year: number, lang: Language): Promise { + public async getTimetable(child: Skola24Child, week: number, year: number, lang: Language) + : Promise { if (this.isFake) return fakeResponse(fake.timetable(child)) + + if(!child.timetableID) { + return new Array() + } const url = routes.timetable const renderKey = await this.getRenderKey() From 5f5b467802c6564d7cd65408cb1e9ea1ff9700de Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Fri, 10 Sep 2021 22:49:52 +0200 Subject: [PATCH 2/4] Fix id used in request It always returned an error --- lib/api.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/api.ts b/lib/api.ts index fec7a8c..3176487 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -20,6 +20,7 @@ import { Skola24Child, EtjanstChild, SSOSystem, + TimetableEntry } from './types' import * as routes from './routes' import * as parse from './parse/index' @@ -259,7 +260,7 @@ export class Api extends EventEmitter { ): Promise { 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() From b88b460bd6a8336dc8eead6346cc9643a750a522 Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Fri, 10 Sep 2021 22:51:27 +0200 Subject: [PATCH 3/4] Update devrun to support schedule and timetable --- devrun.js | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/devrun.js b/devrun.js index 2bfb8db..05897db 100644 --- a/devrun.js +++ b/devrun.js @@ -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') @@ -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() @@ -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]) */ From 1e944adf24482fef472174d14c9d76fc9e3bf78b Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Sat, 11 Sep 2021 13:49:26 +0200 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Return=20a=20empty=20?= =?UTF-8?q?array=20if=20backend=20returns=20a=20specific=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If this error it returned from backend a child has no schedule. Better to return an empty array than spamming the log with errors. --- lib/parse/schedule.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/parse/schedule.ts b/lib/parse/schedule.ts index bb3fdbe..93f1593 100644 --- a/lib/parse/schedule.ts +++ b/lib/parse/schedule.ts @@ -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() + } + } + throw e + } +}