word-def 1.1.2 → 1.1.4
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/index.js +111 -55
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -43,80 +43,136 @@ for (let i = 0; i < args.length; i++) {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
// Make search case-insensitive
|
|
47
|
-
const searchWord = word.toLowerCase();
|
|
48
|
-
|
|
49
46
|
if (!word) {
|
|
50
47
|
showHelp();
|
|
51
48
|
process.exit(1);
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
// Make search case-insensitive - try original first, then lowercase
|
|
52
|
+
const searchWord = word.toLowerCase();
|
|
53
|
+
const originalWord = word;
|
|
56
54
|
|
|
57
|
-
|
|
55
|
+
const langName = wiktionaryLanguages[lang] || lang;
|
|
56
|
+
fetchFromWiktionary(originalWord, searchWord, lang);
|
|
57
|
+
|
|
58
|
+
function fetchFromWiktionary(originalWord, lowerWord, targetLang) {
|
|
59
|
+
// For German and other languages with capitalized nouns, try original case first
|
|
60
|
+
// Then lowercase, then capitalized first letter
|
|
61
|
+
const wordsToTry = [originalWord];
|
|
62
|
+
|
|
63
|
+
// Only add lowercase if different from original
|
|
64
|
+
if (originalWord.toLowerCase() !== originalWord) {
|
|
65
|
+
wordsToTry.push(originalWord.toLowerCase());
|
|
66
|
+
}
|
|
67
|
+
|
|
58
68
|
// Try the language-specific Wiktionary first
|
|
59
69
|
const langCodes = [
|
|
60
70
|
targetLang,
|
|
61
71
|
targetLang === 'en' ? null : 'en' // fallback to English
|
|
62
72
|
];
|
|
63
73
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
hostname,
|
|
78
|
-
path,
|
|
79
|
-
method: 'GET',
|
|
80
|
-
headers: {
|
|
81
|
-
'Accept': 'application/json',
|
|
82
|
-
'User-Agent': 'word-def-cli/1.0'
|
|
74
|
+
let wordIndex = 0;
|
|
75
|
+
let langIndex = 0;
|
|
76
|
+
let retries = 0;
|
|
77
|
+
const MAX_RETRIES = 2;
|
|
78
|
+
const RETRY_DELAY = 1000;
|
|
79
|
+
|
|
80
|
+
function tryNext() {
|
|
81
|
+
// Try next language
|
|
82
|
+
while (langIndex < langCodes.length) {
|
|
83
|
+
const currentLang = langCodes[langIndex];
|
|
84
|
+
if (!currentLang) {
|
|
85
|
+
langIndex++;
|
|
86
|
+
continue;
|
|
83
87
|
}
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
https.get(options, (res) => {
|
|
87
|
-
let data = '';
|
|
88
|
-
|
|
89
|
-
res.on('data', chunk => data += chunk);
|
|
90
|
-
res.on('end', () => {
|
|
91
|
-
try {
|
|
92
|
-
const parsed = JSON.parse(data);
|
|
93
|
-
|
|
94
|
-
// Check if we got a valid response with definitions
|
|
95
|
-
if (!parsed || Object.keys(parsed).length === 0) {
|
|
96
|
-
tryNextLanguage(index + 1);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
88
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
89
|
+
const currentWord = wordsToTry[wordIndex];
|
|
90
|
+
const hostname = currentLang === 'en' ? 'en.wiktionary.org' : `${currentLang}.wiktionary.org`;
|
|
91
|
+
const path = `/api/rest_v1/page/definition/${encodeURIComponent(currentWord)}`;
|
|
92
|
+
|
|
93
|
+
const options = {
|
|
94
|
+
hostname,
|
|
95
|
+
path,
|
|
96
|
+
method: 'GET',
|
|
97
|
+
headers: {
|
|
98
|
+
'Accept': 'application/json',
|
|
99
|
+
'User-Agent': 'word-def-cli/1.1'
|
|
100
|
+
}
|
|
101
|
+
};
|
|
104
102
|
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
const req = https.get(options, (res) => {
|
|
104
|
+
let data = '';
|
|
105
|
+
|
|
106
|
+
res.on('data', chunk => data += chunk);
|
|
107
|
+
res.on('end', () => {
|
|
108
|
+
// Rate limited - retry after delay
|
|
109
|
+
if (res.statusCode === 429 && retries < MAX_RETRIES) {
|
|
110
|
+
retries++;
|
|
111
|
+
setTimeout(tryNext, RETRY_DELAY * retries);
|
|
107
112
|
return;
|
|
108
113
|
}
|
|
109
114
|
|
|
110
|
-
|
|
115
|
+
try {
|
|
116
|
+
const parsed = JSON.parse(data);
|
|
117
|
+
|
|
118
|
+
// Check if we got an error response
|
|
119
|
+
if (!parsed || typeof parsed !== 'object' || Object.keys(parsed).length === 0) {
|
|
120
|
+
langIndex++;
|
|
121
|
+
retries = 0;
|
|
122
|
+
tryNext();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Find definitions in the target language
|
|
127
|
+
const targetDefs = parsed[targetLang];
|
|
128
|
+
const fallbackDefs = parsed['en'];
|
|
129
|
+
const definitions = targetDefs || fallbackDefs;
|
|
130
|
+
|
|
131
|
+
if (!definitions || definitions.length === 0) {
|
|
132
|
+
// Move to next language
|
|
133
|
+
langIndex++;
|
|
134
|
+
retries = 0;
|
|
135
|
+
tryNext();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Success! Display and exit
|
|
140
|
+
displayDefinitions(currentWord, targetLang, definitions, parsed);
|
|
141
|
+
|
|
142
|
+
} catch (e) {
|
|
143
|
+
// Move to next language
|
|
144
|
+
langIndex++;
|
|
145
|
+
retries = 0;
|
|
146
|
+
tryNext();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
111
150
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
151
|
+
req.on('error', () => {
|
|
152
|
+
// Network error - try next language
|
|
153
|
+
langIndex++;
|
|
154
|
+
retries = 0;
|
|
155
|
+
tryNext();
|
|
115
156
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
157
|
+
|
|
158
|
+
return; // Wait for response
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Tried all languages for current word, try next word
|
|
162
|
+
wordIndex++;
|
|
163
|
+
langIndex = 0;
|
|
164
|
+
retries = 0;
|
|
165
|
+
|
|
166
|
+
if (wordIndex < wordsToTry.length) {
|
|
167
|
+
tryNext();
|
|
168
|
+
} else {
|
|
169
|
+
// Exhausted all options
|
|
170
|
+
console.log(`❌ No definition found for "${originalWord}" (${langName})`);
|
|
171
|
+
process.exit(1);
|
|
172
|
+
}
|
|
119
173
|
}
|
|
174
|
+
|
|
175
|
+
tryNext();
|
|
120
176
|
}
|
|
121
177
|
|
|
122
178
|
function displayDefinitions(word, lang, definitions, fullData) {
|
|
@@ -168,9 +224,9 @@ Examples:
|
|
|
168
224
|
word-def chien --lang fr # French
|
|
169
225
|
word-def Hund --lang de # German
|
|
170
226
|
word-def cane --lang it # Italian
|
|
171
|
-
word-def狗 --lang zh # Chinese
|
|
227
|
+
word-def 狗 --lang zh # Chinese
|
|
172
228
|
word-def собака --lang ru # Russian
|
|
173
|
-
|
|
229
|
+
word-def 犬 --lang ja # Japanese
|
|
174
230
|
|
|
175
231
|
Supported languages:
|
|
176
232
|
${Object.entries(wiktionaryLanguages).slice(0, 12).map(([c, n]) => ` ${c} - ${n}`).join('\n')}
|