|
@@ -0,0 +1,127 @@
|
|
1
|
+// SETUP VARIABLES
|
|
2
|
+// ==========================================================
|
|
3
|
+
|
|
4
|
+// This variable will be pre-programmed with our authentication key (the one we received when we registered)
|
|
5
|
+var authKey = "b9f91d369ff59547cd47b931d8cbc56b:0:74623931";
|
|
6
|
+
|
|
7
|
+// These variables will hold the results we get from the user's inputs via HTML
|
|
8
|
+var queryTerm = "";
|
|
9
|
+var numResults = 0;
|
|
10
|
+var startYear = 0;
|
|
11
|
+var endYear = 0;
|
|
12
|
+
|
|
13
|
+// Based on the queryTerm we will create a queryURL
|
|
14
|
+var queryURLBase = "http://api.nytimes.com/svc/search/v2/articlesearch.json?api-key=" + authKey + "&q=";
|
|
15
|
+
|
|
16
|
+// Array to hold the various article info
|
|
17
|
+var articleCounter = 0;
|
|
18
|
+
|
|
19
|
+// FUNCTIONS
|
|
20
|
+// ==========================================================
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+// This runQuery function expects two parameters (the number of articles to show and the final URL to download data from)
|
|
24
|
+function runQuery(numArticles, queryURL){
|
|
25
|
+
|
|
26
|
+ // The AJAX function uses the URL and Gets the JSON data associated with it. The data then gets stored in the variable called: "NYTData"
|
|
27
|
+ $.ajax({url: queryURL, method: "GET"})
|
|
28
|
+ .done(function(NYTData) {
|
|
29
|
+
|
|
30
|
+ // Here we are logging the URL so we have access to it for troubleshooting
|
|
31
|
+ console.log("------------------------------------")
|
|
32
|
+ console.log("URL: " + queryURL);
|
|
33
|
+ console.log("------------------------------------")
|
|
34
|
+
|
|
35
|
+ // Here we then log the NYTData to console, where it will show up as an object.
|
|
36
|
+ console.log(NYTData);
|
|
37
|
+ console.log("------------------------------------")
|
|
38
|
+
|
|
39
|
+ // Loop through and provide the correct number of articles
|
|
40
|
+ for (var i=0; i<numArticles; i++) {
|
|
41
|
+
|
|
42
|
+ // Add to the Article Counter (to make sure we show the right number)
|
|
43
|
+ articleCounter++;
|
|
44
|
+
|
|
45
|
+ // Create the HTML Well (Section) and Add the Article content for each
|
|
46
|
+ var wellSection = $("<div>");
|
|
47
|
+ wellSection.addClass('well');
|
|
48
|
+ wellSection.attr('id', 'articleWell-' + articleCounter)
|
|
49
|
+ $('#wellSection').append(wellSection);
|
|
50
|
+
|
|
51
|
+ // Confirm that the specific JSON for the article isn't missing any details
|
|
52
|
+ // If the article has a headline include the headline in the HTML
|
|
53
|
+ if(NYTData.response.docs[i].headline != "null")
|
|
54
|
+ {
|
|
55
|
+ $("#articleWell-"+ articleCounter).append('<h3><span class="label label-primary">' + articleCounter + '</span><strong> ' + NYTData.response.docs[i].headline.main + "</strong></h3>");
|
|
56
|
+
|
|
57
|
+ // Log the first article's Headline to console.
|
|
58
|
+ console.log(NYTData.response.docs[i].headline.main);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ // If the article has a Byline include the headline in the HTML
|
|
62
|
+ if( NYTData.response.docs[i].byline && NYTData.response.docs[i].byline.hasOwnProperty("original"))
|
|
63
|
+ {
|
|
64
|
+ $("#articleWell-"+ articleCounter).append('<h5>' + NYTData.response.docs[i].byline.original + "</h5>");
|
|
65
|
+
|
|
66
|
+ // Log the first article's Author to console.
|
|
67
|
+ console.log(NYTData.response.docs[i].byline.original);
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ // Then display the remaining fields in the HTML (Section Name, Date, URL)
|
|
71
|
+ $("#articleWell-"+ articleCounter).append('<h5>Section: ' + NYTData.response.docs[i].section_name + "</h5>");
|
|
72
|
+ $("#articleWell-"+ articleCounter).append('<h5>' + NYTData.response.docs[i].pub_date + "</h5>");
|
|
73
|
+ $("#articleWell-"+ articleCounter).append("<a href='" + NYTData.response.docs[i].web_url + "'>" + NYTData.response.docs[i].web_url + "</a>");
|
|
74
|
+
|
|
75
|
+ // Log the remaining fields to console as well
|
|
76
|
+ console.log(NYTData.response.docs[i].pub_date);
|
|
77
|
+ console.log(NYTData.response.docs[i].section_name);
|
|
78
|
+ console.log(NYTData.response.docs[i].web_url);
|
|
79
|
+ }
|
|
80
|
+ });
|
|
81
|
+
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+// METHODS
|
|
85
|
+// ==========================================================
|
|
86
|
+
|
|
87
|
+ // On Click button associated with the Search Button
|
|
88
|
+ $('#runSearch').on('click', function(){
|
|
89
|
+
|
|
90
|
+ // Initially sets the articleCounter to 0
|
|
91
|
+ articleCounter = 0;
|
|
92
|
+
|
|
93
|
+ // Empties the region associated with the articles
|
|
94
|
+ $("#wellSection").empty();
|
|
95
|
+
|
|
96
|
+ // Search Term
|
|
97
|
+ var searchTerm = $('#searchTerm').val().trim();
|
|
98
|
+ queryURL = queryURLBase + searchTerm;
|
|
99
|
+
|
|
100
|
+ // Num Results
|
|
101
|
+ numResults = $("#numRecordsSelect").val();
|
|
102
|
+
|
|
103
|
+ // Start Year
|
|
104
|
+ startYear = $('#startYear').val().trim();
|
|
105
|
+
|
|
106
|
+ // End Year
|
|
107
|
+ endYear = $('#endYear').val().trim();
|
|
108
|
+
|
|
109
|
+ // If the user provides a startYear -- the startYear will be included in the queryURL
|
|
110
|
+ if (parseInt(startYear)) {
|
|
111
|
+ queryURL = queryURL + "&begin_date=" + startYear + "0101";
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ // If the user provides a startYear -- the endYear will be included in the queryURL
|
|
115
|
+ if (parseInt(endYear)) {
|
|
116
|
+ queryURL = queryURL + "&end_date=" + endYear + "0101";
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ // Then we will pass the final queryURL and the number of results to include to the runQuery function
|
|
120
|
+ runQuery(numResults, queryURL);
|
|
121
|
+ });
|
|
122
|
+
|
|
123
|
+// This button clears the top articles section
|
|
124
|
+$('#clearAll').on('click', function(){
|
|
125
|
+ articleCounter = 0;
|
|
126
|
+ $("#wellSection").empty();
|
|
127
|
+})
|