Skip to content

Commit

Permalink
fix: prefer full-sized requests again, hopefully no 429?
Browse files Browse the repository at this point in the history
  • Loading branch information
bddvlpr committed Jan 24, 2024
1 parent 5a54a3b commit 222dc3b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/lessons/lessons.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export class LessonsController {
@Get(':classId')
async getLessonsForClass(@Param('classId', ParseIntPipe) classId: number) {
const lessons = await this.untisService.fetchTimetable(
this.configService.get<number>('LESSONS_TIMETABLE_BEFORE', 31),
this.configService.get<number>('LESSONS_TIMETABLE_AFTER', 31),
this.configService.get<number>('LESSONS_TIMETABLE_BEFORE', 7),
this.configService.get<number>('LESSONS_TIMETABLE_AFTER', 7),
classId,
);
if (!lessons)
Expand Down Expand Up @@ -77,8 +77,8 @@ export class LessonsController {
offset?: number,
) {
const lessons = await this.untisService.fetchTimetable(
this.configService.get<number>('LESSONS_TIMETABLE_BEFORE', 31),
this.configService.get<number>('LESSONS_TIMETABLE_AFTER', 31),
this.configService.get<number>('LESSONS_TIMETABLE_BEFORE', 7),
this.configService.get<number>('LESSONS_TIMETABLE_AFTER', 7),
classId,
);
if (!lessons)
Expand Down
22 changes: 8 additions & 14 deletions src/untis/untis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,16 @@ export class UntisService {
async () => {
await this.validateSession();

const currentMoment = moment().subtract(before, 'days');
const endMoment = moment().add(after, 'days');

const promises = [];
while (currentMoment.isBefore(endMoment)) {
promises.push(
this.fetchTimetableFor(currentMoment.toDate(), classId),
);
currentMoment.add(1, 'days');
}
return (await Promise.all(promises)).filter((l) => l).flat();
return await this.fetchTimetableRange(
moment().subtract(before, 'days').toDate(),
moment().add(after, 'days').toDate(),
classId,
);
},
60_000,
);
}

/**
* @deprecated
*/
private fetchTimetableRange(
start: Date,
end: Date,
Expand All @@ -139,6 +130,9 @@ export class UntisService {
return this.client.getTimetableForRange(start, end, classId, type);
}

/**
* @deprecated
*/
private fetchTimetableFor(date: Date, classId: number, type = 1) {
this.logger.log(`Fetching timetable for ${date.toLocaleDateString()}...`);
return this.client
Expand Down

0 comments on commit 222dc3b

Please sign in to comment.