feat: 微信公众号(tgchanel)全文 (#2208)

* enhance: 微信公众号(tgchanel)全文

fix 2 typos

* enhance: 添加作者并兼容多个公众号情况
This commit is contained in:
Chenyang Shi
2019-05-24 17:44:17 +08:00
committed by DIYgod
parent e21dec83d3
commit e895dcf05a
2 changed files with 35 additions and 8 deletions

View File

@@ -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()
);