xxxxxxxxxx
206
/*
建议:
1、出现文章的标题
2、把活跃的时间标注出来
api的数据是否可信
*/
let api = "https://api.nytimes.com/svc/search/v2/articlesearch.json?q=";
let input;
let apikey = '&api-key=YzRs5lSYgMFki1WbdkJ6uyhGIOStf8pu';
let headline, keyword, pubdate, author_fn, author_ln, authorName;
let headlineArray = [];
let keywordArray = [];
let pubdateArray = [];
let authorNameArray = [];
let button;
let authorMax,authorArticle;
let authorArticleArray = [];
function setup() {
// createCanvas(windowWidth, 200);
noCanvas();
button = select('#search');
button.mousePressed(searchNews);
}
function searchNews() {
let reporterInnerhtml = document.getElementById("reporter").innerHTML;
let articleInnerhtml = document.getElementById("article").innerHTML;
console.log(reporterInnerhtml);
reporterInnerhtml = " ";
articleInnerhtml = " ";
let tester = document.getElementById("input").innerHTML;
tester = tester.innerHTML;
if(tester !== ""){
input = select("#input").value();
let url = api + input + apikey;
loadJSON(url, getData);
console.log(input, url,"Your program is running!");
setInterval(detectLoading,1000);
}else{
document.getElementById("loading").innerHTML = "Type something to search";
}
}
function detectLoading(){
let reporterInnerhtml = document.getElementById("reporter").innerHTML;
let articleInnerhtml = document.getElementById("article").innerHTML;
if(reporterInnerhtml == ""){
document.getElementById("loading").innerHTML = "loading...";
}else if(reporterInnerhtml !== ""){
document.getElementById("loading").innerHTML = "";
}
}
function getData(data) {
// select("p").clear;
// select("h2").clear;
headlineArray = [];
keywordArray = [];
pubdateArray = [];
authorNameArray = [];
let docIndex = data.response.docs;
for (let i = 0; i < docIndex.length; i++) {
headline = docIndex[i].headline.main;
keyword = docIndex[i].keywords[0].value;
pubdate = docIndex[i].pub_date;
let p = docIndex[i].byline.person.length;
// console.log(p);
if (p > 0) {
for (let j = 0; j < p; j++) {
author_fn = docIndex[i].byline.person[j].firstname;
author_ln = docIndex[i].byline.person[j].lastname;
authorName = author_fn + " " + author_ln;
}
}
// createP(headline);
// createP(keyword);
// createP(pubdate);
// createP(authorName);
headlineArray.push(docIndex[i].headline.main);
keywordArray.push(docIndex[i].keywords[0].value);
pubdateArray.push(docIndex[i].pub_date);
if (p > 0) {
for (let j = 0; j < p; j++) {
author_fn = docIndex[i].byline.person[j].firstname;
author_ln = docIndex[i].byline.person[j].lastname;
authorNameArray.push(author_fn + " " + author_ln);
// authorNameArray.push('<br/>' + author_fn + " " + author_ln + i + ":" + j);
}
}
}
// console.log(headlineArray, keywordArray, pubdateArray, authorNameArray);
// createElement("h2", headlineArray);
// createElement("p", keywordArray);
// createElement("h4", pubdateArray); //date comparasion?
// createElement("p", authorNameArray);
/* ----------------------------------------
--------------- calculation ---------------
---------------------------------------- */
let calArray = [];
authorArticleArray = [];
for(let i=0;i<authorNameArray.length;i++){
let cal = 0;
for(let j=0;j<authorNameArray.length;j++){
if(authorNameArray[i] == authorNameArray[j] && i!== j){
cal ++;
}
}
calArray.push(cal);
//authorNameArray[i].
console.log(authorNameArray,calArray);
}
let calMax = calArray[0];
for(let i = 0;i < calArray.length;i++){
for(let j = 0;j < calArray.length;j++){
if(calArray[j]>calArray[i]){
calMax = calArray[j];
}else{
calMax = calArray[i];
}
}
}
authorMax = authorNameArray[calMax];
console.log(calMax,authorMax);
document.getElementById("reporter").innerHTML = authorMax;
// select(".h2").innerHTML(authorMax);
for(let i=0; i<docIndex.length; i++){
let p = docIndex[i].byline.person.length;
if (p > 0) {
for (let j = 0; j < p; j++) {
author_fn = docIndex[i].byline.person[j].firstname;
author_ln = docIndex[i].byline.person[j].lastname;
authorName = author_fn + " " + author_ln;
if(authorName == authorMax){
authorArticle = docIndex[i].headline.main;
authorArticleArray.push(authorArticle);
// select("p").add(authorDetail);
// createP(authorDetail);
}
}
}
}
authorArticleSplit = authorArticleArray.join("<br/>");
document.getElementById("article").innerHTML = authorArticleSplit;
console.log(authorArticle);
console.log("These're all the articles: ", authorArticleArray);
}
// function draw() {
// background(250);
// textSize(30);
// textAlign(CENTER,CENTER);
// textFont(font1);
// text(authorMax,windowWidth/2,50);
// push();
// textSize(14);
// textStyle(ITALIC);
// fill('grey');
// for(let i=0; i<authorDetail.length; i++){
// text(authorDetail[i],windowWidth/2*0.5, 5,windowWidth/2,200);
// }
// pop();
// }