chuvsu-js 2.8.0 → 2.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -78,6 +78,7 @@ export declare class TtClient {
78
78
  audienceId: number;
79
79
  period: Period;
80
80
  }): Promise<Schedule>;
81
+ private getCachedAudienceImage;
81
82
  /** Get the audience photo (audimage). Returns null if missing. */
82
83
  getAudienceImage(audienceId: number): Promise<Buffer | null>;
83
84
  /** Get the building exterior image (blockimage). Returns null if missing. */
package/dist/tt/client.js CHANGED
@@ -259,29 +259,38 @@ export class TtClient {
259
259
  schedules.set(opts.period, days);
260
260
  return new Schedule(opts.audienceId, schedules, opts.period, this.educationType);
261
261
  }
262
+ async getCachedAudienceImage(cacheKey, fetchUrl) {
263
+ const cached = this.cache?.get("audienceImages", cacheKey);
264
+ if (cached !== null && cached !== undefined) {
265
+ const entry = cached;
266
+ return entry.data ? Buffer.from(entry.data, "base64") : null;
267
+ }
268
+ const url = await fetchUrl();
269
+ if (!url) {
270
+ this.cache?.set("audienceImages", cacheKey, { data: null });
271
+ return null;
272
+ }
273
+ const buf = await this.authGetBuffer(`${BASE}${url}`);
274
+ if (buf.length === 0) {
275
+ this.cache?.set("audienceImages", cacheKey, { data: null });
276
+ return null;
277
+ }
278
+ this.cache?.set("audienceImages", cacheKey, {
279
+ data: buf.toString("base64"),
280
+ });
281
+ return buf;
282
+ }
262
283
  /** Get the audience photo (audimage). Returns null if missing. */
263
284
  async getAudienceImage(audienceId) {
264
- const info = await this.getAudienceInfo(audienceId);
265
- if (!info?.audImageUrl)
266
- return null;
267
- const buf = await this.authGetBuffer(`${BASE}${info.audImageUrl}`);
268
- return buf.length > 0 ? buf : null;
285
+ return this.getCachedAudienceImage(`aud:${audienceId}`, async () => (await this.getAudienceInfo(audienceId))?.audImageUrl);
269
286
  }
270
287
  /** Get the building exterior image (blockimage). Returns null if missing. */
271
288
  async getAudienceBlockImage(audienceId) {
272
- const info = await this.getAudienceInfo(audienceId);
273
- if (!info?.blockImageUrl)
274
- return null;
275
- const buf = await this.authGetBuffer(`${BASE}${info.blockImageUrl}`);
276
- return buf.length > 0 ? buf : null;
289
+ return this.getCachedAudienceImage(`block:${audienceId}`, async () => (await this.getAudienceInfo(audienceId))?.blockImageUrl);
277
290
  }
278
291
  /** Get the floor plan image for the audience. Returns null if missing. */
279
292
  async getAudienceFloorplan(audienceId) {
280
- const info = await this.getAudienceInfo(audienceId);
281
- if (!info?.floorplanUrl)
282
- return null;
283
- const buf = await this.authGetBuffer(`${BASE}${info.floorplanUrl}`);
284
- return buf.length > 0 ? buf : null;
293
+ return this.getCachedAudienceImage(`floor:${audienceId}`, async () => (await this.getAudienceInfo(audienceId))?.floorplanUrl);
285
294
  }
286
295
  async searchTeacher(opts) {
287
296
  const { body } = await this.authPost(`${BASE}/`, {
package/dist/tt/parse.js CHANGED
@@ -397,6 +397,26 @@ export function parseAudienceInfo(html) {
397
397
  const audImg = doc.querySelector("#audsrc");
398
398
  const blockImg = doc.querySelector("#blocksrc");
399
399
  const floorImg = doc.querySelector("#floorsrc");
400
+ // Highlight rect from the image map: prefer the <area> whose id matches
401
+ // the current audience (planaudNNNN); fall back to the first rect area.
402
+ let floorplanRect;
403
+ const areas = doc.querySelectorAll('map[name="flooraud"] area[shape="rect"]');
404
+ let chosen = undefined;
405
+ for (const a of areas) {
406
+ if (a.getAttribute("alt")?.trim() === name) {
407
+ chosen = a;
408
+ break;
409
+ }
410
+ }
411
+ if (!chosen && areas.length > 0)
412
+ chosen = areas[0];
413
+ if (chosen) {
414
+ const coords = chosen.getAttribute("coords") ?? "";
415
+ const parts = coords.split(",").map((s) => parseInt(s.trim(), 10));
416
+ if (parts.length === 4 && parts.every((n) => Number.isFinite(n))) {
417
+ floorplanRect = { x1: parts[0], y1: parts[1], x2: parts[2], y2: parts[3] };
418
+ }
419
+ }
400
420
  return {
401
421
  name,
402
422
  building,
@@ -405,6 +425,7 @@ export function parseAudienceInfo(html) {
405
425
  audImageUrl: audImg?.getAttribute("src") || undefined,
406
426
  blockImageUrl: blockImg?.getAttribute("src") || undefined,
407
427
  floorplanUrl: floorImg?.getAttribute("src") || undefined,
428
+ floorplanRect,
408
429
  };
409
430
  }
410
431
  function parseAudienceSemesterEntry(el) {
@@ -27,6 +27,13 @@ export interface AudienceInfo {
27
27
  blockImageUrl?: string;
28
28
  /** Relative URL of the floor plan image (/index/floorplan/...). */
29
29
  floorplanUrl?: string;
30
+ /** Rectangle (in floorplan image pixels) highlighting this audience. */
31
+ floorplanRect?: {
32
+ x1: number;
33
+ y1: number;
34
+ x2: number;
35
+ y2: number;
36
+ };
30
37
  }
31
38
  /** A date-specific substitution (room and/or teacher change). */
32
39
  export interface Substitution {
@@ -140,6 +147,7 @@ export interface CacheConfig {
140
147
  teacherInfo?: number;
141
148
  teacherPhotos?: number;
142
149
  audienceInfo?: number;
150
+ audienceImages?: number;
143
151
  }
144
152
  export interface TtClientOptions {
145
153
  educationType?: EducationType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chuvsu-js",
3
- "version": "2.8.0",
3
+ "version": "2.8.2",
4
4
  "description": "Node.js library for ChuvSU student portal (lk.chuvsu.ru) and schedule (tt.chuvsu.ru)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",