-
Notifications
You must be signed in to change notification settings - Fork 23
/
NYTimes_hardcover_fiction_today.html
155 lines (122 loc) · 5.32 KB
/
NYTimes_hardcover_fiction_today.html
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NYTimes API</title>
<meta name="description" content="NYTimes API example">
<meta name="author" content="Ulises Gascón">
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="http://bootswatch.com/paper/bootstrap.min.css">
<style type="text/css">
html {
position: relative;
min-height: 100%;
}
body {
/* Margin bottom by footer height */
margin-bottom: 60px;
}
.amazon {
width: 125px;
}
#error-ajax {
display: none;
}
#cargando {
float: none;
margin: auto;
}
.caratula {
width: 230px;
}
.footer {
position: absolute;
bottom: 0;
width: 100%;
height: 40px;
background-color: #F1F1F1;
}
</style>
</head>
<body>
<div class="container">
<div class="alert alert-dismissible alert-danger" id="error-ajax">
<button type="button" class="close" data-dismiss="alert">x</button>
<strong>Oh Error!</strong> Error al cargar los datos del <a href="http://developer.nytimes.com/" target="_blank" class="alert-link">NYTimes API</a>
</div>
<div class="page-header">
<h1>Top Hardcover Fiction</h1>
</div>
<div id="cargando">
<img src="https://d13yacurqjgara.cloudfront.net/users/493970/screenshots/1691794/progress.gif">
</div>
<div class="row" id="row-contenido">
<div class="col-md-3" id="col1"></div>
<div class="col-md-3" id="col2"></div>
<div class="col-md-3" id="col3"></div>
<div class="col-md-3" id="col4"></div>
</div>
</div>
<footer class="footer">
<div class="container">
<p class="text-muted">
<a href="http://developer.nytimes.com/" target="_blank"><img src="http://graphics8.nytimes.com/packages/images/developer/logos/poweredby_nytimes_200b.png"></a>
</p>
</div>
</footer>
<!-- Bootstrap -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script>
var apiKey = "";
var fecha = "2017-02-01";
var divID = "col4";
function pushToHTML(datos) {
var contenido;
if (divID === "col4") {
divID = "col1";
} else if (divID === "col1") {
divID = "col2";
} else if (divID === "col2") {
divID = "col3";
} else if (divID === "col3") {
divID = "col4";
}
console.info(divID);
contenido = '<div class="panel panel-default"><div class="panel-body">';
contenido += '<p><strong> #' + datos.rank + ' ' + datos.title + '</strong><p>';
contenido += '<img class="caratula" src="' + datos.book_image + '">';
contenido += '<p><i>Weeks on list: ' + datos.weeks_on_list + '</i></p>';
contenido += '<p>' + datos.description + '</p>';
contenido += '<a href="' + datos.amazon_product_url + '" target="_blank" class="btn btn-warning">Buy at Amazon <span class="glyphicon glyphicon-play-circle" aria-hidden="true"></span></a>';
contenido += '</div></div>';
document.getElementById(divID).innerHTML += contenido;
}
function peticionAjax(url) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status >= 100 && xmlHttp.status <= 300) {
document.getElementById("cargando").style.display = 'none';
var datosCrudos = JSON.parse(xmlHttp.responseText);
var datos = datosCrudos.results.books;
console.info(datos);
for (var i = 0; i < datos.length; i++) {
pushToHTML(datos[i]);
}
} else if (xmlHttp.status >= 400 && xmlHttp.status <= 600) {
document.getElementById("cargando").style.display = 'none';
document.getElementById("error-ajax").style.display = 'block';
document.getElementById("row-contenido").innerHTML = '<img src="http://www.404notfound.fr/assets/images/pages/img/androiddev101.jpg">';
console.error("ERROR! 404", JSON.parse(xmlHttp.responseText));
}
}
};
xmlHttp.open("GET", url, true);
xmlHttp.send();
}
peticionAjax("http://api.nytimes.com/svc/books/v3/lists/" + fecha + "/hardcover-fiction.json?api-key=" + apiKey);
</script>
</body>
</html>