2017-11-12 17:34:13 +00:00
|
|
|
const vault = require('../vault')
|
2017-11-05 03:59:36 +00:00
|
|
|
const malScraper = require('mal-scraper')
|
|
|
|
const Api = malScraper.officialApi
|
|
|
|
|
2017-11-23 15:43:50 +00:00
|
|
|
const {Logger} = require('../utils')
|
|
|
|
const logger = new Logger('Mal-Scraper')
|
|
|
|
|
2017-11-05 03:59:36 +00:00
|
|
|
let api
|
|
|
|
|
|
|
|
const checkCreds = (res) => {
|
|
|
|
api.checkCredentials()
|
2017-11-11 10:44:45 +00:00
|
|
|
.then((data) => {
|
|
|
|
const isOk = data.includes(api._credentials.username)
|
|
|
|
res.status(isOk ? 200 : 206).send()
|
|
|
|
})
|
2017-11-05 03:59:36 +00:00
|
|
|
.catch((err) => {
|
2017-11-23 15:43:50 +00:00
|
|
|
logger.error('An error occurred while checking credentials.', err)
|
2017-11-05 03:59:36 +00:00
|
|
|
res.status(204).send()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const initOfficialApi = (req, res) => {
|
|
|
|
req.on('data', (chunk) => {
|
|
|
|
chunk = JSON.parse(chunk)
|
|
|
|
|
2017-11-12 17:34:13 +00:00
|
|
|
vault.getCreds(chunk.service)
|
|
|
|
.then((creds) => {
|
|
|
|
api = new Api(creds)
|
2017-11-05 03:59:36 +00:00
|
|
|
checkCreds(res)
|
|
|
|
})
|
|
|
|
.catch(() => res.status(204).send())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const actOnList = (req, res) => {
|
|
|
|
req.on('data', (chunk) => {
|
|
|
|
chunk = JSON.parse(chunk)
|
|
|
|
|
|
|
|
const {type, opts, id} = chunk
|
|
|
|
|
|
|
|
api.actOnList(type, id, opts)
|
|
|
|
.then((data) => {
|
2017-11-23 15:43:50 +00:00
|
|
|
logger.info('(Act on List):' + data)
|
2017-11-10 15:24:31 +00:00
|
|
|
res.status(typeof data === 'string' ? 200 : 204).send()
|
2017-11-05 03:59:36 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2017-11-23 15:43:50 +00:00
|
|
|
logger.error('(Act on List): An error occurred.', err)
|
2017-11-05 03:59:36 +00:00
|
|
|
res.status(204).send()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
initOfficialApi,
|
|
|
|
actOnList
|
|
|
|
}
|