-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathindex.js
43 lines (40 loc) · 1.39 KB
/
index.js
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
async function search_latest_news({
q,
qInTitle,
country = 'us',
category,
language = 'en',
domain,
page
}) {
const queryParams = new URLSearchParams({
apikey: process.env.API_KEY,
country,
language,
});
q && queryParams.append('q', q);
qInTitle && queryParams.append('qInTitle', qInTitle);
category && queryParams.append('category', category);
domain && queryParams.append('domain', domain);
page && queryParams.append('page', page);
const url = `https://newsdata.io/api/1/news?${queryParams.toString()}`;
ais_progress(`Accessing: ${url.replace(process.env.API_KEY, '***')}`);
return await fetch(url).then(res => {
// 保留3条
return res.json().then(json => {
const {status, totalResults, results, nextPage} = json;
if (status === 'success') {
return JSON.stringify({
status,
totalResults,
articles: results.slice(0, 3),
nextPage
});
}
return JSON.stringify(json);
});
});
}
// Example usage:
// process.env.API_KEY = '';
// search_latest_news({q: 'pizza', country: 'au', language: 'en'}).then(console.log);