This repository has been archived by the owner on Sep 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 623d582
Showing
7 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: node index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var mongo = require('mongodb').MongoClient; | ||
|
||
module.exports = function Database(callback) { | ||
var self = this; | ||
|
||
mongo.connect(process.env.MONGO_URI, function(err, db) { | ||
if (err) return console.log(err); | ||
self.collection = db.collection(process.env.MONGO_COLLECTION); | ||
callback(); | ||
}); | ||
|
||
this.latestQuery = function(cb) { | ||
self.collection.find().sort({ $natural : -1 }).limit(10).toArray(function(err, data) { | ||
if (err) return cb(err); | ||
cb(null, data); | ||
}); | ||
} | ||
|
||
this.insertQuery = function(query, cb) { | ||
self.collection.insert({query : query, date: new Date()}, function(err) { | ||
if (err) return cb(err); | ||
cb(); | ||
}); | ||
}; | ||
return this; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var env = require('node-env-file'); | ||
try { | ||
env(__dirname + '/.env'); | ||
} catch (e) { } | ||
|
||
var imgur = require('./lib/imgur'); | ||
var express = require('express'); | ||
var async = require('async'); | ||
var app = express(); | ||
var db = null; | ||
|
||
app.set('view engine', 'jade'); | ||
|
||
app.get('/', function(req,res) { | ||
res.render('index', {host : req.protocol + '://' + req.get('host')}); | ||
}); | ||
|
||
app.get('/api/search/:search', function(req, res) { | ||
var query = req.params.search; | ||
var page = Number(req.query.offset) || 0; | ||
|
||
async.parallel([ | ||
function(callback) { | ||
imgur.query(query, page, function(err, data) { | ||
if (err) return callback(err); | ||
callback(data); | ||
}); | ||
}, | ||
function(callback) { | ||
db.insertQuery(query, callback); | ||
} | ||
],function() { | ||
if (Array.isArray(arguments[0]) !== true) { | ||
res.send({}).end(); | ||
} else { | ||
res.send(arguments[0]).end(); | ||
} | ||
}); | ||
}); | ||
|
||
app.get('/api/latest', function(req, res) { | ||
db.latestQuery(function(err, data) { | ||
if (err) return res.send({}).end(); | ||
res.send(data).end(); | ||
}); | ||
}); | ||
|
||
|
||
db = new require('./database')(function() { | ||
app.listen(process.env.PORT, function() { | ||
console.log('Application started on :' + process.env.PORT); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
var imgurg = exports; | ||
var request = require('request'); | ||
|
||
imgurg.cid = process.env.IMGUR_CLIENT_ID; | ||
|
||
imgurg.query = function(query, page, callback){ | ||
var query = { c: String(query) } | ||
var subcat = (query.c).match(/(?:^r\/)(.*)/); | ||
query.e = subcat && encodeURIComponent((query.c).substring(2,(query.c).length)) || encodeURIComponent(query.c); | ||
|
||
var options = { | ||
encoding: 'utf8', | ||
json: true, | ||
uri : 'https://api.imgur.com/3/gallery/search/top/'+page+'?q='+query.e, | ||
headers: { | ||
'Authorization': 'Client-ID '+ imgurg.cid | ||
}, | ||
method: 'GET' | ||
} | ||
|
||
function tError(stat, err, body){ | ||
callback('error'); | ||
return false | ||
} | ||
|
||
function isObjectEmpty(obj) { | ||
for (var key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
request(options, function (err, res, body) { | ||
if ( (err != "" && body !== undefined) || res.statusCode !== 200 ){ | ||
if(body.data === undefined || isObjectEmpty(body.data) ){ return tError(res.statusCode, err, body); } | ||
callback(null, body.data.map(function(d) { | ||
return { | ||
title : d.title, | ||
link : 'http://imgur.com/' + d.id, | ||
image : d.link, | ||
time : d.datetime | ||
}; | ||
})); | ||
}else{ return tError(res.statusCode, err, body); } | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "imagesearch-freecodecamp", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/b37t1td/imagesearch-freecodecamp.git" | ||
}, | ||
"author": "Svetlana Linuxenko <[email protected]> (http://www.linuxenko.pro)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/b37t1td/imagesearch-freecodecamp/issues" | ||
}, | ||
"homepage": "https://github.com/b37t1td/imagesearch-freecodecamp#readme", | ||
"dependencies": { | ||
"async": "^2.0.0-rc.2", | ||
"express": "^4.13.4", | ||
"jade": "^1.11.0", | ||
"mongodb": "^2.1.13", | ||
"node-env-file": "^0.1.8", | ||
"request": "^2.69.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
html | ||
head | ||
title Image search microservice | ||
body | ||
h2 Image Search Abstraction Layer (freeCodeCamp) | ||
hr | ||
h3 Search for images | ||
p #{host}/api/search/:query[?offset=0] | ||
strong Example of request second page of "cats" query | ||
p #{host}/api/search/cats?offset=1 | ||
h3 Latest queries stats (last 10 queries) | ||
p #{host}/api/latest |