utilitas 1998.2.37 → 1998.2.39
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/utilitas.lite.mjs +1 -1
- package/dist/utilitas.lite.mjs.map +1 -1
- package/lib/alan.mjs +286 -290
- package/lib/manifest.mjs +1 -1
- package/lib/shot.mjs +21 -58
- package/package.json +1 -1
package/lib/manifest.mjs
CHANGED
package/lib/shot.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import { sha256 } from './encryption.mjs';
|
|
|
5
5
|
import { distillHtml } from './web.mjs';
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
|
+
ensureInt,
|
|
8
9
|
ensureString, extract, ignoreErrFunc, inBrowser, parseJson, parseVersion,
|
|
9
10
|
throwError, which
|
|
10
11
|
} from './utilitas.mjs';
|
|
@@ -160,66 +161,28 @@ const initSearch = (options) => {
|
|
|
160
161
|
};
|
|
161
162
|
|
|
162
163
|
const search = async (query, options) => {
|
|
164
|
+
const [key, cx, min, max]
|
|
165
|
+
= [options?.apiKey || googleApiKey, options?.cx || googleCx, 1, 10];
|
|
163
166
|
assert(query, 'Query is required.');
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
assert(cx, 'CX is required.');
|
|
174
|
-
url = 'https://www.googleapis.com/customsearch/v1'
|
|
175
|
-
+ `?key=${encodeURIComponent(apiKey)}`
|
|
176
|
-
+ `&cx=${encodeURIComponent(cx)}`
|
|
177
|
-
+ `&q=${encodeURIComponent(query)}`;
|
|
178
|
-
break;
|
|
179
|
-
case DUCKDUCKGO:
|
|
180
|
-
url = 'https://api.duckduckgo.com/'
|
|
181
|
-
+ `?q=${encodeURIComponent(query)}&format=json&skip_disambig=1`;
|
|
182
|
-
parser = x => x.FirstURL ? {
|
|
183
|
-
title: x.FirstURL.replace(/^.*\/([^\/]*)$/, '$1').replace(/_/g, ' '),
|
|
184
|
-
link: x.FirstURL, snippet: x.Text,
|
|
185
|
-
image: `https://duckduckgo.com${x.Icon.URL}`, source: null,
|
|
186
|
-
} : null;
|
|
187
|
-
break;
|
|
188
|
-
default:
|
|
189
|
-
throwError(`Invalid provider: ${provider}.`);
|
|
190
|
-
}
|
|
167
|
+
assert(key, 'API key is required.');
|
|
168
|
+
assert(cx, 'CX is required.');
|
|
169
|
+
const num = ensureInt(options?.num || max, { min, max });
|
|
170
|
+
const start = ensureInt(options?.start || min, { min });
|
|
171
|
+
assert(start + num <= 100, 'Reached maximum search limit.');
|
|
172
|
+
const url = 'https://www.googleapis.com/customsearch/v1'
|
|
173
|
+
+ `?key=${encodeURIComponent(key)}&cx=${encodeURIComponent(cx)}`
|
|
174
|
+
+ `&q=${encodeURIComponent(query)}&num=${num}&start=${start}`
|
|
175
|
+
+ (options?.image ? `&searchType=image` : '');
|
|
191
176
|
const resp = await get(url, { encode: _JSON, ...options || {} });
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
image: x.pagemap?.cse_image?.[0]?.src || null,
|
|
199
|
-
}))
|
|
200
|
-
}
|
|
201
|
-
const cnt = resp?.content;
|
|
202
|
-
if (cnt?.Abstract) {
|
|
203
|
-
result.push({
|
|
204
|
-
title: cnt?.Heading + (cnt?.Entity ? ` (${cnt.Entity})` : ''),
|
|
205
|
-
link: cnt?.AbstractURL, snippet: cnt?.AbstractText,
|
|
206
|
-
image: cnt?.Image ? `https://duckduckgo.com${cnt.Image}` : null,
|
|
207
|
-
source: cnt?.AbstractSource,
|
|
208
|
-
});
|
|
209
|
-
if (cnt?.Results) {
|
|
210
|
-
result.push(...cnt.Results.map(x => ({
|
|
211
|
-
title: x.Text, link: x.FirstURL, snippet: null,
|
|
212
|
-
image: x.Icon ? `https://duckduckgo.com${x.Icon.URL}` : null,
|
|
213
|
-
source: null,
|
|
214
|
-
})));
|
|
215
|
-
}
|
|
216
|
-
} else if (cnt?.RelatedTopics?.[0] && !options?.no_recurse) {
|
|
217
|
-
result = await search(cnt.RelatedTopics[0].FirstURL.replace(
|
|
218
|
-
/^.*\/([^\/]*)$/, '$1'
|
|
219
|
-
).replace(/_/g, ' '), { ...options, no_recurse: true });
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
return result;
|
|
177
|
+
return options?.raw ? resp.content : {
|
|
178
|
+
totalResults: resp?.content?.searchInformation?.totalResults || 0,
|
|
179
|
+
startIndex: resp?.content?.queries?.request?.[0]?.startIndex || 1,
|
|
180
|
+
items: resp?.content?.items.map(x => ({
|
|
181
|
+
title: x.title, link: options?.image ? null : x.link,
|
|
182
|
+
snippet: x.snippet,
|
|
183
|
+
image: (options?.image ? x.link : x.pagemap?.cse_image?.[0]?.src) || null,
|
|
184
|
+
})),
|
|
185
|
+
};
|
|
223
186
|
};
|
|
224
187
|
|
|
225
188
|
export default get;
|