2017-05-27 18:41:10 +00:00
|
|
|
/**
|
|
|
|
* Created by Kylart on 27/05/2017.
|
|
|
|
*/
|
|
|
|
|
2017-05-30 11:24:29 +00:00
|
|
|
const nyaa = require('nyaapi')
|
|
|
|
const malScraper = require('mal-scraper')
|
2017-09-25 11:55:58 +00:00
|
|
|
const _ = require('lodash')
|
2017-08-04 09:16:05 +00:00
|
|
|
const {removeUnwanted} = require('./utils')
|
2017-05-27 18:41:10 +00:00
|
|
|
|
|
|
|
const sendRes = (object, res) => {
|
2017-07-24 08:45:00 +00:00
|
|
|
res.status(200).send(JSON.stringify(object))
|
2017-05-27 18:41:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 11:55:58 +00:00
|
|
|
const formatMagnets = (data, searchData, choice, res) => {
|
|
|
|
const magnets = []
|
|
|
|
const eps = []
|
|
|
|
|
|
|
|
data.forEach((elem) => {
|
|
|
|
elem.title[0] = removeUnwanted(elem.title[0])
|
|
|
|
const ep = elem.title[0].split(' ').splice(-2, 1)[0]
|
|
|
|
eps.push(ep)
|
|
|
|
|
|
|
|
if (ep <= searchData.untilEp && ep >= searchData.fromEp) {
|
|
|
|
magnets.push({
|
|
|
|
name: elem.title[0],
|
|
|
|
link: `magnet:?xt=urn:btih:${choice === 'si' ? elem['nyaa:infoHash'][0] : elem.link[0].split('/').slice(-1)}`
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
sendRes({
|
|
|
|
minEp: _.min(eps),
|
|
|
|
maxEp: _.max(eps),
|
|
|
|
magnets
|
|
|
|
}, res)
|
|
|
|
}
|
|
|
|
|
2017-05-29 11:23:52 +00:00
|
|
|
const download = (req, res) => {
|
|
|
|
req.on('data', (chunk) => {
|
|
|
|
chunk = JSON.parse(chunk)
|
|
|
|
|
|
|
|
const choice = chunk.choice
|
|
|
|
|
2017-05-30 10:26:35 +00:00
|
|
|
console.log('[Nyaa] (Download): Received a download request. Choice is ' + choice)
|
|
|
|
|
2017-05-29 11:23:52 +00:00
|
|
|
const searchData = {
|
|
|
|
quality: chunk.quality,
|
|
|
|
name: chunk.name,
|
|
|
|
fansub: chunk.fansub,
|
|
|
|
fromEp: chunk.fromEp,
|
|
|
|
untilEp: chunk.untilEp
|
|
|
|
}
|
|
|
|
|
2017-05-30 10:26:35 +00:00
|
|
|
console.log(searchData)
|
|
|
|
|
2017-07-27 09:25:16 +00:00
|
|
|
const term = `[${searchData.fansub}]+${searchData.quality}+${searchData.name}+` + (choice === 'si' ? '-unofficial' : '')
|
2017-05-29 11:23:52 +00:00
|
|
|
|
2017-05-27 18:41:10 +00:00
|
|
|
if (choice === 'si') {
|
2017-05-29 11:23:52 +00:00
|
|
|
nyaa.searchSi(term).then((data) => {
|
2017-09-25 11:55:58 +00:00
|
|
|
formatMagnets(data, searchData, choice, res)
|
2017-05-29 11:23:52 +00:00
|
|
|
}).catch(/* istanbul ignore next */(err) => {
|
|
|
|
console.log('[Nyaa]: An error occurred...\n' + err)
|
2017-07-24 08:45:00 +00:00
|
|
|
res.status(204).send()
|
2017-05-29 11:23:52 +00:00
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
} else {
|
2017-05-29 11:23:52 +00:00
|
|
|
nyaa.searchPantsu(term).then((data) => {
|
2017-09-25 11:55:58 +00:00
|
|
|
formatMagnets(data, searchData, choice, res)
|
2017-05-29 11:23:52 +00:00
|
|
|
}).catch(/* istanbul ignore next */(err) => {
|
2017-07-27 09:25:16 +00:00
|
|
|
console.log(err.message)
|
2017-07-24 08:45:00 +00:00
|
|
|
res.status(204).send()
|
2017-05-29 11:23:52 +00:00
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
}
|
2017-05-29 11:23:52 +00:00
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 11:23:52 +00:00
|
|
|
// TODO remove this wtf ?
|
|
|
|
/* istanbul ignore next */
|
2017-07-24 08:45:00 +00:00
|
|
|
const getLatest = (query, res) => {
|
2017-05-27 18:41:10 +00:00
|
|
|
const fansub = query.fansub
|
2017-05-29 11:23:52 +00:00
|
|
|
const choice = query.choice
|
|
|
|
const quality = query.quality
|
2017-05-27 18:41:10 +00:00
|
|
|
|
|
|
|
let counter = 0
|
|
|
|
let toReturn = []
|
|
|
|
|
|
|
|
if (choice === 'si') {
|
2017-07-03 18:22:48 +00:00
|
|
|
nyaa.searchSi(`[${fansub}] ${quality} -unofficial`, 18).then((data) => {
|
2017-05-27 18:41:10 +00:00
|
|
|
for (let i = 0; i < 18; ++i) {
|
2017-08-04 09:16:05 +00:00
|
|
|
const realName = removeUnwanted(data[i].title[0])
|
2017-05-27 18:41:10 +00:00
|
|
|
const name = realName.split(' ').slice(1).join(' ')
|
2017-05-29 11:23:52 +00:00
|
|
|
const rawName = name.split(' ').slice(0, -3).join(' ')
|
2017-05-27 18:41:10 +00:00
|
|
|
const researchName = rawName.split(' ').join('').toLowerCase()
|
2017-05-29 11:23:52 +00:00
|
|
|
const ep = name.split(' ').splice(-2, 1)[0]
|
|
|
|
const link = `magnet:?xt=urn:btih:${data[i]['nyaa:infoHash'][0]}`
|
|
|
|
|
|
|
|
malScraper.getResultsFromSearch(rawName).then((items) => {
|
|
|
|
return malScraper.getInfoFromURI(malScraper.getBestMatch(rawName, items))
|
|
|
|
}).then((item) => {
|
|
|
|
const picture = item.picture
|
|
|
|
const fullSynopsis = item.synopsis
|
|
|
|
const synopsis = item.synopsis.length > 170
|
|
|
|
? item.synopsis.slice(0, 175) + '...'
|
|
|
|
: fullSynopsis
|
|
|
|
|
|
|
|
toReturn[i] = {
|
|
|
|
name: name,
|
|
|
|
rawName: rawName,
|
|
|
|
researchName: researchName,
|
|
|
|
ep: ep,
|
|
|
|
magnetLink: link,
|
|
|
|
picture: picture,
|
|
|
|
synopsis: synopsis,
|
|
|
|
fullSynopsis: fullSynopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
++counter
|
|
|
|
if (counter === 18) {
|
|
|
|
console.log('[Nyaa] (Releases): Sending Latest releases.')
|
|
|
|
res.writeHead(200, {'Content-type': 'application/json'})
|
|
|
|
res.write(JSON.stringify(toReturn))
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
}).catch(/* istanbul ignore next */(err) => {
|
|
|
|
console.log('[MalScraper] (Releases): An error occurred...\n' + err)
|
2017-07-24 08:45:00 +00:00
|
|
|
res.status(202).send()
|
2017-05-29 11:23:52 +00:00
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
}
|
2017-05-29 11:23:52 +00:00
|
|
|
}).catch(/* istanbul ignore next */(err) => {
|
2017-05-30 07:26:24 +00:00
|
|
|
console.log('[Nyaa] (Releases): An error occurred...\n' + err)
|
2017-07-24 08:45:00 +00:00
|
|
|
res.status(204).send()
|
2017-05-29 11:23:52 +00:00
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
} else if (choice === 'pantsu') {
|
|
|
|
nyaa.searchPantsu(`[${fansub}] ${quality}`, 18).then((data) => {
|
|
|
|
for (let i = 0; i < 18; ++i) {
|
2017-08-04 09:16:05 +00:00
|
|
|
const realName = removeUnwanted(data[i].title[0])
|
2017-05-27 18:41:10 +00:00
|
|
|
const name = realName.split(' ').slice(1).join(' ')
|
2017-05-29 11:23:52 +00:00
|
|
|
const rawName = name.split(' ').slice(0, -3).join(' ')
|
2017-05-27 18:41:10 +00:00
|
|
|
const researchName = rawName.split(' ').join('').toLowerCase()
|
2017-08-23 15:16:01 +00:00
|
|
|
const ep = name.split(' ').splice(-2, 1)
|
2017-05-27 18:41:10 +00:00
|
|
|
const link = `${data[i].link[0]}`
|
|
|
|
|
2017-05-29 11:23:52 +00:00
|
|
|
malScraper.getResultsFromSearch(rawName).then((items) => {
|
|
|
|
return malScraper.getInfoFromURI(malScraper.getBestMatch(rawName, items))
|
|
|
|
}).then((item) => {
|
|
|
|
const picture = item.picture
|
|
|
|
const fullSynopsis = item.synopsis
|
|
|
|
const synopsis = item.synopsis.length > 170
|
|
|
|
? item.synopsis.slice(0, 175) + '...'
|
|
|
|
: fullSynopsis
|
|
|
|
|
|
|
|
toReturn[i] = {
|
|
|
|
name: name,
|
|
|
|
rawName: rawName,
|
|
|
|
researchName: researchName,
|
|
|
|
ep: ep,
|
|
|
|
magnetLink: link,
|
|
|
|
picture: picture,
|
|
|
|
synopsis: synopsis,
|
|
|
|
fullSynopsis: fullSynopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
++counter
|
|
|
|
if (counter === 18) {
|
|
|
|
console.log('[Nyaa] (Releases): Sending Latest releases.')
|
|
|
|
res.writeHead(200, {'Content-type': 'application/json'})
|
|
|
|
res.write(JSON.stringify(toReturn))
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
}).catch(/* istanbul ignore next */(err) => {
|
|
|
|
console.log('[MalScraper] (Releases): An error occurred...\n' + err)
|
2017-07-24 08:45:00 +00:00
|
|
|
res.status(202).send()
|
2017-05-29 11:23:52 +00:00
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
}
|
2017-05-29 11:23:52 +00:00
|
|
|
}).catch(/* istanbul ignore next */(err) => {
|
2017-05-30 07:26:24 +00:00
|
|
|
console.log('[Nyaa] (Releases): An error occurred...\n' + err)
|
2017-07-24 08:45:00 +00:00
|
|
|
res.status(204).send()
|
2017-05-29 11:23:52 +00:00
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
download,
|
|
|
|
getLatest
|
|
|
|
}
|