actually use push target filters when new books are published
parent
460cf44c4c
commit
a734069b74
|
@ -7,6 +7,7 @@
|
|||
|
||||
const HttpError = require('../errors/HttpError')
|
||||
const request = require('request')
|
||||
const uriRegex = /^(.+:\/\/)?(.+\.)*(.+\.).{1,}(:\d+)?(.+)?/i
|
||||
|
||||
module.exports = {
|
||||
publish: async function (req, res) {
|
||||
|
@ -73,16 +74,27 @@ module.exports = {
|
|||
async function sendUpdatesAsync (id) {
|
||||
const book = await Book.findOne({ id })
|
||||
const targets = await TargetUrl.find()
|
||||
if (!book) return
|
||||
for (const i in targets) {
|
||||
sails.log('sending ' + book.id + ' info to ' + targets[i].url)
|
||||
const item = targets[i]
|
||||
const { author: fAuthor, publisher: fPublisher, title: fTitle, isbn: fIsbn, url } = item
|
||||
const { author: bAuthor, publisher: bPublisher, title: bTitle, isbn: bIsbn } = book
|
||||
sails.log('sending ' + book.id + ' info to ' + url)
|
||||
|
||||
if (uriRegex.test(url)) {
|
||||
if (fAuthor && !((bAuthor || '').includes(fAuthor))) continue
|
||||
if (fPublisher && !((bPublisher || '').includes(fPublisher))) continue
|
||||
if (fTitle && !((bTitle || '').includes(fTitle))) continue
|
||||
if (fIsbn && !((bIsbn || '').includes(fIsbn))) continue
|
||||
request.post({
|
||||
url: targets[i].url,
|
||||
url: url,
|
||||
headers: { 'User-Agent': 'RoE-aggregator' },
|
||||
form: book
|
||||
}, function (err, httpResp, body) {
|
||||
if (err) {
|
||||
sails.log(`error: failed to send book ${id} to ${targets[i].url}`)
|
||||
sails.log(`error: failed to send book ${id} to ${url}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ module.exports = {
|
|||
},
|
||||
title: { type: 'string', required: true },
|
||||
author: { type: 'string' },
|
||||
publisher: { type: 'string' },
|
||||
isbn: { type: 'string' },
|
||||
version: { type: 'string' }
|
||||
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
exports.up = function (knex, Promise) {
|
||||
return Promise.all([
|
||||
knex.schema.table('book', t => {
|
||||
t.string('publisher')
|
||||
})
|
||||
])
|
||||
}
|
||||
|
||||
exports.down = function (knex, Promise) {
|
||||
return Promise.all([
|
||||
knex.schema.table('book', t => {
|
||||
t.dropColumns('publisher')
|
||||
})
|
||||
])
|
||||
}
|
|
@ -21,7 +21,8 @@
|
|||
"debug": "node --inspect app.js",
|
||||
"custom-tests": "echo 'Nothing yet'",
|
||||
"db:migrate": "knex migrate:latest",
|
||||
"db:rollback": "knex migrate:rollback"
|
||||
"db:rollback": "knex migrate:rollback",
|
||||
"db:gmig": "knex migrate:make"
|
||||
},
|
||||
"dependencies": {
|
||||
"@sailshq/connect-redis": "^3.2.1",
|
||||
|
|
Loading…
Reference in New Issue