Ver código fonte

added NYT files

Ahmed Haque 8 anos atrás
pai
commit
1a8b3ca3cf
5 arquivos alterados com 247 adições e 0 exclusões
  1. 127
    0
      NYTCode.js
  2. 118
    0
      NYTExample.html
  3. BIN
      NYTSearchImage.png
  4. 1
    0
      composer.json
  5. 1
    0
      index.php

+ 127
- 0
NYTCode.js Ver arquivo

@@ -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
+})

+ 118
- 0
NYTExample.html Ver arquivo

@@ -0,0 +1,118 @@
1
+<!DOCTYPE html>
2
+<html lang="en">
3
+<head>
4
+	<meta charset="UTF-8">
5
+	<title>New York Times Search</title>
6
+
7
+	<!-- Bootstrap CSS -->
8
+	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
9
+
10
+	<!-- Bootswatch Styling for Improving the Aesthetics -->
11
+	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/flatly/bootstrap.min.css">
12
+
13
+	<!-- Font Awesome CSS Icons (For cool glyphicons) -->
14
+	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
15
+</head>
16
+<body>
17
+
18
+<!-- Main Bootstrap Search -->
19
+<div class="container">
20
+
21
+	<!-- Jumbotron for Title -->
22
+	<div class="jumbotron" style="background-color: #20315A ; color: white;">
23
+		<h1 class="text-center"><strong><i class="fa fa-newspaper-o"></i> New York Times Search</strong></h1>
24
+	</div>
25
+
26
+	<!-- Row for Searching New York Times -->
27
+	<div class="row">
28
+		<div class="col-lg-12">
29
+		<br>
30
+			<!-- First panel is for handling the search parameters -->
31
+			<div class="panel panel-primary">
32
+				<div class="panel-heading">
33
+					<h3 class="panel-title"><strong><i class="fa  fa-list-alt"></i>   Search Parameters</strong></h3>
34
+				</div>
35
+				<div class="panel-body">
36
+
37
+					<!-- Here we create an HTML Form for handling the inputs-->
38
+					<form role="form">
39
+
40
+				  	  <!-- Here we create the text box for capturing the search term-->
41
+					  <div class="form-group">
42
+					    <label for="search">Search Term:</label>
43
+					    <input type="text" class="form-control" id="searchTerm">
44
+					  </div>
45
+
46
+					  <!-- Here we capture the number of records that the user wants to retrieve  -->
47
+					  <div class="form-group">
48
+					    <label for="pwd">Number of Records to Retrieve:</label>
49
+						<select class="form-control" id="numRecordsSelect">
50
+							<option value=1>1</option>
51
+							<option value=5>5</option>
52
+							<option value=10>10</option>
53
+						</select>			  
54
+					  </div>
55
+
56
+				  	  <!-- Here we capture the Start Year Parameter-->
57
+					  <div class="form-group">
58
+					    <label for="startYear">Start Year (Optional):</label>
59
+					    <input type="text" class="form-control" id="startYear">
60
+					  </div>
61
+
62
+				  	  <!-- Here we capture the End Year Parameter -->
63
+					  <div class="form-group">
64
+					    <label for="endYear">End Year (Optional):</label>
65
+					    <input type="text" class="form-control" id="endYear">
66
+					  </div>
67
+
68
+					  <!-- Here we have our final submit button -->
69
+					  <button type="button" class="btn btn-default" id="runSearch"><i class="fa fa-search"></i> Search</button>
70
+  					  <button type="button" class="btn btn-default" id="clearAll"><i class="fa fa-trash"></i> Clear Results</button>
71
+
72
+					</form>
73
+				</div>
74
+			</div>
75
+		</div>
76
+	</div>
77
+
78
+	<!-- This row will handle all of the retrieved articles -->
79
+	<div class="row">
80
+		<div class="col-lg-12">
81
+		<br>
82
+
83
+			<!-- This panel will initially be made up of a panel and wells for each of the articles retrieved -->
84
+			<div class="panel panel-primary">
85
+
86
+				<!-- Panel Heading for the retrieved articles box -->
87
+				<div class="panel-heading">
88
+					<h3 class="panel-title"><strong><i class="fa fa-table"></i>   Top Articles</strong></h3>
89
+				</div>
90
+
91
+				<!-- This main panel will hold each of the resulting articles -->
92
+				<div class="panel-body" id="wellSection">
93
+				</div>
94
+			</div>
95
+		</div>
96
+	</div>
97
+
98
+	<!-- Footer Region -->
99
+	<div class="row">
100
+		<div class="col-lg-12">
101
+			
102
+			<!-- Line Break followed by closing -->
103
+			<hr>
104
+			<h5 class="text-center"><small>Made by Ahmed with lots and lots of <i class="fa fa-heart"></i></small></h5>
105
+
106
+		</div>
107
+	</div>
108
+
109
+</div>
110
+	
111
+<!-- jQuery JS -->
112
+<script src="https://code.jquery.com/jquery.js"></script>
113
+
114
+<!-- Code to the Javascript File -->
115
+<script src="NYTCode.js"></script>
116
+
117
+</body>
118
+</html>

BIN
NYTSearchImage.png Ver arquivo


+ 1
- 0
composer.json Ver arquivo

@@ -0,0 +1 @@
1
+{}

+ 1
- 0
index.php Ver arquivo

@@ -0,0 +1 @@
1
+<?php include_once("NYTExample.html"); ?>