Repository navigation
Expand file tree
/
Copy pathsearch.js
More file actions
89 lines (84 loc) · 2.62 KB
/
Copy pathsearch.js
File metadata and controls
89 lines (84 loc) · 2.62 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
/**
* shipxy search — 关键字搜索船舶
*
* 输入:船名 / MMSI / IMO / 呼号(1个或多个,逗号或空格分隔)
* 输出:候选船舶列表(mmsi/imo/name/callsign/type)
*
* 策略:PUBLIC(走 searchv4.shipxy.com/index.ashx?f=srch,无需登录)
*
* 例:
* opencli shipxy search --kw COSCO12
* opencli shipxy search --kw "413543265,MAERSK,9525338" --limit 5
*/
import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, EmptyResultError } from '@jackwener/opencli/errors';
import { searchByKeyword, shipTypeLabel } from './shared/helpers.js';
cli({
site: 'shipxy',
name: 'search',
access: 'read',
description: '船讯网关键字搜索:按船名 / MMSI / IMO / 呼号返回候选船舶列表',
domain: 'searchv4.shipxy.com',
strategy: Strategy.PUBLIC,
browser: false,
example: 'opencli shipxy search --kw "COSCO12,MAERSK" --limit 5',
args: [
{ name: 'kw', type: 'string', help: '关键字(船名 / MMSI / IMO / 呼号),多个用逗号或空格分隔' },
{ name: 'limit', type: 'int', default: 10, help: '每个关键字返回条数 (max 50)' },
],
columns: ['keyword', 'mmsi', 'imo', 'name', 'callsign', 'type'],
func: async (args) => {
const raw = String(args.kw ?? '').trim();
if (!raw) throw new ArgumentError('--kw 必填');
const limit = Number(args.limit ?? 10);
if (!Number.isInteger(limit) || limit <= 0 || limit > 50) {
throw new ArgumentError('--limit 必须是 1..50 的整数');
}
const keywords = raw
.split(/[,\s]+/)
.map((s) => s.trim())
.filter(Boolean);
const rows = [];
for (const kw of keywords) {
let ships;
try {
ships = await searchByKeyword(kw, { limit });
} catch (e) {
rows.push({
keyword: kw,
mmsi: '',
imo: '',
name: `[error] ${e.message || e}`,
callsign: '',
type: '',
});
continue;
}
if (!ships.length) {
rows.push({
keyword: kw,
mmsi: '',
imo: '',
name: '[no-match]',
callsign: '',
type: '',
});
continue;
}
for (const s of ships) {
rows.push({
keyword: kw,
mmsi: s.m ? String(s.m) : '',
imo: s.i && s.i !== 0 ? String(s.i) : '',
name: s.n || '',
callsign: s.c || '',
type: shipTypeLabel(s.t) + (s.t != null ? ` (${s.t})` : ''),
});
}
}
if (rows.length === 0) {
throw new EmptyResultError('shipxy search', '没有匹配的船舶');
}
return rows;
},
});