Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CI

permissions:
contents: read
packages: write

on:
push:
branches: [main]
tags: ["v*.*.*"]
pull_request:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: docker/login-action@v3.6.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- uses: docker/setup-qemu-action@v3

- uses: docker/setup-buildx-action@v3

- id: meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ github.repository_owner }}/${{github.repository}}
tags: |
type=semver,pattern={{version}}
type=ref,event=branch
type=ref,event=pr

- name: Build image
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "easistent-tt",
"module": "src/index.ts",
"type": "module",
"version": "4.4.1-evil-world",
"version": "4.4.2-mrk",
"devDependencies": {
"bun-types": "latest",
"typescript": "^5.8.3",
Expand Down
4 changes: 3 additions & 1 deletion src/easistent/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { version } from "../../package.json" with { type: "json" };

export type TimetableRequestParams = {
schoolId?: number;
classId?: number;
Expand All @@ -13,7 +15,7 @@ export class Fetcher {
private static readonly fetchOptions: RequestInit = {
method: "GET",
headers: {
"user-agent": "Mozilla/5.0 (easistent-tt, matic says hi)",
"user-agent": `Mozilla/5.0 (X11; Linux x86_64) EasistentTt/${version} (https://github.com/MaticBabnik/easistent-tt; Merry Christmas)`,
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
accept: "text/html",
},
Expand Down
29 changes: 24 additions & 5 deletions src/easistent/school.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ export type WeekData = {
scrapedAt: number;
};

const DEFAULT_TTL = 30 * 60 * 1000;
const SCRAPE_INTERVAL = DEFAULT_TTL - 30_000;
const HOUR = 1000 * 60 * 60;
const DEFAULT_TTL = 2 * HOUR;
const SCRAPE_INTERVAL = HOUR - 30_000;

const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));

export class School {
public errors: SchoolError[] = [];
Expand Down Expand Up @@ -370,17 +373,33 @@ export class School {
if (this.asRunning) return;
this.asRunning = true;

const as = () => {
console.log("Refreshing cache");
/**
* Scrape surrounding weeks half as often
* starts as false but will get flipped on first run
*/
let scrapeSurrounding = false;

const as = async () => {
const week = this.getWeekForDate();

scrapeSurrounding = !scrapeSurrounding;
console.log(`Refreshing cache, surrounding=${scrapeSurrounding}`);

if (!scrapeSurrounding) {
await this.getWeek(week, true);
return;
}

const from = Math.max(1, week - 1);
const to = Math.min(52, week + 1);

for (let i = from; i <= to; i++) {
this.getWeek(i, true);
// biome-ignore lint/performance/noAwaitInLoops: intentional
await this.getWeek(i, true);
delay(3333); // be nice to the server
}
};

setInterval(as, SCRAPE_INTERVAL);
as();
}
Expand Down
Loading