-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
114 lines (89 loc) · 3.02 KB
/
logic.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//set up variables
var authKey="ceadb42d410e487d884bd07d6764a53d";
var searchTerm = '';
var startYear = 0;
var endYear = 0;
var numResult = 0;
var urlBase = "https://api.nytimes.com/svc/search/v2/articlesearch.json";
urlBase += '?' + $.param({
'api-key': authKey,
'q': 'barack obama',
});
var articleCounter = 0;
function runQuery(numberarticles, queryURL){
$.ajax({
url: urlBase,
method: 'GET',
}).done(function(NYTData){
for (var i=0; i < numResult; i++){
console.log(NYTData.response.docs[i].web_url);
console.log(NYTData.response.docs[i].headline.main);
console.log(NYTData.response.docs[i].section_name);
console.log(NYTData.response.docs[i].pub_date);
var wellSection = $('<div>');
wellSection.addClass("well");
wellSection.attr('id', 'article-'+i);
$('#wellSection').append(wellSection);
$('#article-'+i).append('<h3>'+NYTData.response.docs[i].headline.main +'</h3>');
$('#article-'+i).append('<h5>'+NYTData.response.docs[i].snippet +'</h5>');
$('#article-'+i).append("<a href=" + NYTData.response.docs[i].web_url + ">"+ NYTData.response.docs[i].web_url + "</a>");
$('#article-'+i).append('<h5>'+NYTData.response.docs[i].section_name +'</h5>');
$('#article-'+i).append('<h5>'+ NYTData.response.docs[i].pub_date+'</h5>');
}
console.log(urlBase);
console.log(NYTData);
}).fail(function(err){
throw err;
});
}
$('#searchButton').on('click', function(){
searchTerm = $('#searchParam').val().trim();
numResult = $('#recordNum').val();
startYear = $('#startYear').val();
endYear = $('#endYear').val();
numResult = $('#recordNum').val();
var newUrl = urlBase += '?' + $.param({
'api-key': authKey,
'q': searchTerm,
});
if (parseInt(startYear)){
var newUrl = urlBase += '?' + $.param({
'api-key': authKey,
'q': searchTerm,
'begin_date': startYear +"0101",
});
}
if (parseInt(endYear)){
var newUrl = urlBase += '?' + $.param({
'api-key': authKey,
'q': searchTerm,
'begin_date': startYear +"0101",
'end_date': endYear+"1231"
});
}
runQuery(numResult, newUrl);
})
$('#clearResults').on('click', function(){
$('#wellSection').empty();
})
//functions
//main process
// retrieve user inputs and conver to variables
// use variables to run an ajax call
// break down the NYT object into usable fields
// generate content for html
//dealing with EDGE cases
// var url = "https://api.nytimes.com/svc/search/v2/articlesearch.json";
// url += '?' + $.param({
// 'api-key': "ceadb42d410e487d884bd07d6764a53d",
// 'q': "barack obama",
// 'begin_date': "20150101"
// });
// $.ajax({
// url: url,
// method: 'GET',
// }).done(function(result) {
// console.log(result);
// }).fail(function(err) {
// throw err;
// });