2017-05-27 18:41:10 +00:00
|
|
|
/**
|
|
|
|
* Created by Kylart on 27/05/2017.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import nyaa from 'nyaapi'
|
|
|
|
import malScraper from 'mal-scraper'
|
|
|
|
import qs from 'querystring'
|
|
|
|
|
|
|
|
const sendRes = (object, res) => {
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'})
|
|
|
|
res.write(JSON.stringify(object))
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
|
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-05-29 11:23:52 +00:00
|
|
|
const term = `[${searchData.fansub}]+${searchData.quality}+${searchData.name}`
|
|
|
|
|
2017-05-27 18:41:10 +00:00
|
|
|
if (choice === 'si') {
|
2017-05-29 11:23:52 +00:00
|
|
|
nyaa.searchSi(term).then((data) => {
|
|
|
|
const magnets = []
|
|
|
|
|
|
|
|
data.forEach((elem) => {
|
|
|
|
const ep = elem.title[0].split(' ').splice(-2, 1)[0]
|
|
|
|
|
2017-05-30 10:26:35 +00:00
|
|
|
|
2017-05-29 11:23:52 +00:00
|
|
|
if (ep <= searchData.untilEp && ep >= searchData.fromEp) {
|
2017-05-30 10:26:35 +00:00
|
|
|
console.log(ep)
|
2017-05-29 11:23:52 +00:00
|
|
|
magnets.push(`magnet:?xt=urn:btih:${elem['nyaa:infoHash'][0]}`)
|
|
|
|
}
|
2017-05-27 18:41:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
sendRes(magnets, res)
|
2017-05-29 11:23:52 +00:00
|
|
|
}).catch(/* istanbul ignore next */(err) => {
|
|
|
|
console.log('[Nyaa]: An error occurred...\n' + err)
|
|
|
|
res.writeHead(204, {})
|
|
|
|
res.end()
|
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
} else {
|
2017-05-29 11:23:52 +00:00
|
|
|
nyaa.searchPantsu(term).then((data) => {
|
|
|
|
const magnets = []
|
2017-05-27 18:41:10 +00:00
|
|
|
|
2017-05-29 11:23:52 +00:00
|
|
|
data.forEach((elem) => {
|
|
|
|
const ep = parseInt(elem.title[0].split(' ').splice(-2, 1)[0])
|
2017-05-27 18:41:10 +00:00
|
|
|
|
2017-05-29 11:23:52 +00:00
|
|
|
if (ep <= searchData.untilEp && ep >= searchData.fromEp) {
|
|
|
|
magnets.push(elem.link[0].split('&')[0])
|
|
|
|
}
|
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
|
2017-05-29 11:23:52 +00:00
|
|
|
sendRes(magnets, res)
|
|
|
|
}).catch(/* istanbul ignore next */(err) => {
|
|
|
|
console.log('[Nyaa]: An error occurred...\n' + err)
|
|
|
|
res.writeHead(204, {})
|
|
|
|
res.end()
|
|
|
|
})
|
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-05-27 18:41:10 +00:00
|
|
|
const getLatest = (url, res) => {
|
2017-05-29 11:23:52 +00:00
|
|
|
const query = qs.parse(url.query.replace('?', ''))
|
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') {
|
|
|
|
nyaa.searchSi(`[${fansub}] ${quality}`, 18).then((data) => {
|
|
|
|
for (let i = 0; i < 18; ++i) {
|
2017-05-30 07:26:24 +00:00
|
|
|
const realName = data[i].title[0].replace(' VOSTFR', '')
|
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-05-30 07:26:24 +00:00
|
|
|
res.writeHead(202, {})
|
2017-05-29 11:23:52 +00:00
|
|
|
res.end()
|
|
|
|
})
|
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-05-29 11:23:52 +00:00
|
|
|
res.writeHead(204, {})
|
|
|
|
res.end()
|
|
|
|
})
|
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-05-30 07:26:24 +00:00
|
|
|
const realName = data[i].title[0].replace(' VOSTFR', '')
|
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()
|
|
|
|
const ep = name.split(' ').slice(-1)[0]
|
|
|
|
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-05-30 07:26:24 +00:00
|
|
|
res.writeHead(202, {})
|
2017-05-29 11:23:52 +00:00
|
|
|
res.end()
|
|
|
|
})
|
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-05-29 11:23:52 +00:00
|
|
|
res.writeHead(204, {})
|
|
|
|
res.end()
|
|
|
|
})
|
2017-05-27 18:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
download,
|
|
|
|
getLatest
|
|
|
|
}
|