xxxxxxxxxx
353
let state = 0;
let radio2;
let topStoryButton;
let searchStoryButton;
let backButton;
let searchArtsButton;
let searchHomeButton;
let searchScienceButton;
let searchUSButton;
let searchWorldButton;
let selectedTopic = 'arts';
let shouldSearch = true;
let AFINN;
let topStoryResults;
let searchInput;
let hasVisitedSearch = false;
let isLoading = false;
let searchArticleResults = [];
let searchTerm;
const nytApiKey = 'otMhcdzXQYCrN8NkJuJfbMlKVGItb8LP';
async function callNYTTopStoryApi() {
let nytAPIUrl = `https://api.nytimes.com/svc/topstories/v2/${selectedTopic}.json?api-key=${nytApiKey}`;
isLoading = true;
await httpGet(nytAPIUrl, false, function(response) {
isLoading = false;
topStoryResults = response.results;
});
}
async function getHistoricalArticles() {
searchTerm = searchInput.value();
let nytAPIUrl = `https://api.nytimes.com/svc/search/v2/articlesearch.json?q=${searchTerm}&api-key=${nytApiKey}`
isLoading = true;
await httpGet(nytAPIUrl, false, function(response) {
isLoading = false;
response.response.docs.forEach((article) => {
if ('abstract' in article && 'web_url' in article && 'snippet' in article) {
searchArticleResults.push(article)
}
})
drawSearchStories();
});
}
function getColorFromSentimentScore(sentimentScore) {
/*
Red 255, 0, 0
Yellow 255, 255, 0
Green 0, 255, 0
*/
let rCol, bCol, gCol;
if (sentimentScore > 0) {
rCol = map(sentimentScore, 1, 10, 255, 0);
bCol = 255;
gCol = 0;
} else {
rCol = 255;
bCol = map(sentimentScore, 0, -10, 255, 0);
gCol = 0;
}
return color(rCol, bCol, gCol);
}
function drawIntro() {
removeElements();
background('#f4f2f2');
textStyle(NORMAL);
fill('black');
textFont('Georgia');
textSize(28);
text('New York Times Sentiment Analysis', 20, 60);
rect(10, 70, 500, 0.5);
textSize(12);
text('How is the news feeling today?', 20, 90);
textSize(18);
text('This app attaches and assists a user by supplementing the \nsentiment analysis of top articles and historical stories \nfrom the New York Times.', 20, 140);
textSize(24);
text('Select one of the following:', 20, 240);
topStoryButton = createButton('View Top Stories');
topStoryButton.position(19, 275);
topStoryButton.size(200, 100);
topStoryButton.style('background-color', color(25, 23, 200, 50));
topStoryButton.mousePressed(() => {
drawTopStories();
});
topStoryButton.style('font-size', '20px');
searchStoryButton = createButton('Search Historical Articles');
searchStoryButton.position(300, 275);
searchStoryButton.size(200, 100);
searchStoryButton.style('background-color', color(25, 23, 200, 50));
searchStoryButton.mousePressed(() => {
drawSearchStories();
});
searchStoryButton.style('font-size', '20px');
textSize(16);
textStyle(ITALIC);
text('NOTE: We do no track any of your browsing habits or data. This project \nis not associated with the New York Times.', 20, 440);
textSize(12);
textStyle(ITALIC);
text('Jason Brill - 2020', 495, 580);
}
function drawSearchStories() {
removeElements();
background('#f4f2f2');
fill('black');
textFont('Georgia');
textSize(28);
text('Search Historical Articles', 20, 60);
rect(10, 70, 500, 0.5);
textSize(12);
text('How is the news feeling today?', 20, 90);
searchInput = createInput();
searchInput.position(20, 105);
if (searchTerm) {
textSize(12);
text(`Search Term:\t${searchTerm}`, 400, 120);
}
let searchButton = createButton('Search');
searchButton.position(searchInput.x + searchInput.width, 105);
searchButton.value(searchTerm)
searchButton.mousePressed(async () => {
isLoading = true;
searchArticleResults = [];
removeElements();
background('#f4f2f2');
fill('black');
textFont('Georgia');
textSize(28);
text('Search Historical Articles', 20, 60);
rect(10, 70, 500, 0.5);
textSize(12);
text('How is the news feeling today?', 20, 90);
textSize(12);
text('Loading', 250, 250);
await getHistoricalArticles();
});
console.log(searchArticleResults)
if (searchArticleResults) {
console.log(searchArticleResults)
for (let resultIdx in searchArticleResults) {
if (resultIdx == 4) {
break;
}
let topStoryScore = analyze(searchArticleResults[resultIdx].abstract) + analyze(searchArticleResults[resultIdx].snippet);
let color = getColorFromSentimentScore(topStoryScore);
fill(color);
console.log(searchArticleResults[resultIdx])
let anchor = createA(searchArticleResults[resultIdx].web_url, 'link', ['_blank']);
anchor.position(40, 220 + (85 * resultIdx))
rect(20, 180 + (85 * resultIdx), 550, 70);
fill('black');
textSize(12);
text(searchArticleResults[resultIdx].headline.main, 40, 210 + (85 * resultIdx));
text(topStoryScore, 540, 220 + (85 * resultIdx));
}
}
backButton = createButton('Back');
backButton.position(19, 550);
backButton.size(60, 35);
backButton.style('font-size', '10px');
backButton.mousePressed(() => {
drawIntro();
backButton = null;
});
}
async function drawTopStories() {
removeElements();
background('#f4f2f2');
fill('black');
textFont('Georgia');
textSize(28);
text('Top Stories for the Day', 20, 60);
rect(10, 70, 500, 0.5);
textSize(12);
text('Select a topic', 20, 90);
textSize(12);
text('Loading', 250, 250);
await callNYTTopStoryApi(selectedTopic);
searchArtsButton = createButton('Arts');
searchArtsButton.position(20, 100);
searchArtsButton.size(75, 40);
searchArtsButton.style('background-color', color(25, 23, 200, 50));
searchArtsButton.mousePressed(() => {
topStoryResults = null;
selectedTopic = 'arts';
drawTopStories();
});
searchArtsButton.style('font-size', '12px');
searchHomeButton = createButton('Home');
searchHomeButton.position(125, 100);
searchHomeButton.size(75, 40);
searchHomeButton.style('background-color', color(25, 23, 200, 50));
searchHomeButton.mousePressed(() => {
topStoryResults = null;
selectedTopic = 'home';
drawTopStories();
});
searchHomeButton.style('font-size', '12px');
searchScienceButton = createButton('Science');
searchScienceButton.position(230, 100);
searchScienceButton.size(75, 40);
searchScienceButton.style('background-color', color(25, 23, 200, 50));
searchScienceButton.mousePressed(() => {
topStoryResults = null;
selectedTopic = 'science';
drawTopStories();
});
searchScienceButton.style('font-size', '12px');
searchUSButton = createButton('US');
searchUSButton.position(335, 100);
searchUSButton.size(75, 40);
searchUSButton.style('background-color', color(25, 23, 200, 50));
searchUSButton.mousePressed(() => {
topStoryResults = null;
selectedTopic = 'us';
drawTopStories();
});
searchUSButton.style('font-size', '12px');
searchWorldButton = createButton('World');
searchWorldButton.position(440, 100);
searchWorldButton.size(75, 40);
searchWorldButton.style('background-color', color(25, 23, 200, 50));
searchWorldButton.mousePressed(() => {
topStoryResults = null;
selectedTopic = 'world';
drawTopStories();
});
searchWorldButton.style('font-size', '12px');
switch (selectedTopic) {
case 'arts':
searchArtsButton.style('background-color', color(255, 23, 200, 50));
break;
case 'home':
searchHomeButton.style('background-color', color(255, 23, 200, 50));
break;
case 'science':
searchScienceButton.style('background-color', color(255, 23, 200, 50));
break;
case 'us':
searchUSButton.style('background-color', color(255, 23, 200, 50));
break;
case 'world':
searchWorldButton.style('background-color', color(255, 23, 200, 50));
break;
}
textSize(28);
text('Results', 20, 200);
// if (isLoading) {
// }
if (topStoryResults) {
for (let resultIdx in topStoryResults) {
if (resultIdx == 4) {
break;
}
let topStoryScore = analyze(topStoryResults[resultIdx].abstract) + analyze(topStoryResults[resultIdx].title);
let color = getColorFromSentimentScore(topStoryScore);
fill(color);
console.log(topStoryResults[resultIdx])
let anchor = createA(topStoryResults[resultIdx].url, 'link', ['_blank']);
anchor.position(40, 260 + (85 * resultIdx))
rect(20, 220 + (85 * resultIdx), 550, 70);
fill('black');
textSize(12);
text(topStoryResults[resultIdx].title, 40, 250 + (85 * resultIdx));
text(topStoryScore, 540, 270 + (85 * resultIdx));
}
}
backButton = createButton('Back');
backButton.position(19, 550);
backButton.size(60, 35);
backButton.style('font-size', '10px');
backButton.mousePressed(() => {
drawIntro();
backButton = null;
});
}
function tokenize(text) {
return text.toLowerCase().split(" ");
}
function deleteUselessChars(word) {
return word.replace(/[^\w]/g, "");
}
function rateWord(word) {
return (word in AFINN) ? AFINN[word] : 0;
}
function sum(x, y) {
return int(x) + int(y);
}
function analyze(text) {
return tokenize(text).map(deleteUselessChars).map(rateWord).reduce(sum);
}
function preload() {
AFINN = loadJSON("./AFINN.json");
}
function setup() {
createCanvas(600, 600);
frameRate(60);
removeElements();
background('#f4f2f2');
if (state === 0) {
drawIntro();
}
if (state === 1) {
drawTopStories();
}
if (state === 2) {
drawSearchStories();
}
}
function draw() {
}