-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (54 loc) · 2.36 KB
/
script.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
function loadData() {
var $body = $('body');
var $wikiElem = $('#wikipedia-links');
var $nytHeaderElem = $('#nytimes-header');
var $nytElem = $('#nytimes-articles');
var $greeting = $('#greeting');
// clear out old data before new request
$wikiElem.text("");
$nytElem.text("");
var streetStr = $('#street').val();
var cityStr = $('#city').val();
var address = streetStr + ', ' + cityStr;
$greeting.text('So, you want to live at ' + address + '?');
// load streetview
// var streetviewUrl = 'http://maps.googleapis.com/maps/api/streetview?size=600x400&location=' + address + '';
// $body.append('<img class="bgimg" src="' + streetviewUrl + '">');
// load nytimes
var nytimesUrl = 'http://api.nytimes.com/svc/search/v2/articlesearch.json?q=' + cityStr + '&sort=newest&api-key=YDVhQxsxYDiAIRMqwkGJpmEal7WRL55x';
$.getJSON(nytimesUrl, function(data){
$nytHeaderElem.text('New York Times Articles About ' + cityStr);
articles = data.response.docs;
for (var i = 0; i < articles.length; i++) {
var article = articles[i];
$nytElem.append('<li class="article">'+
'<a href="'+article.web_url+'">'+article.headline.main+'</a>'+
'<p>' + article.snippet + '</p>'+
'</li>');
};
}).error(function(e){
$nytHeaderElem.text('New York Times Articles Could Not Be Loaded');
});
// load wikipedia data
var wikiUrl = 'http://en.wikipedia.org/w/api.php?action=opensearch&search=' + cityStr + '&format=json&callback=wikiCallback';
var wikiRequestTimeout = setTimeout(function(){
$wikiElem.text("failed to get wikipedia resources");
}, 8000);
$.ajax({
url: wikiUrl,
dataType: "jsonp",
jsonp: "callback",
success: function( response ) {
var articleList = response[1];
for (var i = 0; i < articleList.length; i++) {
articleStr = articleList[i];
var url = 'http://en.wikipedia.org/wiki/' + articleStr;
$wikiElem.append('<li><a href="' + url + '">' + articleStr + '</a></li>');
};
clearTimeout(wikiRequestTimeout);
}
});
return false;
};
$('#form-container').submit(loadData);
// loadData();