-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbracery-news.js
55 lines (50 loc) · 1.6 KB
/
bracery-news.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
44
45
46
47
48
49
50
51
52
53
54
55
/* This is a small AWS lambda function for scraping newsapi.org to Bracery.
*/
// Bracery web
const config = require('./bracery-config');
const util = require('./bracery-util');
// newsapi.org
const NEWS_API_KEY = process.env.NEWS_API_KEY; // must be defined from AWS Lambda
const NewsApiDomain = 'newsapi.org';
const NewsApiPathPrefix = '/v2/top-headlines?country=us&apiKey=';
const BracerySymbolName = 'news_story';
// DynamoDB
const dynamoPromise = util.dynamoPromise();
// The Lambda function
exports.handler = async (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
try {
const newsOpts = {
hostname: NewsApiDomain,
port: 443,
path: NewsApiPathPrefix + NEWS_API_KEY,
method: 'GET'
};
let [newsRes, newsData] = await util.httpsRequest (newsOpts);
if (newsRes.statusCode == 200) {
const newsBody = JSON.parse (newsData);
const newsBracery = '[' + newsBody.articles.map
((article) => {
let desc = article.description;
return util.escapeHTML
(typeof(article.author) === 'string'
? (article.author
.split(/ /)
.reduce
((title, authorWord) =>
(authorWord
? title.replace (new RegExp(authorWord,'g'), '')
: title),
desc))
: desc);
}).join('|') + ']';
var item = { name: BracerySymbolName,
bracery: newsBracery,
locked: true };
await util.updateBracery (item, dynamoPromise);
}
} catch (e) {
console.error (e)
throw e
}
};