-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
317 lines (231 loc) · 10.4 KB
/
app.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// define our application and instantiate Express
var express = require('express');
var app = express();
// enables you to use jQuery to examine and transform HTML on Node.js
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM(`<!DOCTYPE html>`);
const $ = require('jquery')(window);
// pass our app into http
var http = require('http').Server(app);
// install Handlebars {{title}}
var hbs = require('hbs');
// define a route to handle form post
var path = require('path');
// generates MD5
var md5 = require('md5');
// empty directory everytime
var rimraf = require('rimraf');
// store responses using FileSystem
var fs = require('fs');
var formidable = require('formidable');
var rimraf = require('rimraf');
var FileAPI = require('file-api');
var File = FileAPI.file;
var FileList = FileAPI.FileList;
var FileReader = FileAPI.FileReader;
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.static(path.join(__dirname, '/img')));
app.use(express.static(path.join(__dirname, '/views')));
app.set('view engine', 'html');
app.engine('html', hbs.__express);
// Loads the welcome page and waits for year to be entered
app.get('/', function(req, res) {
// clear img directory - DOES NOT WORK
var destpath = __dirname + '/public/img/photo*';
rimraf(destpath, function() { console.log('done'); });
res.render(__dirname + '/views/index.html'), {
};
})
// Calls the API and loads all the comics
app.get('/year-entered', function(req, res) {
var year = req.query.year;
// req.query.year has the year
if (year == "") {
res.render(__dirname + '/views/index.html', {
});
return;
}
else {
// Create apiURL
var timestamp = Date.now();
var publicKey = "a090825504ad5c202bbe5a728a8d051d";
var privateKey = "2aa7212be76cf50d10a1e3ba1665f6b34f334363";
var md5base = timestamp + privateKey + publicKey;
var md5hash = md5(md5base);
// if year is within Marvel's range
if ((year >= 1947) && (year <= (new Date()).getFullYear())) {
var offset = 0;
var total = 0;
var apiURL = "http://gateway.marvel.com/v1/public/comics?dateRange="
+ year + "-01-01%2C" + year + "-12-31&ts=" + timestamp
+ "&limit=" + 50 + "&offset=" + offset + "&apikey=" + publicKey + "&hash=" + md5hash;
// contains pageNumber and URL of each page
var allPageInfo = { page: [ ], year };
var pageNumber = 1;
allPageInfo.year = year;
allPageInfo.page.push({
pageNumber: pageNumber,
url: apiURL,
curPage: true
});
// get total number of results
$.getJSON(apiURL).done(function(result) {
var total = result.data.total;
// put all apiURLs in apiurlarray
while (total > (offset+50)) {
offset += 50;
pageNumber += 1;
var apiURL = "http://gateway.marvel.com/v1/public/comics?dateRange="
+ year + "-01-01%2C" + year + "-12-31&ts=" + timestamp
+ "&limit=" + 50 + "&offset=" + offset + "&apikey=" + publicKey + "&hash=" + md5hash;
allPageInfo.page.push({
pageNumber: pageNumber,
url: apiURL,
curPage: false
});
}
// store in hits.json
var filepath = __dirname + '/hits' + '.json';
fs.writeFileSync(filepath, JSON.stringify(result.data, undefined, 2));
// number of comics in this search
var hits = result.data.count;
// create structure that contains everything
var allComicInfo = { comics: [ ] };
// store each comic's cover URLs in an array
for (comic = 0; comic < hits; comic++) {
// Note: for some reason 2019 has some undefined comics in the API
// this is the workaround for that
if (result.data.results[comic] != undefined) {
// store only the data we need in separate variables
var imageURL = result.data.results[comic].thumbnail.path + "." +
result.data.results[comic].thumbnail.extension;
var title = result.data.results[comic].title;
var description = result.data.results[comic].description;
var url = result.data.results[comic].urls[0].url;
var month = result.data.results[comic].dates[0].date.slice(5,7);
var date = result.data.results[comic].dates[0].date.slice(8,10);
// if image not available
if (result.data.results[comic].thumbnail.path ==
"http://i.annihil.us/u/prod/marvel/i/mg/b/40/image_not_available") {
var imageURL = "http://static1.squarespace.com/static/51b3dc8ee4b051b96ceb10de/51ce6099e4b0d911b4489b79/5283aa6fe4b043b800f05a32/1384381259739/marvel-studios-releases-new-credits-logo-preview.jpg?format=1500w"
}
// if description not available
if (description == null) {
description = "No description available."
}
// add to structure
allComicInfo.comics.push({
cover: imageURL,
title: title,
description: description,
url: url,
month: month,
date: date,
})
}
}
res.render(__dirname + '/views/index.html', {
allComicInfo: allComicInfo,
allPageInfo: allPageInfo
});
});
}
else {
res.render(__dirname + '/views/index.html', {
});
}
}
})
// request a different page with the same year
app.get('/page-changed', function(req, res) {
// get year and page from URL
var year = req.query.year;
var page = req.query.page;
// calculate offset based on page
var offset = 50*(page-1);
// Create apiURL
var timestamp = Date.now();
var publicKey = "a090825504ad5c202bbe5a728a8d051d";
var privateKey = "2aa7212be76cf50d10a1e3ba1665f6b34f334363";
var md5base = timestamp + privateKey + publicKey;
var md5hash = md5(md5base);
var apiURL = "http://gateway.marvel.com/v1/public/comics?dateRange="
+ year + "-01-01%2C" + year + "-12-31&ts=" + timestamp
+ "&limit=" + 50 + "&offset=" + offset + "&apikey=" + publicKey + "&hash=" + md5hash;
// contains pageNumber and URL of each page
var allPageInfo = { page: [ ], year };
// get JSON object from apiURL
$.getJSON(apiURL, function(result) {
var total = result.data.total;
var offset = 0;
var pageNumber = 1;
allPageInfo.year = year;
while (total > offset) {
var apiURL = "http://gateway.marvel.com/v1/public/comics?dateRange="
+ year + "-01-01%2C" + year + "-12-31&ts=" + timestamp
+ "&limit=" + 50 + "&offset=" + offset + "&apikey=" + publicKey + "&hash=" + md5hash;
if (pageNumber == page) {
allPageInfo.page.push({
pageNumber: pageNumber,
url: apiURL,
curPage: true
});
}
else {
allPageInfo.page.push({
pageNumber: pageNumber,
url: apiURL,
curPage: false
});
}
offset += 50;
pageNumber += 1;
}
// number of comics in this search
var hits = result.data.count;
// create structure that contains everything
var allComicInfo = { comics: [ ] };
// store each comic's cover URLs in an array
for (comic = 0; comic < hits; comic++) {
// see 2019 note above
if (result.data.results[comic] != undefined) {
// store only the data we need in separate variables
var imageURL = result.data.results[comic].thumbnail.path + "." +
result.data.results[comic].thumbnail.extension;
var title = result.data.results[comic].title;
var description = result.data.results[comic].description;
var url = result.data.results[comic].urls[0].url;
var month = result.data.results[comic].dates[0].date.slice(5,7);
var date = result.data.results[comic].dates[0].date.slice(8,10);
// if image not available
if (result.data.results[comic].thumbnail.path ==
"http://i.annihil.us/u/prod/marvel/i/mg/b/40/image_not_available") {
var imageURL = "http://static1.squarespace.com/static/51b3dc8ee4b051b96ceb10de/51ce6099e4b0d911b4489b79/5283aa6fe4b043b800f05a32/1384381259739/marvel-studios-releases-new-credits-logo-preview.jpg?format=1500w"
}
// if description not available
if (description == null) {
description = "No description available."
}
// add to structure
allComicInfo.comics.push({
cover: imageURL,
title: title,
description: description,
url: url,
month: month,
date: date,
})
}
}
res.render(__dirname + '/views/index.html', {
allComicInfo: allComicInfo,
allPageInfo: allPageInfo
});
})
});
module.exports = app;
// host the website
http.listen(process.env.PORT || 3000, function() {
console.log("Running on", http.address().port);
});