2017-06-27 18:44:24 +00:00
|
|
|
/**
|
|
|
|
* Created by Kylart on 27/06/2017.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const qs = require('querystring')
|
|
|
|
const axios = require('axios')
|
2017-06-28 10:42:02 +00:00
|
|
|
const malScraper = require('mal-scraper')
|
2017-06-27 18:44:24 +00:00
|
|
|
|
|
|
|
const SEARCH_URI = 'https://myanimelist.net/search/prefix.json'
|
|
|
|
|
|
|
|
const searchTerm = (url, res) => {
|
|
|
|
const query = qs.parse(url.query.replace('?', ''))
|
|
|
|
|
|
|
|
axios.get(SEARCH_URI, {
|
|
|
|
params: {
|
|
|
|
type: 'anime',
|
|
|
|
keyword: query.term
|
|
|
|
}
|
|
|
|
}).then(({data}) => {
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'})
|
|
|
|
res.write(JSON.stringify(data))
|
|
|
|
res.end()
|
2017-06-27 18:49:32 +00:00
|
|
|
}).catch(/* istanbul ignore next */(e) => {
|
2017-06-27 18:44:24 +00:00
|
|
|
console.log('[Search] (Term):' + e.message)
|
|
|
|
res.writeHead(204, {})
|
|
|
|
res.end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:42:02 +00:00
|
|
|
const searchOnMal = (url, res) => {
|
|
|
|
const query = qs.parse(url.query.replace('?', ''))
|
|
|
|
|
|
|
|
malScraper.getInfoFromName(query.term).then((data) => {
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'})
|
|
|
|
res.write(JSON.stringify(data))
|
|
|
|
res.end()
|
2017-06-30 14:45:11 +00:00
|
|
|
}).catch(/* istanbul ignore next */(err) => {
|
2017-06-28 10:42:02 +00:00
|
|
|
console.log(err.message)
|
|
|
|
res.writeHead(204, {})
|
|
|
|
res.end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-27 18:44:24 +00:00
|
|
|
module.exports = {
|
2017-06-28 10:42:02 +00:00
|
|
|
searchTerm,
|
|
|
|
searchOnMal
|
2017-06-27 18:44:24 +00:00
|
|
|
}
|