Skip to content
This repository was archived by the owner on Jun 9, 2022. It is now read-only.

Commit 18cc510

Browse files
committed
working aggregator
1 parent b18cde2 commit 18cc510

File tree

9 files changed

+67
-6
lines changed

9 files changed

+67
-6
lines changed

backend/aggregator.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,46 @@
11
"use strict";
2+
const request = require("request-promise");
3+
const Bottleneck = require("bottleneck");
4+
5+
const limiter = new Bottleneck({
6+
maxConcurrent: 1,
7+
minTime: 1000
8+
});
9+
210
const fetchArticles = require("./lib/fetchArticles");
311

12+
const SENTIMENT_URL = process.env.SENTIMENT_URL;
13+
414
module.exports.main = async event => {
515
try {
616
const searchTerm = event.queryStringParameters.searchTerm;
717

818
const rawSapiResult = await fetchArticles.search(searchTerm);
919
const uuids = rawSapiResult.sapiObj.results[0].results.map(obj => obj.id);
20+
console.log(SENTIMENT_URL);
21+
22+
const result = await Promise.all(
23+
uuids.map(uuid =>
24+
limiter.schedule(async () => {
25+
const result = await request(createParams(uuid));
26+
if (result !== "not article") {
27+
return JSON.parse(result);
28+
}
29+
return result;
30+
})
31+
)
32+
);
1033
return {
1134
statusCode: 200,
12-
body: JSON.stringify(uuids),
35+
body: JSON.stringify(result),
1336
headers: {
1437
"Access-Control-Allow-Origin": "*"
1538
}
1639
};
1740
} catch (err) {
18-
console.error(err);
41+
console.log(err);
42+
console.log("GETTING IN ERROR");
43+
console.error(JSON.stringify(err));
1944
return {
2045
statusCode: 401,
2146
body: "Something went wrong",
@@ -25,3 +50,11 @@ module.exports.main = async event => {
2550
};
2651
}
2752
};
53+
54+
function createParams(uuid) {
55+
return {
56+
method: "GET",
57+
url: SENTIMENT_URL + uuid + "?provider=meaningCloud,aws",
58+
headers: { "content-type": "application/json" }
59+
};
60+
}

backend/handler.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,25 @@ module.exports.main = async event => {
3939
}
4040
const uuid = event.pathParameters.uuid;
4141
const result = await fetchContent.getArticle(uuid);
42+
if (!result.bodyXML || !result.title || !result.standfirst) {
43+
console.log("getting in catch");
44+
return {
45+
statusCode: 200,
46+
body: "not article",
47+
headers: {
48+
"Access-Control-Allow-Origin": "*"
49+
}
50+
};
51+
}
52+
console.log(JSON.stringify(result));
4253
const articleContent = extractText(result.bodyXML);
4354
const title = result.title;
4455
const standfirst = result.standfirst;
4556

57+
console.log("articleContent", articleContent);
58+
console.log("title", title);
59+
console.log("standfirst", standfirst);
60+
4661
const sentimentResult = await Promise.all(
4762
finalProviderArray.map(async provider => {
4863
const result = await provider.functionality.getSentiment({
@@ -68,10 +83,10 @@ module.exports.main = async event => {
6883
}
6984
};
7085
} catch (err) {
71-
console.error(err);
86+
console.error(JSON.stringify(err));
7287
return {
7388
statusCode: 401,
74-
body: "Something went wrong",
89+
body: "Something went wrong in handler",
7590
headers: {
7691
"Access-Control-Allow-Origin": "*"
7792
}

backend/lib/fetchArticles.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const searchBody = {
1111
curations: ["ARTICLES"]
1212
},
1313
resultContext: {
14-
maxResults: "10",
14+
maxResults: "5",
1515
offset: "0",
1616
aspects: [
1717
"audioVisual",
@@ -48,7 +48,7 @@ const searchBody = {
4848
function constructSAPIQuery(params) {
4949
const defaults = {
5050
queryString: "",
51-
maxResults: 10,
51+
maxResults: 5,
5252
offset: 0,
5353
aspects: ["title", "lifecycle", "location"], // [ "title", "location", "summary", "lifecycle", "metadata"],
5454
constraints: [],

backend/package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"homepage": "https://github.com/ftlabs/sentiment#readme",
2222
"dependencies": {
2323
"aws-sdk": "^2.551.0",
24+
"bottleneck": "^2.19.5",
2425
"ibm-watson": "^5.1.0",
2526
"node-fetch": "^2.6.0",
2627
"request": "^2.88.0",

backend/serverless.yml

+3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ service: sentiment
33
provider:
44
name: aws
55
runtime: nodejs10.x
6+
timeout: 30
7+
memorySize: 3008
68
environment:
79
CAPI_KEY: "${file(./serverless.env.yml):CAPI_API_KEY}"
810
TONE_ANALYZER_IAM_APIKEY: "${file(./serverless.env.yml):TONE_ANALYZER_IAM_APIKEY}"
911
TONE_ANALYZER_URL: "${file(./serverless.env.yml):TONE_ANALYZER_URL}"
1012
TEST: "${file(./serverless.env.yml):TEST}"
1113
MEANING_CLOUD_URL: "${file(./serverless.env.yml):MEANING_CLOUD_URL}"
1214
MEANING_CLOUD_APIKEY: "${file(./serverless.env.yml):MEANING_CLOUD_APIKEY}"
15+
SENTIMENT_URL: "https://umwi7bvika.execute-api.us-east-1.amazonaws.com/${opt:stage, self:provider.stage}/ftlabs-sentiment/"
1316
stackTags:
1417
environment: p
1518

backend/services/aws.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const truncate = require("truncate-utf8-bytes");
33
const comprehend = new AWS.Comprehend();
44

55
async function getSentiment({ articleContent, title, standfirst }) {
6+
console.log("aws", articleContent, title, standfirst);
67
const result = await Promise.all([
78
comprehend
89
.detectSentiment({

backend/services/ibm.js

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const toneAnalyzer = new ToneAnalyzerV3({
1111
});
1212

1313
async function getSentiment({ articleContent, standfirst, title }) {
14+
console.log("ibm", articleContent, title, standfirst);
15+
1416
const result = await Promise.all([
1517
new Promise(function(resolve, reject) {
1618
toneAnalyzer.tone(

backend/services/meaningCloud.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const URL = process.env.MEANING_CLOUD_URL;
33
const API_KEY = process.env.MEANING_CLOUD_APIKEY;
44

55
async function getSentiment({ articleContent, title, standfirst }) {
6+
console.log("meaningCloud", articleContent, title, standfirst);
67
var request = require("request-promise");
78

89
const result = await Promise.all([

0 commit comments

Comments
 (0)