From e895dcf05a0a347947ba5f4616b30d84d1d855df Mon Sep 17 00:00:00 2001 From: Chenyang Shi Date: Fri, 24 May 2019 17:44:17 +0800 Subject: [PATCH] =?UTF-8?q?=20feat:=20=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97?= =?UTF-8?q?=E5=8F=B7=EF=BC=88tgchanel=EF=BC=89=E5=85=A8=E6=96=87=20(#2208)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * enhance: 微信公众号(tgchanel)全文 fix 2 typos * enhance: 添加作者并兼容多个公众号情况 --- docs/social-media.md | 2 +- lib/routes/tencent/wechat/tgchannel.js | 41 +++++++++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/docs/social-media.md b/docs/social-media.md index dbb9bd1345..d47d1a212e 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -640,7 +640,7 @@ pageClass: routes -### 公众号(Telegrame 频道来源) +### 公众号(Telegram 频道来源) diff --git a/lib/routes/tencent/wechat/tgchannel.js b/lib/routes/tencent/wechat/tgchannel.js index ae0ee27b64..e30186bbb3 100644 --- a/lib/routes/tencent/wechat/tgchannel.js +++ b/lib/routes/tencent/wechat/tgchannel.js @@ -6,25 +6,52 @@ module.exports = async (ctx) => { const { data } = await axios.get(`https://t.me/s/${id}`); const $ = cheerio.load(data); - const list = $('.tgme_widget_message_wrap'); + const list = $('.tgme_widget_message_wrap').slice(-10); const out = await Promise.all( list - .map((index, item) => { + .map(async (index, item) => { item = $(item); - const title = item.find('.tgme_widget_message_text > a:nth-child(2)').text(); - const link = item.find('.tgme_widget_message_text > a:nth-child(2)').attr('href'); + + let author; + let title = item.find('.tgme_widget_message_text > a:nth-child(5)').text() || item.find('.tgme_widget_message_text > a:nth-child(2)').text(); + + const all_text = item.find('.tgme_widget_message_text').text(); + if (all_text.indexOf(':') !== -1) { + author = all_text.split(':')[0].split(' ')[1]; + title = author + ': ' + title; + } + + const link = item.find('.tgme_widget_message_text > a:nth-child(5)').attr('href') || item.find('.tgme_widget_message_text > a:nth-child(2)').attr('href'); const pubDate = new Date(item.find('.tgme_widget_message_date time').attr('datetime')).toUTCString(); - const description = item.find('.tgme_widget_message_text').html(); const single = { title, pubDate, link, - description, + author, }; - return single; + if (link !== undefined) { + const value = await ctx.cache.get(link); + if (value) { + single.description = value; + } else { + try { + const reponse = await axios.get(link); + const $ = cheerio.load(reponse.data); + + single.description = $('.rich_media_content') + .html() + .replace(/data-src/g, 'src'); + ctx.cache.set(link, single.description, 12 * 60 * 60); + } catch (err) { + single.description = item.find('.tgme_widget_message_text').html(); + } + } + } + + return Promise.resolve(single); }) .get() );