Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
janfejtek committed Oct 28, 2017
1 parent 4f330af commit 842d2b0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"express": "^4.16.2",
"global": "^4.3.2",
"google-images": "^2.1.0",
"mongodb": "^2.2.33"
},
"engines": {
Expand Down
30 changes: 27 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
var express = require('express');

var GoogleImages = require('google-images');
const client = new GoogleImages(process.env.SEARCH_ID, process.env.SEARCH_API_KEY);
const MAX_LATEST = 10;

var app = express();

var latest = [];

function addLatest(term) {
var data = {term: term, timestamp: new Date().toISOString()};
latest.unshift(data);
while (latest.length > MAX_LATEST) {
latest.pop();
}
}

app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});

app.get("/search*", function (request, response) {

app.get("/search", function (request, response) {
var term = request.query.term;
var offset = request.query.offset;
if (offset == undefined)
offset = 0;
addLatest(term);
client.search(term, {start: offset, linkSite: "true"})
.then(images => {
var data = [];
console.log(images);
for (var idx in images) {
data.push({url: images[idx].url, description: images[idx].description, originalurl: images[idx].parentPage});
}
response.send(data);
});

});

app.get("/search/latest", function(request, response) {
response.send(latest);
})

// listen for requests :)
Expand Down
12 changes: 12 additions & 0 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ <h2>
</h2>
<ul>
<li>
<a href="https://pepper-shallot.glitch.me/search/latest">https://pepper-shallot.glitch.me/search/latest</a>
</li>
<li>
<a href="https://pepper-shallot.glitch.me/search?term=monkey&offset=10">https://pepper-shallot.glitch.me/search?term=monkey&offset=10</a>
</li>
</ul>
<h2>
Notes:
</h2>
<ul>
<li>
Endpoint /search/latest shows last 10 search queries, format is: [{"term":<i>searchterm</i>,"timestamp":<i>ISO_format_date_time_string</i>}, ...]
</li>
</ul>

Expand Down

0 comments on commit 842d2b0

Please sign in to comment.