From f5bacca85572c107c59ef6d9df40d7d85c417bb1 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sat, 18 Aug 2018 08:37:57 +0300 Subject: [PATCH] fix(nyaa): nyaa test fix --- src/background/strategies/nyaa.js | 45 ++++++++++++++++++------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/background/strategies/nyaa.js b/src/background/strategies/nyaa.js index 7ccf7af..e78f74b 100644 --- a/src/background/strategies/nyaa.js +++ b/src/background/strategies/nyaa.js @@ -1,4 +1,3 @@ - const fetch = require('node-fetch') const cheerio = require('cheerio') @@ -14,24 +13,32 @@ module.exports = class Nyaa async parse() { - let html; - try { - html = await fetch('https://nyaa.si/' + (this.threadId ? `view/${this.threadId}` : (this.hash ? `?q=${this.hash}` : ''))) - } catch(err) { - return - } - if(!html) - return - html = await html.textConverted() - const $ = cheerio.load(html) - let topicTitle = $('.panel-title').first().text() - if(!topicTitle) - return + if(this.promise) + await this.promise - topicTitle = topicTitle.replace(/\t/g, '').replace(/\n/g, '') - return { - name: topicTitle, - description: $('#torrent-description').text(), - } + this.promise = new Promise(async (resolve) => { + let html; + try { + html = await fetch('https://nyaa.si/' + (this.threadId ? `view/${this.threadId}` : (this.hash ? `?q=${this.hash}` : ''))) + } catch(err) { + resolve() + } + if(!html) + resolve() + html = await html.textConverted() + const $ = cheerio.load(html) + let topicTitle = $('.panel-title').first().text() + if(!topicTitle) + resolve() + + topicTitle = topicTitle.replace(/\t/g, '').replace(/\n/g, '') + + resolve({ + name: topicTitle, + description: $('#torrent-description').text(), + }) + }) + + return await this.promise } } \ No newline at end of file