diff --git a/src/background/strategies/nyaa.js b/src/background/strategies/nyaa.js new file mode 100644 index 0000000..bbc1838 --- /dev/null +++ b/src/background/strategies/nyaa.js @@ -0,0 +1,30 @@ + +const fetch = require('node-fetch') +const cheerio = require('cheerio') + +module.exports = class Nyaa +{ + async findHash(hash) + { + this.hash = hash + return await this.parse() + } + + async parse() + { + let html = await fetch('https://nyaa.si/' + (this.threadId ? `view/${this.threadId}` : (this.hash ? `?q=${this.hash}` : ''))) + if(!html) + return + html = await html.text() + const $ = cheerio.load(html) + let topicTitle = $('.panel-title').first().text() + if(!topicTitle) + return + + topicTitle = topicTitle.replace(/\t/g, '').replace(/\n/g, '') + return { + name: topicTitle, + description: $('#torrent-description').text(), + } + } +} \ No newline at end of file diff --git a/tests/strategies/nyaa.test.js b/tests/strategies/nyaa.test.js new file mode 100644 index 0000000..4d0cc08 --- /dev/null +++ b/tests/strategies/nyaa.test.js @@ -0,0 +1,15 @@ +const { assert } = require('chai') +const Nyaa = require('../../src/background/strategies/nyaa.js') + +describe("nyaa", () => { + let nyaa = new Nyaa(); + + it("findHash", async function() { + const data = await nyaa.findHash('b57cc972ecc31dd31e9349017e5d26c164e08906') + assert.equal(data.name, '[HorribleSubs] One Piece - 794 [1080p].mkv') + }) + + it("notFound", async function() { + assert(!await nyaa.findHash('FFEDA8DF683F34A08BE89026114E6C05F881DCC0')) + }) +});