From a2b6a281056dcb7364a8a40ea9ab871686ac4cc7 Mon Sep 17 00:00:00 2001 From: hoilc Date: Sat, 15 Feb 2020 21:34:18 +0800 Subject: [PATCH] feat: add 2048 torrent support (#3969) --- docs/multimedia.md | 2 +- lib/routes/2048/bbs.js | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/docs/multimedia.md b/docs/multimedia.md index 9200d0cf0..51e358a40 100644 --- a/docs/multimedia.md +++ b/docs/multimedia.md @@ -8,7 +8,7 @@ pageClass: routes ### 论坛更新 - + ## 60-Second Science - Scientific American diff --git a/lib/routes/2048/bbs.js b/lib/routes/2048/bbs.js index 35e9fe439..cebaae724 100644 --- a/lib/routes/2048/bbs.js +++ b/lib/routes/2048/bbs.js @@ -37,16 +37,34 @@ module.exports = async (ctx) => { .text() .replace(/»/g, '-'); - const parseContent = (htmlString) => { + const parseContent = async (htmlString) => { const $ = cheerio.load(htmlString); const time = $('.tiptop.cc > .fl.gray').attr('title'); const content = $('.tpc_content'); - return { + const result = { description: content.html(), pubDate: time ? new Date(time) : new Date(), }; + + const test_external_torrent = content.find('a').filter(function() { + return $(this) + .attr('href') + .match(/\/list\.php\?name=\w{32}/); + }); + + if (test_external_torrent.length !== 0) { + const torrent_url = test_external_torrent[0].attribs.href; + const response = await got.get(torrent_url); + const magnet_url = response.data.match(/"(magnet:\?.*?)"/); + if (magnet_url) { + result.enclosure_url = magnet_url[1]; + result.enclosure_type = 'application/x-bittorrent'; + } + } + + return result; }; const out = await Promise.all( @@ -86,10 +104,12 @@ module.exports = async (ctx) => { try { const response = await got.get(link); - const result = parseContent(response.data); + const result = await parseContent(response.data); rssitem.description = result.description; rssitem.pubDate = result.pubDate; + rssitem.enclosure_url = result.enclosure_url; + rssitem.enclosure_type = result.enclosure_type; } catch (err) { return Promise.resolve(''); }