@curless/hotel-storefront 0.84.7 → 0.85.0
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.
- package/dist/card.generated.d.ts +1 -1
- package/dist/card.generated.js +2 -2
- package/dist/card.generated.js.map +1 -1
- package/dist/config.d.ts +35 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/huazhu/ari-sync.d.ts +93 -6
- package/dist/huazhu/ari-sync.d.ts.map +1 -1
- package/dist/huazhu/ari-sync.js +303 -28
- package/dist/huazhu/ari-sync.js.map +1 -1
- package/dist/huazhu/backend.d.ts +9 -0
- package/dist/huazhu/backend.d.ts.map +1 -1
- package/dist/huazhu/backend.js +62 -22
- package/dist/huazhu/backend.js.map +1 -1
- package/dist/huazhu/catalog.d.ts +58 -1
- package/dist/huazhu/catalog.d.ts.map +1 -1
- package/dist/huazhu/catalog.js +318 -15
- package/dist/huazhu/catalog.js.map +1 -1
- package/dist/huazhu/live-asks.d.ts +63 -0
- package/dist/huazhu/live-asks.d.ts.map +1 -0
- package/dist/huazhu/live-asks.js +127 -0
- package/dist/huazhu/live-asks.js.map +1 -0
- package/dist/huazhu/make.d.ts.map +1 -1
- package/dist/huazhu/make.js +50 -4
- package/dist/huazhu/make.js.map +1 -1
- package/dist/pictures.d.ts +17 -0
- package/dist/pictures.d.ts.map +1 -1
- package/dist/pictures.js +65 -1
- package/dist/pictures.js.map +1 -1
- package/dist/present.d.ts +10 -0
- package/dist/present.d.ts.map +1 -1
- package/dist/present.js +27 -0
- package/dist/present.js.map +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +3 -1
- package/dist/server.js.map +1 -1
- package/dist/tools.d.ts +5 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +8 -18
- package/dist/tools.js.map +1 -1
- package/package.json +1 -1
package/dist/huazhu/catalog.d.ts
CHANGED
|
@@ -14,8 +14,26 @@ export type CatalogOptions = {
|
|
|
14
14
|
* quote, a trip or a booking - and its record is never read.
|
|
15
15
|
*/
|
|
16
16
|
exclude?: string[];
|
|
17
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* How old a hotel's record may be before it is fetched again, and how old the list may be. Not
|
|
19
|
+
* given: the list is asked every day, and a record is fetched again after a day - or, with more
|
|
20
|
+
* hotels than a day's calls allow (`recordAgeFor`), after as many days as they need.
|
|
21
|
+
*/
|
|
18
22
|
maxAgeMs?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Calls to /hotel this connector makes in any 24 hours at most (default `HOTEL_DAILY_BUDGET`,
|
|
25
|
+
* under Huazhu's 20,000: the rest is for another process on the same credentials, and a run
|
|
26
|
+
* that went wrong). Counted in the copy's own file, so a restart does not forget them.
|
|
27
|
+
*/
|
|
28
|
+
dailyBudget?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Another connector's state directory, read and never written - this one asks Huazhu for NO hotel
|
|
31
|
+
* and NO list (0.85.0, `data.api.catalogFrom`). The second process on one Huazhu account (the
|
|
32
|
+
* demonstration beside the production connector) would otherwise fetch every hotel again: with
|
|
33
|
+
* 15,000 hotels, 30,000 calls to /hotel in a day against a limit of 20,000. The copy is read
|
|
34
|
+
* again when the other connector's file changed, at most every six hours.
|
|
35
|
+
*/
|
|
36
|
+
followDir?: string;
|
|
19
37
|
now?: () => number;
|
|
20
38
|
log?: (line: string) => void;
|
|
21
39
|
};
|
|
@@ -35,6 +53,19 @@ export type CatalogState = {
|
|
|
35
53
|
* (we deploy several times a day) or a sync that keeps failing cannot go over.
|
|
36
54
|
*/
|
|
37
55
|
export declare const LIST_MIN_GAP_MS: number;
|
|
56
|
+
/**
|
|
57
|
+
* Calls to /hotel this connector makes in 24 hours at most (0.85.0): 90% of Huazhu's 20,000 -
|
|
58
|
+
* 15,000 hotels are 15,000 calls for the first copy, and nothing else may be asked that day.
|
|
59
|
+
*/
|
|
60
|
+
export declare const HOTEL_DAILY_BUDGET = 18000;
|
|
61
|
+
/**
|
|
62
|
+
* How old a hotel's record may be before it is fetched again, with `n` hotels (0.85.0): a day, or
|
|
63
|
+
* as many days as a day's budget needs to fetch them all (`REFRESH_SHARE` of it: the rest is for
|
|
64
|
+
* hotels new to the list and for the calls that failed). 83 hotels: a day. 15,000: two days.
|
|
65
|
+
* Names, addresses and photographs change in weeks; the LIST is what must be asked every day, for
|
|
66
|
+
* a hotel that left it leaves the connector that day.
|
|
67
|
+
*/
|
|
68
|
+
export declare const recordAgeFor: (n: number, budget?: number) => number;
|
|
38
69
|
/**
|
|
39
70
|
* How often a running sync writes the copy down: every 500 hotels or every
|
|
40
71
|
* minute, whichever comes first - and never when nothing changed.
|
|
@@ -53,6 +84,10 @@ export declare class Catalog {
|
|
|
53
84
|
private syncing?;
|
|
54
85
|
private stoppedBecause?;
|
|
55
86
|
private readonly path;
|
|
87
|
+
private readonly ledgerPath;
|
|
88
|
+
/** Calls to /hotel by hour (hours since 1970): the daily budget's ledger, in its own small file. */
|
|
89
|
+
private calls;
|
|
90
|
+
private callsUnwritten;
|
|
56
91
|
/** Changes since the copy was last written, and when that was. */
|
|
57
92
|
private dirty;
|
|
58
93
|
private savedAt;
|
|
@@ -62,6 +97,8 @@ export declare class Catalog {
|
|
|
62
97
|
*/
|
|
63
98
|
private changes;
|
|
64
99
|
private readonly excluded;
|
|
100
|
+
/** The followed file's modified time when it was read, and when (`followDir`). */
|
|
101
|
+
private followed?;
|
|
65
102
|
constructor(opts: CatalogOptions);
|
|
66
103
|
private now;
|
|
67
104
|
private log;
|
|
@@ -80,6 +117,15 @@ export declare class Catalog {
|
|
|
80
117
|
get version(): number;
|
|
81
118
|
get size(): number;
|
|
82
119
|
state(): CatalogState;
|
|
120
|
+
/** How old a hotel's record may be before it is fetched again - see `recordAgeFor`. */
|
|
121
|
+
private recordAge;
|
|
122
|
+
private budget;
|
|
123
|
+
/**
|
|
124
|
+
* Following another connector's copy (`followDir`): stale when its file changed, and not read
|
|
125
|
+
* again within six hours of the last read - a reload holds two copies in memory for a while.
|
|
126
|
+
*/
|
|
127
|
+
private followStale;
|
|
128
|
+
private reload;
|
|
83
129
|
/** True when the list is older than a day, or the last sync did not reach its end. */
|
|
84
130
|
stale(): boolean;
|
|
85
131
|
/** When /hotels may next be asked for, if that is not yet; otherwise undefined. */
|
|
@@ -96,9 +142,20 @@ export declare class Catalog {
|
|
|
96
142
|
get writes(): number;
|
|
97
143
|
private written;
|
|
98
144
|
private save;
|
|
145
|
+
/** Calls to /hotel in the last 24 hours, by the ledger. */
|
|
146
|
+
private callsToday;
|
|
147
|
+
/**
|
|
148
|
+
* ⚠️ THE LEDGER IS ITS OWN FILE, WRITTEN EVERY 100 CALLS (0.85.0 review): kept in catalog.json it
|
|
149
|
+
* was lost with up to 1,500 calls at a restart, and with the whole file when it could not be read -
|
|
150
|
+
* and a day's budget that starts again at nothing is how the limit gets passed.
|
|
151
|
+
*/
|
|
152
|
+
private countCall;
|
|
153
|
+
private flushLedger;
|
|
154
|
+
private loadLedger;
|
|
99
155
|
private run;
|
|
100
156
|
/** The per-hotel /hotel pass over `ids`; marks the copy complete when it reaches the end whole. */
|
|
101
157
|
private refresh;
|
|
158
|
+
private refreshOnce;
|
|
102
159
|
}
|
|
103
160
|
export type CatalogView = {
|
|
104
161
|
language: 'zh' | 'en';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/huazhu/catalog.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/huazhu/catalog.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAS,MAAM,aAAa,CAAC;AACxF,OAAO,EAAE,KAAK,YAAY,EAAiB,MAAM,aAAa,CAAC;AAE/D,OAAO,EAA4C,KAAK,WAAW,EAAY,MAAM,WAAW,CAAC;AAkBjG,MAAM,MAAM,cAAc,G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AAqBF;;;;;;GAMG;AACH,eAAO,MAAM,OAAO,GAClB,WAAW,MAAM,GAAG,SAAS,EAC7B,WAAW,MAAM,GAAG,SAAS,KAC5B;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,SA0C5C,CAAC;AAEF,yFAAyF;AACzF,eAAO,MAAM,UAAU,GAAI,GAAG,WAAW,EAAE,KAAK,MAAM,KAAG,UAaxD,CAAC;AAEF,6HAA6H;AAC7H,eAAO,MAAM,QAAQ,GAAI,GAAG,WAAW,KAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,SAaxE,CAAC;AAwDF;;;;;;;;GAQG;AACH,eAAO,MAAM,OAAO,GAAI,GAAG,WAAW,EAAE,eAAa,KAAG,UAyBvD,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,GAAG,WAAW,EAAE,GAAG,WAAW,EAAE,KAAK,MAAM,KAAG,KAoCrE,CAAC;AAEF,6EAA6E;AAC7E,eAAO,MAAM,UAAU,GAAI,GAAG,WAAW,KAAG,MAqB1B,CAAC;AAQnB;;;GAGG;AACH,eAAO,MAAM,OAAO,GAAI,GAAG,WAAW,EAAE,OAAO,MAAM,KAAG,OAavD,CAAC"}
|
package/dist/huazhu/catalog.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
1
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, renameSync, statSync, writeFileSync, writeSync, } from 'node:fs';
|
|
2
2
|
import { dirname, join } from 'node:path';
|
|
3
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
3
4
|
import { todayAt } from '../dates.js';
|
|
4
5
|
import { DESCRIPTION_MAX, cleanHotelText } from '../fence.js';
|
|
5
6
|
import { HuazhuRefusal } from './client.js';
|
|
@@ -14,6 +15,21 @@ const DAY = 86_400_000;
|
|
|
14
15
|
* (we deploy several times a day) or a sync that keeps failing cannot go over.
|
|
15
16
|
*/
|
|
16
17
|
export const LIST_MIN_GAP_MS = 60 * 60_000;
|
|
18
|
+
/**
|
|
19
|
+
* Calls to /hotel this connector makes in 24 hours at most (0.85.0): 90% of Huazhu's 20,000 -
|
|
20
|
+
* 15,000 hotels are 15,000 calls for the first copy, and nothing else may be asked that day.
|
|
21
|
+
*/
|
|
22
|
+
export const HOTEL_DAILY_BUDGET = 18_000;
|
|
23
|
+
/** A record is fetched again after a day, unless the hotels would take more than this share of a day's budget. */
|
|
24
|
+
const REFRESH_SHARE = 0.45;
|
|
25
|
+
/**
|
|
26
|
+
* How old a hotel's record may be before it is fetched again, with `n` hotels (0.85.0): a day, or
|
|
27
|
+
* as many days as a day's budget needs to fetch them all (`REFRESH_SHARE` of it: the rest is for
|
|
28
|
+
* hotels new to the list and for the calls that failed). 83 hotels: a day. 15,000: two days.
|
|
29
|
+
* Names, addresses and photographs change in weeks; the LIST is what must be asked every day, for
|
|
30
|
+
* a hotel that left it leaves the connector that day.
|
|
31
|
+
*/
|
|
32
|
+
export const recordAgeFor = (n, budget = HOTEL_DAILY_BUDGET) => DAY * Math.max(1, Math.ceil(n / (budget * REFRESH_SHARE)));
|
|
17
33
|
/**
|
|
18
34
|
* How often a running sync writes the copy down: every 500 hotels or every
|
|
19
35
|
* minute, whichever comes first - and never when nothing changed.
|
|
@@ -26,12 +42,136 @@ export const LIST_MIN_GAP_MS = 60 * 60_000;
|
|
|
26
42
|
*/
|
|
27
43
|
export const SAVE_EVERY_HOTELS = 500;
|
|
28
44
|
export const SAVE_EVERY_MS = 60_000;
|
|
45
|
+
/**
|
|
46
|
+
* ...AND IT GROWS WITH THE COPY (0.85.0): 15,000 hotels are a 200 MB file, and a rewrite a minute
|
|
47
|
+
* during the first sync is 12 GB an hour. The whole copy is written at most every 40 ms a hotel
|
|
48
|
+
* (10 minutes at 15,000), and every tenth of the hotels; a crash costs that many fetches again.
|
|
49
|
+
*/
|
|
50
|
+
const SAVE_MS_PER_HOTEL = 40;
|
|
51
|
+
// ------------------------------------------------------------- the file ----
|
|
52
|
+
/**
|
|
53
|
+
* ⚠️ THE FILE IS ONE HOTEL A LINE (0.85.0), AND STILL ONE JSON DOCUMENT. 15,000 hotels are a 200 MB
|
|
54
|
+
* file: read whole, that is 200 MB of text and then 300 MB of objects at once (1.2 GB for a moment
|
|
55
|
+
* on every start, in each of the connector's processes), and written whole from one string. Laid
|
|
56
|
+
* out as
|
|
57
|
+
*
|
|
58
|
+
* {"version":1,"listedAt":...,"hotels":{
|
|
59
|
+
* "1160012":{"fetchedAt":...,"data":{...}},
|
|
60
|
+
* "1160013":{...}
|
|
61
|
+
* },"order":["1160012","1160013"]}
|
|
62
|
+
*
|
|
63
|
+
* it is read and written a hotel at a time, and an older build reads it as the JSON it is. A file
|
|
64
|
+
* written by an older build (one line) is read whole, as ever, and rewritten in this layout.
|
|
65
|
+
*/
|
|
66
|
+
const HEADER_END = '"hotels":{';
|
|
67
|
+
const FOOTER_START = '},"order":';
|
|
68
|
+
/** The copy at `path`, or nothing when there is none (or it cannot be read). */
|
|
69
|
+
const readCatalog = (path) => {
|
|
70
|
+
let fd;
|
|
71
|
+
try {
|
|
72
|
+
fd = openSync(path, 'r');
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
const buf = Buffer.allocUnsafe(1 << 20);
|
|
79
|
+
const decoder = new StringDecoder('utf8');
|
|
80
|
+
let carry = '';
|
|
81
|
+
let header;
|
|
82
|
+
let file;
|
|
83
|
+
let first = true;
|
|
84
|
+
let streamed = true;
|
|
85
|
+
const hotels = {};
|
|
86
|
+
let rest = '';
|
|
87
|
+
const line = (text) => {
|
|
88
|
+
if (first) {
|
|
89
|
+
first = false;
|
|
90
|
+
if (!text.startsWith('{"version":1,') || !text.endsWith(HEADER_END)) {
|
|
91
|
+
streamed = false;
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
header = text.slice(0, text.length - HEADER_END.length);
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (text.startsWith(FOOTER_START)) {
|
|
98
|
+
const tail = JSON.parse(`{"_":0${text.slice(1)}`);
|
|
99
|
+
const head = JSON.parse(`${(header ?? '{').replace(/,$/, '')}}`);
|
|
100
|
+
file = { ...head, hotels, order: tail.order ?? [] };
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const one = JSON.parse(`{${text.endsWith(',') ? text.slice(0, -1) : text}}`);
|
|
104
|
+
Object.assign(hotels, one);
|
|
105
|
+
return true;
|
|
106
|
+
};
|
|
107
|
+
scan: for (;;) {
|
|
108
|
+
const n = readSync(fd, buf, 0, buf.length, null);
|
|
109
|
+
const chunk = n > 0 ? decoder.write(buf.subarray(0, n)) : decoder.end();
|
|
110
|
+
carry += chunk;
|
|
111
|
+
let at = carry.indexOf('\n');
|
|
112
|
+
while (at >= 0) {
|
|
113
|
+
if (!line(carry.slice(0, at)))
|
|
114
|
+
break scan;
|
|
115
|
+
carry = carry.slice(at + 1);
|
|
116
|
+
at = carry.indexOf('\n');
|
|
117
|
+
}
|
|
118
|
+
if (n <= 0) {
|
|
119
|
+
rest = carry;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// The last line has no newline after it: the footer in this layout, the whole document in the older one.
|
|
124
|
+
if (streamed && !file && rest)
|
|
125
|
+
line(rest);
|
|
126
|
+
if (!streamed || first) {
|
|
127
|
+
// The older layout: one document. Read whole - what every build did before.
|
|
128
|
+
carry = '';
|
|
129
|
+
rest = '';
|
|
130
|
+
const text = readFileSync(path, 'utf8');
|
|
131
|
+
const raw = JSON.parse(text);
|
|
132
|
+
return raw?.version === 1 && raw.hotels && Array.isArray(raw.order) ? raw : undefined;
|
|
133
|
+
}
|
|
134
|
+
return file && file.version === 1 ? file : undefined;
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
// Not a copy we can read: the first sync makes one.
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
closeSync(fd);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
/** The copy to `path` in the layout above, a megabyte at a time. */
|
|
145
|
+
const writeCatalog = (path, file) => {
|
|
146
|
+
const fd = openSync(path, 'w');
|
|
147
|
+
try {
|
|
148
|
+
const { hotels, order, ...head } = file;
|
|
149
|
+
let out = `${JSON.stringify(head).slice(0, -1)},${HEADER_END}\n`;
|
|
150
|
+
const ids = Object.keys(hotels);
|
|
151
|
+
ids.forEach((id, i) => {
|
|
152
|
+
out += `${JSON.stringify(id)}:${JSON.stringify(hotels[id])}${i < ids.length - 1 ? ',' : ''}\n`;
|
|
153
|
+
if (out.length >= 1 << 20) {
|
|
154
|
+
writeSync(fd, out);
|
|
155
|
+
out = '';
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
out += `${FOOTER_START}${JSON.stringify(order)}}`;
|
|
159
|
+
writeSync(fd, out);
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
closeSync(fd);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
29
165
|
export class Catalog {
|
|
30
166
|
opts;
|
|
31
167
|
file = { version: 1, hotels: {}, order: [] };
|
|
32
168
|
syncing;
|
|
33
169
|
stoppedBecause;
|
|
34
170
|
path;
|
|
171
|
+
ledgerPath;
|
|
172
|
+
/** Calls to /hotel by hour (hours since 1970): the daily budget's ledger, in its own small file. */
|
|
173
|
+
calls = {};
|
|
174
|
+
callsUnwritten = 0;
|
|
35
175
|
/** Changes since the copy was last written, and when that was. */
|
|
36
176
|
dirty = 0;
|
|
37
177
|
savedAt = 0;
|
|
@@ -41,17 +181,45 @@ export class Catalog {
|
|
|
41
181
|
*/
|
|
42
182
|
changes = 0;
|
|
43
183
|
excluded;
|
|
184
|
+
/** The followed file's modified time when it was read, and when (`followDir`). */
|
|
185
|
+
followed;
|
|
44
186
|
constructor(opts) {
|
|
45
187
|
this.opts = opts;
|
|
46
188
|
this.excluded = new Set((opts.exclude ?? []).map(String));
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
189
|
+
// ⚠️ THE COPY HAS A DIRECTORY OF ITS OWN (0.85.0 review): `<state dir>/catalog/catalog.json`, so a second
|
|
190
|
+
// process can be given that directory and nothing else of this one's (its bookings, its token).
|
|
191
|
+
// A copy at the old place (`<state dir>/catalog.json`) is read and carried over; the old file stays,
|
|
192
|
+
// so going back to an older build finds its copy.
|
|
193
|
+
this.ledgerPath = join(opts.stateDir, 'hotel-calls.json');
|
|
194
|
+
this.path = opts.followDir
|
|
195
|
+
? existsSync(join(opts.followDir, 'catalog.json'))
|
|
196
|
+
? join(opts.followDir, 'catalog.json')
|
|
197
|
+
: join(opts.followDir, 'catalog', 'catalog.json')
|
|
198
|
+
: join(opts.stateDir, 'catalog', 'catalog.json');
|
|
199
|
+
let loaded = readCatalog(this.path);
|
|
200
|
+
if (!loaded && !opts.followDir) {
|
|
201
|
+
loaded = readCatalog(join(opts.stateDir, 'catalog.json'));
|
|
202
|
+
if (loaded) {
|
|
203
|
+
this.file = loaded;
|
|
204
|
+
try {
|
|
205
|
+
this.save();
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
// Written at the next sync.
|
|
209
|
+
}
|
|
210
|
+
}
|
|
52
211
|
}
|
|
53
|
-
|
|
54
|
-
|
|
212
|
+
if (!opts.followDir)
|
|
213
|
+
this.loadLedger();
|
|
214
|
+
if (loaded)
|
|
215
|
+
this.file = loaded;
|
|
216
|
+
if (opts.followDir) {
|
|
217
|
+
try {
|
|
218
|
+
this.followed = { mtimeMs: statSync(this.path).mtimeMs, readAt: Date.now() };
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
// Not there yet: read when it is.
|
|
222
|
+
}
|
|
55
223
|
}
|
|
56
224
|
}
|
|
57
225
|
now() {
|
|
@@ -97,8 +265,47 @@ export class Catalog {
|
|
|
97
265
|
...(this.stoppedBecause ? { stoppedBecause: this.stoppedBecause } : {}),
|
|
98
266
|
};
|
|
99
267
|
}
|
|
268
|
+
/** How old a hotel's record may be before it is fetched again - see `recordAgeFor`. */
|
|
269
|
+
recordAge() {
|
|
270
|
+
return this.opts.maxAgeMs ?? recordAgeFor(this.file.order.length, this.budget());
|
|
271
|
+
}
|
|
272
|
+
budget() {
|
|
273
|
+
return this.opts.dailyBudget ?? HOTEL_DAILY_BUDGET;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Following another connector's copy (`followDir`): stale when its file changed, and not read
|
|
277
|
+
* again within six hours of the last read - a reload holds two copies in memory for a while.
|
|
278
|
+
*/
|
|
279
|
+
followStale() {
|
|
280
|
+
if (!this.followed)
|
|
281
|
+
return this.file.order.length === 0;
|
|
282
|
+
if (Date.now() - this.followed.readAt < 6 * 3_600_000)
|
|
283
|
+
return false;
|
|
284
|
+
try {
|
|
285
|
+
return statSync(this.path).mtimeMs !== this.followed.mtimeMs;
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
reload() {
|
|
292
|
+
try {
|
|
293
|
+
const mtimeMs = statSync(this.path).mtimeMs;
|
|
294
|
+
const loaded = readCatalog(this.path);
|
|
295
|
+
if (loaded) {
|
|
296
|
+
this.file = loaded;
|
|
297
|
+
this.changes += 1;
|
|
298
|
+
}
|
|
299
|
+
this.followed = { mtimeMs, readAt: Date.now() };
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
// Still not there: the next look tries again.
|
|
303
|
+
}
|
|
304
|
+
}
|
|
100
305
|
/** True when the list is older than a day, or the last sync did not reach its end. */
|
|
101
306
|
stale() {
|
|
307
|
+
if (this.opts.followDir)
|
|
308
|
+
return this.followStale();
|
|
102
309
|
const age = this.opts.maxAgeMs ?? DAY;
|
|
103
310
|
return (!this.file.listedAt ||
|
|
104
311
|
this.now() - this.file.listedAt > age ||
|
|
@@ -107,6 +314,8 @@ export class Catalog {
|
|
|
107
314
|
}
|
|
108
315
|
/** When /hotels may next be asked for, if that is not yet; otherwise undefined. */
|
|
109
316
|
listHeldUntil() {
|
|
317
|
+
if (this.opts.followDir)
|
|
318
|
+
return undefined;
|
|
110
319
|
const at = this.file.listAskedAt;
|
|
111
320
|
if (at === undefined)
|
|
112
321
|
return undefined;
|
|
@@ -115,6 +324,10 @@ export class Catalog {
|
|
|
115
324
|
}
|
|
116
325
|
/** One sync at a time; a second caller waits on the first. */
|
|
117
326
|
sync() {
|
|
327
|
+
if (this.opts.followDir) {
|
|
328
|
+
this.reload();
|
|
329
|
+
return Promise.resolve();
|
|
330
|
+
}
|
|
118
331
|
if (!this.syncing) {
|
|
119
332
|
this.syncing = this.run().finally(() => {
|
|
120
333
|
this.syncing = undefined;
|
|
@@ -129,8 +342,10 @@ export class Catalog {
|
|
|
129
342
|
}
|
|
130
343
|
/** Written down when enough has changed, or enough time has passed - see SAVE_EVERY_*. */
|
|
131
344
|
saveSoon() {
|
|
132
|
-
|
|
133
|
-
|
|
345
|
+
const n = this.file.order.length;
|
|
346
|
+
if (this.dirty >= Math.max(SAVE_EVERY_HOTELS, Math.round(n / 10)) ||
|
|
347
|
+
(this.dirty > 0 &&
|
|
348
|
+
this.now() - this.savedAt >= Math.max(SAVE_EVERY_MS, n * SAVE_MS_PER_HOTEL)))
|
|
134
349
|
this.save();
|
|
135
350
|
}
|
|
136
351
|
/** Written down now, if there is anything to write. */
|
|
@@ -144,19 +359,75 @@ export class Catalog {
|
|
|
144
359
|
}
|
|
145
360
|
written = 0;
|
|
146
361
|
save() {
|
|
362
|
+
if (this.opts.followDir)
|
|
363
|
+
return;
|
|
147
364
|
this.dirty = 0;
|
|
148
365
|
this.savedAt = this.now();
|
|
149
366
|
this.written += 1;
|
|
150
367
|
mkdirSync(dirname(this.path), { recursive: true });
|
|
151
368
|
const tmp = `${this.path}.${process.pid}.tmp`;
|
|
152
|
-
|
|
369
|
+
writeCatalog(tmp, this.file);
|
|
153
370
|
renameSync(tmp, this.path);
|
|
154
371
|
}
|
|
372
|
+
/** Calls to /hotel in the last 24 hours, by the ledger. */
|
|
373
|
+
callsToday() {
|
|
374
|
+
const hour = Math.floor(this.now() / 3_600_000);
|
|
375
|
+
let n = 0;
|
|
376
|
+
for (const [h, c] of Object.entries(this.calls))
|
|
377
|
+
if (hour - Number(h) < 24)
|
|
378
|
+
n += c;
|
|
379
|
+
return n;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* ⚠️ THE LEDGER IS ITS OWN FILE, WRITTEN EVERY 100 CALLS (0.85.0 review): kept in catalog.json it
|
|
383
|
+
* was lost with up to 1,500 calls at a restart, and with the whole file when it could not be read -
|
|
384
|
+
* and a day's budget that starts again at nothing is how the limit gets passed.
|
|
385
|
+
*/
|
|
386
|
+
countCall() {
|
|
387
|
+
const hour = Math.floor(this.now() / 3_600_000);
|
|
388
|
+
this.calls[hour] = (this.calls[hour] ?? 0) + 1;
|
|
389
|
+
for (const h of Object.keys(this.calls))
|
|
390
|
+
if (hour - Number(h) >= 25)
|
|
391
|
+
delete this.calls[h];
|
|
392
|
+
this.callsUnwritten += 1;
|
|
393
|
+
if (this.callsUnwritten >= 100)
|
|
394
|
+
this.flushLedger();
|
|
395
|
+
}
|
|
396
|
+
flushLedger() {
|
|
397
|
+
if (this.callsUnwritten === 0)
|
|
398
|
+
return;
|
|
399
|
+
this.callsUnwritten = 0;
|
|
400
|
+
try {
|
|
401
|
+
mkdirSync(dirname(this.ledgerPath), { recursive: true });
|
|
402
|
+
const tmp = `${this.ledgerPath}.${process.pid}.tmp`;
|
|
403
|
+
writeFileSync(tmp, JSON.stringify({ version: 1, calls: this.calls }));
|
|
404
|
+
renameSync(tmp, this.ledgerPath);
|
|
405
|
+
}
|
|
406
|
+
catch (err) {
|
|
407
|
+
this.log(`the count of calls to /hotel could not be written: ${err.message}`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
loadLedger() {
|
|
411
|
+
try {
|
|
412
|
+
const f = JSON.parse(readFileSync(this.ledgerPath, 'utf8'));
|
|
413
|
+
for (const [h, c] of Object.entries(f.calls ?? {}))
|
|
414
|
+
if (Number.isFinite(Number(h)) && typeof c === 'number' && c > 0)
|
|
415
|
+
this.calls[h] = c;
|
|
416
|
+
}
|
|
417
|
+
catch {
|
|
418
|
+
// No count yet.
|
|
419
|
+
}
|
|
420
|
+
}
|
|
155
421
|
async run() {
|
|
156
422
|
const { client } = this.opts;
|
|
157
|
-
const maxAge = this.
|
|
423
|
+
const maxAge = this.recordAge();
|
|
158
424
|
this.stoppedBecause = undefined;
|
|
159
425
|
const held = this.listHeldUntil();
|
|
426
|
+
// ⚠️ NO CALLS LEFT TODAY, NO LIST EITHER (0.85.0 review): a run stopped by our own limit asked /hotels
|
|
427
|
+
// again every hour for nothing.
|
|
428
|
+
if (held === undefined && this.callsToday() >= this.budget() && this.file.order.length > 0) {
|
|
429
|
+
return this.refresh(this.file.order.filter((id) => !this.excluded.has(id)), maxAge);
|
|
430
|
+
}
|
|
160
431
|
if (held !== undefined) {
|
|
161
432
|
this.log(`hotel list asked for at ${new Date(this.file.listAskedAt).toISOString()}, less than an hour ago - not asked again until ${new Date(held).toISOString()}`);
|
|
162
433
|
// Go on with the per-hotel refresh on the order we have; nothing to go on with, nothing to do.
|
|
@@ -213,16 +484,48 @@ export class Catalog {
|
|
|
213
484
|
}
|
|
214
485
|
/** The per-hotel /hotel pass over `ids`; marks the copy complete when it reaches the end whole. */
|
|
215
486
|
async refresh(ids, maxAge) {
|
|
487
|
+
try {
|
|
488
|
+
await this.refreshOnce(ids, maxAge);
|
|
489
|
+
}
|
|
490
|
+
finally {
|
|
491
|
+
this.flushLedger();
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
async refreshOnce(ids, maxAge) {
|
|
216
495
|
const { client } = this.opts;
|
|
217
496
|
let fetched = 0;
|
|
218
497
|
let failures = 0;
|
|
219
498
|
/** Hotels this run could not read, so the copy is not whole. */
|
|
220
499
|
let missed = 0;
|
|
221
|
-
|
|
500
|
+
// ⚠️ NEW HOTELS FIRST, THEN THE OLDEST RECORDS (0.85.0): with 15,000 hotels a day's calls may not
|
|
501
|
+
// reach the end, and the ones left must be the freshest, not the last in Huazhu's order.
|
|
502
|
+
const t = this.now();
|
|
503
|
+
// ⚠️ THE OLD RECORDS ARE SPREAD OVER THE DAYS (0.85.0 review): all 15,000 come due together two days after
|
|
504
|
+
// the first pull, and "every other day" was every other day's whole budget. A run takes its share of
|
|
505
|
+
// the oldest - the hotels over the days they may wait - and the rest wait for tomorrow's.
|
|
506
|
+
const share = maxAge >= DAY ? Math.ceil(ids.length / (maxAge / DAY)) : Number.POSITIVE_INFINITY;
|
|
507
|
+
const todo = [
|
|
508
|
+
...ids.filter((id) => !this.file.hotels[id]),
|
|
509
|
+
...ids
|
|
510
|
+
.filter((id) => {
|
|
511
|
+
const had = this.file.hotels[id];
|
|
512
|
+
return had && t - had.fetchedAt >= maxAge;
|
|
513
|
+
})
|
|
514
|
+
.sort((a, b) => (this.file.hotels[a]?.fetchedAt ?? 0) - (this.file.hotels[b]?.fetchedAt ?? 0))
|
|
515
|
+
.slice(0, share),
|
|
516
|
+
];
|
|
517
|
+
for (const id of todo) {
|
|
222
518
|
const had = this.file.hotels[id];
|
|
223
|
-
|
|
224
|
-
|
|
519
|
+
// ⚠️ OUR OWN LIMIT UNDER HUAZHU'S (0.85.0): a day's calls to /hotel are counted, and a run
|
|
520
|
+
// that has used them stops and is tried again at the next hourly look.
|
|
521
|
+
if (this.callsToday() >= this.budget()) {
|
|
522
|
+
this.stoppedBecause = `our own limit on /hotel (${this.budget()} calls in 24 hours, under Huazhu's 20,000) was reached after ${fetched} hotel(s) this run; the rest follow when calls free up`;
|
|
523
|
+
this.log(this.stoppedBecause);
|
|
524
|
+
this.saveIfDirty();
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
225
527
|
try {
|
|
528
|
+
this.countCall();
|
|
226
529
|
const data = await client.hotel(id);
|
|
227
530
|
this.file.hotels[id] = { fetchedAt: this.now(), data: { ...data, hotelId: id } };
|
|
228
531
|
fetched += 1;
|