-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
400 lines (355 loc) · 13.2 KB
/
Copy pathserver.js
File metadata and controls
400 lines (355 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
const http = require("http");
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const HOST = "127.0.0.1";
const FIRST_PORT = Number(process.env.PORT || 4173);
let port = FIRST_PORT;
const ROOT = __dirname;
const CACHE_MS = 15 * 60 * 1000;
const RMB_CACHE_MS = 6 * 60 * 60 * 1000;
const RMB_RETRY_CACHE_MS = 10 * 60 * 1000;
const SF_PRICE_ENDPOINT = "https://htm.sf-express.com/sf-service-core-web/service/product/psds/freightPrice/query";
const RMB_ROUTES = {
A: {
label: "A區(廣州海珠代表)",
dest: "A440105000",
destCityCode: "020",
},
B: {
label: "B區(北京東城代表)",
dest: "A110101000",
destCityCode: "010",
},
};
const SOURCES = {
fuel: "https://htm.sf-express.com/tw/tc/Customer_Zone/download_center/fuel_additional/",
prices: "https://htm.sf-express.com/tw/tc/Customer_Zone/download_center/price_down/",
resource: "https://htm.sf-express.com/tw/tc/news/detail/Announcement-on-the-Implementation-of-Peak-Resource-Adjustment-Fee-for-Taiwan-SF-Express-Shipments-to-Mainland-China-Hong-Kong-and-Macau/",
};
let liveCache = { expiresAt: 0, payload: null };
const rmbCache = new Map();
function stripHtml(value) {
return value
.replace(/<script[\s\S]*?<\/script>/gi, " ")
.replace(/<style[\s\S]*?<\/style>/gi, " ")
.replace(/<[^>]+>/g, " ")
.replace(/ | /gi, " ")
.replace(/&/gi, "&")
.replace(/–|–/gi, "-")
.replace(/—|—/gi, "-")
.replace(/\s+/g, " ")
.trim();
}
function taipeiDateParts() {
const parts = new Intl.DateTimeFormat("en-CA", {
timeZone: "Asia/Taipei",
year: "numeric",
month: "2-digit",
day: "2-digit",
}).formatToParts(new Date());
return Object.fromEntries(parts.map((part) => [part.type, part.value]));
}
function dateNumber(year, month, day) {
return Number(year) * 10000 + Number(month) * 100 + Number(day);
}
function isoDate(year, month, day) {
return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
}
function parseFuelPage(html) {
const rows = [...html.matchAll(/<tr\b[^>]*>([\s\S]*?)<\/tr>/gi)]
.map((match) => stripHtml(match[1]))
.map((text) => {
const period = text.match(/(20\d{2})\/(\d{1,2})\/(\d{1,2})\s*[-~~至]\s*(20\d{2})\/(\d{1,2})\/(\d{1,2})/);
const percentages = [...text.matchAll(/(\d+(?:\.\d+)?)\s*%/g)].map((match) => Number(match[1]));
if (!period || percentages.length === 0) return null;
return {
period: `${period[1]}/${Number(period[2])}/${Number(period[3])}-${period[4]}/${Number(period[5])}/${Number(period[6])}`,
start: dateNumber(period[1], period[2], period[3]),
end: dateNumber(period[4], period[5], period[6]),
startDate: isoDate(period[1], period[2], period[3]),
endDate: isoDate(period[4], period[5], period[6]),
rate: percentages[0],
};
})
.filter(Boolean);
if (!rows.length) throw new Error("官網燃油費表格式無法辨識");
const today = taipeiDateParts();
const todayNumber = dateNumber(today.year, today.month, today.day);
const current = rows.find((row) => todayNumber >= row.start && todayNumber <= row.end) || rows[0];
return {
rate: current.rate,
period: current.period,
schedule: rows.map(({ period, startDate, endDate, rate }) => ({ period, startDate, endDate, rate })),
};
}
function parsePriceSource(html) {
const row = html.match(/<tr\b[^>]*>[\s\S]*?href=["'][^"']*Taiwan-Export-Rates-EC-Ship-Personal-Shipment[^"']*\.pdf["'][\s\S]*?<\/tr>/i);
if (!row) throw new Error("找不到 E順遞官方價目表");
const hrefMatch = row[0].match(/href=["']([^"']*Taiwan-Export-Rates-EC-Ship-Personal-Shipment[^"']*\.pdf)["']/i);
const dateMatch = stripHtml(row[0]).match(/(20\d{2})年(\d{1,2})月(\d{1,2})日/);
const href = hrefMatch ? new URL(hrefMatch[1], SOURCES.prices).href : null;
const fileDateMatch = href ? href.match(/(20\d{2})(\d{2})(\d{2})/) : null;
const effectiveDate = dateMatch
? `${dateMatch[1]}年${Number(dateMatch[2])}月${Number(dateMatch[3])}日`
: fileDateMatch
? `${fileDateMatch[1]}年${Number(fileDateMatch[2])}月${Number(fileDateMatch[3])}日`
: "未辨識";
return {
effectiveDate,
pdfUrl: href,
expected: href ? href.includes("20250401") : false,
};
}
function parseResourceFee(html) {
const text = stripHtml(html);
const match = text.match(/TWD\s*([\d,.]+)\s*\/\s*KG/i);
if (!match) throw new Error("找不到資源調節費");
return { perKg: Number(match[1].replace(/,/g, "")) };
}
async function fetchText(url) {
const response = await fetch(url, {
headers: {
"accept-language": "zh-TW,zh;q=0.9,en;q=0.7",
"user-agent": "Mozilla/5.0 SF-E-Rate-Tool/1.0",
},
signal: AbortSignal.timeout(12000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response.text();
}
async function fetchJson(url) {
const response = await fetch(url, {
headers: {
accept: "application/json, text/plain, */*",
"accept-language": "zh-TW,zh;q=0.9,en;q=0.7",
referer: "https://htm.sf-express.com/we/ow/#/tw/tc/price-query",
"user-agent": "Mozilla/5.0 SF-E-Rate-Tool/1.3.2",
},
signal: AbortSignal.timeout(15000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response.json();
}
function normalizeShippingTime(value) {
const match = String(value || "").match(/^(20\d{2})-(\d{2})-(\d{2})[T ](\d{2})/);
if (match) return `${match[1]}-${match[2]}-${match[3]} ${match[4]}:00:00`;
const parts = new Intl.DateTimeFormat("en-CA", {
timeZone: "Asia/Taipei",
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
hour12: false,
}).formatToParts(new Date());
const values = Object.fromEntries(parts.map((part) => [part.type, part.value]));
const hour = values.hour === "24" ? "00" : values.hour;
return `${values.year}-${values.month}-${values.day} ${hour}:00:00`;
}
function numberOrNull(value) {
const number = Number(value);
return Number.isFinite(number) ? number : null;
}
async function fetchOfficialRmbRate(zone, weight, shippingTime) {
const route = RMB_ROUTES[zone];
const params = new URLSearchParams({
label: "1",
origin: "A000710900",
dest: route.dest,
originCityCode: "886",
destCityCode: route.destCityCode,
srcZoneCode: "",
destZoneCode: "",
weight: String(weight),
weightUnit: "kg",
postcode: "",
time: shippingTime,
width: "1",
height: "1",
length: "1",
lengthUnit: "",
lang: "tc",
region: "tw",
translate: "",
commodityName: "",
orderCount: "1",
destPostcode: "",
payMethod: "2",
paymentCountry: "CN",
});
const data = await fetchJson(`${SF_PRICE_ENDPOINT}?${params}`);
if (!data || data.code !== 0) throw new Error(data?.message || "順豐查價失敗");
const products = data.result?.productFreightPrices || [];
const product = products.find((item) => item.productCode === "EC-Ship")
|| products.find((item) => item.productDisplayName === "E順遞");
if (!product) throw new Error("此路線沒有 E順遞到付價");
const items = product.freightPriceItemList || [];
const fuelItem = items.find((item) => item.code === "IN15" || /燃油/.test(item.name || ""));
const resourceItem = items.find((item) => item.code === "IN104" || /資源|調節/.test(item.name || ""));
const result = {
base: numberOrNull(product.freight),
fuel: numberOrNull(fuelItem?.price ?? product.fuelFreight),
resource: numberOrNull(resourceItem?.price ?? 0),
total: numberOrNull(product.totalFreight),
chargedWeight: numberOrNull(product.weight),
currency: product.currencyCode,
};
if ([result.base, result.fuel, result.resource, result.total].some((item) => item === null)) {
throw new Error("順豐回傳的 E順遞拆價不完整");
}
if (result.currency !== "CNY") throw new Error(`預期 CNY,實際為 ${result.currency || "未知"}`);
return result;
}
async function mapWithConcurrency(items, limit, worker) {
const results = new Array(items.length);
let nextIndex = 0;
async function run() {
while (nextIndex < items.length) {
const index = nextIndex;
nextIndex += 1;
results[index] = await worker(items[index]);
}
}
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, run));
return results;
}
async function getOfficialRmbRates(shippingDateTime, force = false) {
const shippingTime = normalizeShippingTime(shippingDateTime);
const cacheKey = shippingTime;
const cached = rmbCache.get(cacheKey);
if (!force && cached && Date.now() < cached.expiresAt) return cached.payload;
const jobs = [];
for (const zone of Object.keys(RMB_ROUTES)) {
for (let index = 1; index <= 40; index += 1) {
jobs.push({ zone, weight: index / 2 });
}
}
const rates = { A: {}, B: {} };
const errors = [];
await mapWithConcurrency(jobs, 4, async ({ zone, weight }) => {
try {
rates[zone][weight.toFixed(1)] = await fetchOfficialRmbRate(zone, weight, shippingTime);
} catch (error) {
errors.push(`${zone}區 ${weight.toFixed(1)}kg:${error.message}`);
}
});
const count = Object.values(rates).reduce((sum, zoneRates) => sum + Object.keys(zoneRates).length, 0);
const payload = {
ok: count === jobs.length,
checkedAt: new Date().toISOString(),
shippingTime,
count,
expectedCount: jobs.length,
routes: RMB_ROUTES,
rates,
source: SF_PRICE_ENDPOINT,
errors: errors.slice(0, 12),
};
rmbCache.set(cacheKey, {
payload,
expiresAt: Date.now() + (payload.ok ? RMB_CACHE_MS : RMB_RETRY_CACHE_MS),
});
return payload;
}
async function getLiveRates(force = false) {
if (!force && liveCache.payload && Date.now() < liveCache.expiresAt) return liveCache.payload;
const [fuelResult, priceResult, resourceResult] = await Promise.allSettled([
fetchText(SOURCES.fuel).then(parseFuelPage),
fetchText(SOURCES.prices).then(parsePriceSource),
fetchText(SOURCES.resource).then(parseResourceFee),
]);
const errors = [];
if (fuelResult.status === "rejected") errors.push(`燃油費:${fuelResult.reason.message}`);
if (priceResult.status === "rejected") errors.push(`價目表:${priceResult.reason.message}`);
if (resourceResult.status === "rejected") errors.push(`調節費:${resourceResult.reason.message}`);
const payload = {
ok: fuelResult.status === "fulfilled",
checkedAt: new Date().toISOString(),
fuel: fuelResult.status === "fulfilled" ? fuelResult.value : null,
baseSource: priceResult.status === "fulfilled" ? priceResult.value : null,
resource: resourceResult.status === "fulfilled" ? resourceResult.value : null,
sources: SOURCES,
errors,
};
liveCache = { payload, expiresAt: Date.now() + CACHE_MS };
return payload;
}
function sendJson(response, status, value) {
response.writeHead(status, {
"content-type": "application/json; charset=utf-8",
"cache-control": "no-store",
});
response.end(JSON.stringify(value));
}
function serveFile(response, filePath, contentType) {
fs.readFile(filePath, (error, data) => {
if (error) {
response.writeHead(404, { "content-type": "text/plain; charset=utf-8" });
response.end("Not found");
return;
}
response.writeHead(200, { "content-type": contentType, "cache-control": "no-cache" });
response.end(data);
});
}
const server = http.createServer(async (request, response) => {
const url = new URL(request.url, `http://${HOST}`);
if (url.pathname === "/api/live-rates") {
try {
sendJson(response, 200, await getLiveRates(url.searchParams.get("force") === "1"));
} catch (error) {
sendJson(response, 500, { ok: false, errors: [error.message] });
}
return;
}
if (url.pathname === "/api/rmb-rates") {
try {
const payload = await getOfficialRmbRates(
url.searchParams.get("shippingDateTime"),
url.searchParams.get("force") === "1",
);
sendJson(response, payload.count ? 200 : 502, payload);
} catch (error) {
sendJson(response, 500, { ok: false, errors: [error.message] });
}
return;
}
if (url.pathname === "/" || url.pathname === "/index.html") {
serveFile(response, path.join(ROOT, "index.html"), "text/html; charset=utf-8");
return;
}
response.writeHead(404, { "content-type": "text/plain; charset=utf-8" });
response.end("Not found");
});
function onListening() {
const url = `http://${HOST}:${port}`;
console.log(`SF E-Ship rate tool started: ${url}`);
console.log("Press Ctrl+C to stop the server.");
if (process.env.OPEN_BROWSER !== "0") {
const browserCommand = process.platform === "win32"
? { command: "cmd.exe", args: ["/c", "start", "", url] }
: process.platform === "darwin"
? { command: "open", args: [url] }
: null;
if (browserCommand) {
const child = spawn(browserCommand.command, browserCommand.args, {
detached: true,
stdio: "ignore",
windowsHide: true,
});
child.unref();
}
}
}
function listen() {
server.once("error", (error) => {
if (error.code === "EADDRINUSE" && port < FIRST_PORT + 20) {
port += 1;
listen();
return;
}
throw error;
});
server.listen(port, HOST, onListening);
}
listen();