From 604e05b369c48b18586fa2200bed95f4f8d29ce2 Mon Sep 17 00:00:00 2001 From: junfengP <840282629@qq.com> Date: Sat, 27 Nov 2021 15:28:45 +0800 Subject: [PATCH] fix: undefined error when no magnet & torrent (#7745) --- lib/routes/dsndsht23/index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/routes/dsndsht23/index.js b/lib/routes/dsndsht23/index.js index 125fda1190..b5c8190b1f 100644 --- a/lib/routes/dsndsht23/index.js +++ b/lib/routes/dsndsht23/index.js @@ -92,18 +92,25 @@ module.exports = async (ctx) => { } const description = (postMessage.html() || '抓取原帖失败').replace(/ignore_js_op/g, 'div'); - const magnet = postMessage.find('div.blockcode li').text(); - const isMag = magnet.startsWith('magnet'); - const enclosureUrl = isMag ? magnet : url.resolve(host, postMessage.find('a[href^=forum\\.php\\?mod\\=attachment]:not([href$=nothumb\\=yes])').attr('href')); - const single = { title, link: itemUrl, description, pubDate: new Date(date).toUTCString(), - enclosure_url: enclosureUrl, - enclosure_type: isMag ? 'application/x-bittorrent' : 'application/octet-stream', }; + const magnet = postMessage.find('div.blockcode li').text(); + const isMag = magnet.startsWith('magnet'); + const torrent = postMessage.find('a[href^=forum\\.php\\?mod\\=attachment]:not([href$=nothumb\\=yes])').attr('href'); + + const hasEnclosureUrl = isMag || torrent !== undefined; + if (hasEnclosureUrl) { + const enclosureUrl = isMag ? magnet : url.resolve(host, torrent); + const enclosure = { + enclosure_url: enclosureUrl, + enclosure_type: isMag ? 'application/x-bittorrent' : 'application/octet-stream', + }; + Object.assign(single, enclosure); + } if (description !== '抓取原帖失败') { ctx.cache.set(itemUrl, JSON.stringify(single)); }