diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..53c24b7 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -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 }} diff --git a/package.json b/package.json index 0e5cde9..82148b7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/easistent/fetcher.ts b/src/easistent/fetcher.ts index 1280bad..5e2f279 100644 --- a/src/easistent/fetcher.ts +++ b/src/easistent/fetcher.ts @@ -1,3 +1,5 @@ +import { version } from "../../package.json" with { type: "json" }; + export type TimetableRequestParams = { schoolId?: number; classId?: number; @@ -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", }, diff --git a/src/easistent/school.ts b/src/easistent/school.ts index 8116a0c..c4340f3 100644 --- a/src/easistent/school.ts +++ b/src/easistent/school.ts @@ -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[] = []; @@ -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(); }