Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache book results from the NYT best sellers to the database #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/src/models/nyBook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var mongoose = require('mongoose');

var nyBookSchema = mongoose.Schema( {
title : String,
author : String,
listDate : String,
ISBN : String
});

module.exports = mongoose.model('NYBooks', nyBookSchema);
43 changes: 41 additions & 2 deletions api/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ const payload = require('./payload');
const config = require('./config');
const jwt = require('express-jwt');
const User = require('./models/user');
const axios = require('axios');
const moment = require('moment');
var mongoose = require('mongoose');
const NYBook = require('./models/nyBook');

module.exports = function(app, passport, session) {
// Enforce JWT middleware with whitelisted routes.
const authWhitelist = {path: ['/health', '/register', '/login']};
const authWhitelist = {path: ['/health', '/register', '/login', '/populateNY']};
const isRevokedCallback = (req, payload, done) => {
const issuer = payload.iss;
const userId = payload.cid;
Expand Down Expand Up @@ -98,5 +102,40 @@ module.exports = function(app, passport, session) {
health: 'ok'
}
}));
})
});
app.get('/populateNY', (req, res, next) => {
console.log('running...')
mongoose.connection.db.dropCollection('nybooks', function(err, result) {
if (err) return console.error(err)
});
var d = moment()
var timer = setInterval(function () {
axios.get('https://api.nytimes.com/svc/books/v3/lists//.json', {
params: {
'api-key': '29ff6820315e44e5b7b9060c0aa39d52',
'list': 'combined-print-and-e-book-fiction',
'published-date': d.format('YYYY-MM-DD')
}
})
.then((response) => {
for (let i=0; i<response.data.results.length; i++) {
console.log(response.data.results[i].book_details[0].title)
var details = new NYBook({
title: response.data.results[i].book_details[0].title,
author: response.data.results[i].book_details[0].author,
listDate: response.data.results[i].published_date,
ISBN: response.data.results[i].book_details[0].primary_isbn13
});
details.save(function (err, details) {
if (err) return console.error(err)
});
}
}, (error) => {
console.log(error)
})
d.subtract(7, 'days')
if (d.isBefore('2013-01-01')) clearInterval(timer)
}, 300);
console.log('done!')
});
};