From 7a350cf31dca843f217b7fefdf1419d7afd46744 Mon Sep 17 00:00:00 2001 From: Kylart Date: Tue, 30 May 2017 12:26:35 +0200 Subject: [PATCH] Implemented download with config's fansub and ANY name with nyaapi Fixed a bug where saved config was malformed Fixed tests --- assets/api/horrible.js | 38 ----------------------- assets/api/main.js | 10 +----- assets/api/nyaa.js | 8 +++-- pages/downloader.vue | 32 +++++++++----------- store/index.js | 69 +++++++++++++++++++++++++++++++----------- test/server.test.js | 60 ++++++------------------------------ 6 files changed, 81 insertions(+), 136 deletions(-) diff --git a/assets/api/horrible.js b/assets/api/horrible.js index b2263c4..14ef3d1 100644 --- a/assets/api/horrible.js +++ b/assets/api/horrible.js @@ -65,41 +65,3 @@ exports.getLatest = (url, res) => { res.end() }) } - -exports.download = (req, res) => { - req.on('data', (chunk) => { - chunk = JSON.parse(chunk) - - console.log('[Horrible] (Download): Received a download request.') - - const searchData = { - quality: chunk.quality, - name: chunk.name, - fromEp: chunk.fromEp, - untilEp: chunk.untilEp - } - - console.log(searchData) - - // Calling api - horribleApi.getMagnetsFromAnimeName(searchData).then((links) => { - console.log('[Horrible] (Download): Request fulfilled!') - - res.writeHead(200, {'Content-type': 'application/json'}) - res.write(JSON.stringify(links)) - res.end() - }).catch((err) => { - console.log('[Horrible] (Download): An error occurred...\n' + err) - res.writeHead(204, {}) - res.end() - }) - }) -} - -exports.getShowsList = (res) => { - const list = horribleApi.getShowsOnly() - - res.writeHead(200, {'Content-type': 'application/json'}) - res.write(JSON.stringify(list)) - res.end() -} diff --git a/assets/api/main.js b/assets/api/main.js index b75ad1c..2289d95 100644 --- a/assets/api/main.js +++ b/assets/api/main.js @@ -118,18 +118,10 @@ const route = (nuxt) => { nyaa.getLatest(url, res) break - case '/downloadNyaa': - nyaa.download(req, res) - break - case '/getLatest.json': horrible.getLatest(url, res) break - case '/getAllShows.json': - horrible.getShowsList(res) - break - /* istanbul ignore next */ case '/openThis': openExternal(url, res) @@ -140,7 +132,7 @@ const route = (nuxt) => { break case '/download': - horrible.download(req, res) + nyaa.download(req, res) break case '/news.json': diff --git a/assets/api/nyaa.js b/assets/api/nyaa.js index edb3b6e..4b5b04f 100644 --- a/assets/api/nyaa.js +++ b/assets/api/nyaa.js @@ -14,12 +14,12 @@ const sendRes = (object, res) => { const download = (req, res) => { req.on('data', (chunk) => { - console.log('[Nyaa] (Download): Received a download request.') - chunk = JSON.parse(chunk) const choice = chunk.choice + console.log('[Nyaa] (Download): Received a download request. Choice is ' + choice) + const searchData = { quality: chunk.quality, name: chunk.name, @@ -28,6 +28,8 @@ const download = (req, res) => { untilEp: chunk.untilEp } + console.log(searchData) + const term = `[${searchData.fansub}]+${searchData.quality}+${searchData.name}` if (choice === 'si') { @@ -37,7 +39,9 @@ const download = (req, res) => { data.forEach((elem) => { const ep = elem.title[0].split(' ').splice(-2, 1)[0] + if (ep <= searchData.untilEp && ep >= searchData.fromEp) { + console.log(ep) magnets.push(`magnet:?xt=urn:btih:${elem['nyaa:infoHash'][0]}`) } }) diff --git a/pages/downloader.vue b/pages/downloader.vue index 3c629f4..faa45bf 100644 --- a/pages/downloader.vue +++ b/pages/downloader.vue @@ -19,16 +19,15 @@ - - - + + + @@ -154,9 +153,6 @@ computed: { formValues: function () { return this.$store.state.downloaderForm - }, - list: function () { - return this.$store.state.downloaderList } }, methods: { @@ -183,16 +179,16 @@ next (number) { switch (number) { case 1: - document.getElementsByName('input-2')[0].focus() + document.getElementsByName('from-ep-input')[0].focus() break case 2: - document.getElementsByName('input-3')[0].focus() + document.getElementsByName('until-ep-input')[0].focus() break case 3: document.getElementById('download-btn').click() - document.getElementsByName('input-1')[0].focus() + document.getElementsByName('name-input')[0].focus() break default: @@ -202,11 +198,11 @@ previous (number) { switch (number) { case 2: - if (!this.formValues.fromEp) document.getElementsByName('input-1')[0].focus() + if (!this.formValues.fromEp) document.getElementsByName('name-input')[0].focus() break case 3: - if (!this.formValues.untilEp) document.getElementsByName('input-2')[0].focus() + if (!this.formValues.untilEp) document.getElementsByName('from-ep-input')[0].focus() break default: diff --git a/store/index.js b/store/index.js index 6a41c63..4e099f2 100644 --- a/store/index.js +++ b/store/index.js @@ -28,7 +28,6 @@ const store = new Vuex.Store({ show: false, text: '' }, - downloaderList: [], downloaderForm: { name: '', fromEp: '', @@ -93,10 +92,6 @@ const store = new Vuex.Store({ setReleaseParams (state, data) { state.releaseParams = data }, - setDownloaderList (state, data) { - state.downloaderList = data - log(`${data.length} anime name loaded.`) - }, setInfoSnackbar (state, data) { state.infoSnackbar.text = data state.infoSnackbar.show = true @@ -209,20 +204,14 @@ const store = new Vuex.Store({ const {data} = await axios.get('getConfig.json') commit('init', data.config) - dispatch('downloaderInit').catch(err => { void (err) }) dispatch('releasesInit').catch(err => { void (err) }) dispatch('seasonsInit').catch(err => { void (err) }) - dispatch('newsInit').catch(err => { void (err) }) + // dispatch('newsInit').catch(err => { void (err) }) dispatch('localInit').catch(err => { void (err) }) dispatch('listInit').catch(err => { void (err) }) dispatch('getHistory').catch(err => { void (err) }) } }, - async downloaderInit ({commit}) { - const {data} = await axios.get('getAllShows.json') - - commit('setDownloaderList', data) - }, async releasesInit ({state, commit, dispatch}) { // TODO refactor this since it is a bit hard coded console.log('[INIT] Releases') @@ -447,14 +436,16 @@ const store = new Vuex.Store({ log(`Received a request to download ${name} from ep ${fromEp} to ep ${untilEp}. Transmitting...`) - const {data, status} = await axios.post('download', { + const infos = { name: name, quality: quality, - fromEp: fromEp, - untilEp: untilEp - }) + fromEp: +fromEp, + untilEp: +untilEp, + fansub: state.config.fansub, + choice: 'si' + } - state.downloaderForm.loading = false + const {data, status} = await axios.post('download', infos) if (status === 200) { log(`Request fulfilled!`) @@ -474,10 +465,52 @@ const store = new Vuex.Store({ window.open(link) }) } + } else if (status === 204) { + log('nyaa.si is down, trying with nyaa.pantsu.cat') + + const {data, status} = await axios.post('download', { + name: name, + quality: quality, + fromEp: fromEp, + untilEp: untilEp, + fansub: state.config.fansub, + choice: 'pantsu' + }) + + if (status === 200) { + log(`Request fulfilled!`) + + if (magnets === true) + { + const lastEp = fromEp !== '1' ? +fromEp + +data.length : data.length + log(`User says he prefers having magnets hashes.`) + commit('setDownloaderModal', { + show: true, + title: `${name.replace('_', ' ')}\t ${fromEp} - ${lastEp}`, + text: data + }) + } + else + { + log(`Opening torrents directly on preferred torrent client.`) + + data.forEach((link) => { + window.open(link) + }) + } + } else { + log('Unknown error occurred. nyaa.si and nyaa.pantsu.cat seems both down.') + + commit('setInfoSnackbar', 'Sorry. KawAnime was not able to get your torrents...') + } } + + state.downloaderForm.loading = false }, saveConfig ({}, data) { // eslint-disable-line - axios.post('saveConfig', JSON.stringify(data)).then((res) => { + axios.post('saveConfig', JSON.stringify({ + config: data + })).then((res) => { if (res.status === 200) { log(`Successfully updated config!`) } }).catch((err) => { log(`An error occurred while saving config: ${err}`) diff --git a/test/server.test.js b/test/server.test.js index a21f98f..c335507 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -117,12 +117,6 @@ test('/saveConfig.json route exits and saves config and return 200', async t => writeFileSync(join(DIR, 'config.json'), JSON.stringify(config), 'utf-8') }) -test('/getAllShows.json exits and returns a list of names', async t => { - const { data } = await axios.get(`${uri}/getAllShows.json`) - - t.true(data.length > 1) -}) - test('/getLatest.json exits and returns 18 elements with right keys at 720p', async t => { const { data, status } = await axios.get(`${uri}/getLatest.json?quality=720p`) @@ -141,21 +135,9 @@ test('/getLatest.json exits and returns 18 elements with right keys at 720p', as } }) -test('/download Mahou Shoujo Ikusei Keikaku at 720p exits and returns all magnets', async t => { - const { data } = await axios.post(`${uri}/download`, { - name: 'Mahou Shoujo Ikusei Keikaku', - quality: '720p', - fromEp: 0, - untilEp: 20000 - }) - - t.is(data.length, 12) - t.not(data[0], '') -}) - -test('/downloadNyaa Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p on nyaa.si exits and returns all' + +test('/download Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p on nyaa.si exits and returns all' + ' magnets', async t => { - const { data } = await axios.post(`${uri}/downloadNyaa`, { + const { data } = await axios.post(`${uri}/download`, { name: 'Mahou Shoujo Ikusei Keikaku', quality: '720p', fromEp: 0, @@ -168,9 +150,9 @@ test('/downloadNyaa Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p on nya t.not(data[0], '') }) -test('/downloadNyaa Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p on nyaa.pantsu.cat exits and returns' + +test('/download Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p on nyaa.pantsu.cat exits and returns' + ' all magnets', async t => { - const { data } = await axios.post(`${uri}/downloadNyaa`, { + const { data } = await axios.post(`${uri}/download`, { name: 'Mahou Shoujo Ikusei Keikaku', quality: '720p', fromEp: 0, @@ -201,10 +183,9 @@ test('/getLatestNyaa exits and returns 18 elements with right keys at 720p on ny } }) -test('/downloadNyaa Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p from ep 3 to 9 on nyaa.si exits and' + - ' returns' + - ' all magnets', async t => { - const { data } = await axios.post(`${uri}/downloadNyaa`, { +test('/download Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p from ep 3 to 9 on nyaa.si exits and' + + ' returns 7 magnets', async t => { + const { data } = await axios.post(`${uri}/download`, { name: 'Mahou Shoujo Ikusei Keikaku', quality: '720p', fromEp: 3, @@ -217,10 +198,10 @@ test('/downloadNyaa Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p from e t.not(data[0], '') }) -test('/downloadNyaa Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p from ep 3 to 9 on nyaa.pantsu.cat' + +test('/download Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p from ep 3 to 9 on nyaa.pantsu.cat' + ' exits and returns' + ' all magnets', async t => { - const { data } = await axios.post(`${uri}/downloadNyaa`, { + const { data } = await axios.post(`${uri}/download`, { name: 'Mahou Shoujo Ikusei Keikaku', quality: '720p', fromEp: 3, @@ -233,29 +214,6 @@ test('/downloadNyaa Mahou Shoujo Ikusei Keikaku with HorribleSubs at 720p from e t.not(data[0], '') }) -test('/download Akame ga Kill! at 1080p exits and returns only 4 eps', async t => { - const { data } = await axios.post(`${uri}/download`, { - name: 'Akame ga Kill!', - quality: '720p', - fromEp: 3, - untilEp: 6 - }) - - t.is(data.length, 4) - t.not(data[0], '') -}) - -test('/download with wrong entries exits and returns 204', async t => { - const { status } = await axios.post(`${uri}/download`, { - name: 'Non-existent anime', - quality: '30p', - fromEp: 3, - untilEp: 6 - }) - - t.is(status, 204) -}) - test('/getLatest.json exits and returns 204 status at 30p', async t => { const { status } = await axios.get(`${uri}/getLatest.json?quality=30p`)