KawAnime/server/mal/official.js

57 lines
1.3 KiB
JavaScript
Raw Normal View History

const vault = require('../vault')
2017-11-05 03:59:36 +00:00
const malScraper = require('mal-scraper')
const Api = malScraper.officialApi
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) => {
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)
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) => {
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) => {
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
}