/
app.js
105 lines (93 loc) · 2.69 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
"use strict";
const express = require('express');
const app = express();
const url = require('url');
const path = require('path');
const mongoClient = require('mongodb').MongoClient;
const mongodbURL = process.env.MONGOLAB_URI; // This environmental variable needs to be set
const request = require('request');
function searchImgurGallery(apiUrl, callback) {
let options = {
url: apiUrl,
headers: {
Authorization: 'Client-ID 8692246cebf3be3'
},
json: true,
};
function getPics(err, response, body) {
if (!err && response.statusCode === 200) {
body = body.data.filter(image => {
if (!image.is_album) {
return image;
}
}).map(image => {
return {
image_url: image.link,
alt_text: image.title,
page_url: `https://imgur.com/${image.id}`
};
});
console.log(body);
callback(body);
}
else {
callback("Error");
}
}
request(options, getPics);
}
function storeInDatabase(searchTerm) {
var newSearchTerm = {
searchTerm: searchTerm
};
mongoClient.connect(mongodbURL, function(err, db) {
if (err) throw err;
var collection = db.collection('storedSearchTerms');
collection.insert(newSearchTerm, function(err, data) {
if (err) throw err;
console.log(JSON.stringify(newSearchTerm));
db.close();
});
});
}
function getLastTenSearchTerms(callback) {
mongoClient.connect(mongodbURL, function(err, db) {
if (err) throw err;
var collection = db.collection('storedSearchTerms');
collection.find({}, { '_id': 0, 'searchTerm' : 1}).sort({
$natural: -1
}).limit(10).toArray(function(err, documents) {
if (err) throw err;
console.log(JSON.stringify(documents));
callback(JSON.stringify(documents));
db.close();
});
});
}
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
app.get('/search/*', function(req, res) {
let userQuery = url.parse(req.url, true).pathname.slice(8);
let offsetVal = url.parse(req.url, true).query;
let apiUrl;
if (offsetVal) {
apiUrl = 'https://api.imgur.com/3/gallery/search/' + offsetVal.offset + '/?q=' + userQuery;
}
else {
apiUrl = 'https://api.imgur.com/3/gallery/search/?q=' + userQuery;
}
storeInDatabase(userQuery);
searchImgurGallery(apiUrl, function(bodyData) {
res.send(bodyData);
});
});
app.get('/history*', function(req, res) {
getLastTenSearchTerms(function(history) {
res.send(history);
});
});
app.listen(process.env.PORT, process.env.IP, function() {
console.log("URL shortener listening on port " + process.env.PORT);
});