mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-16 02:42:57 +08:00
feat(route): cw (#10758)
* feat(route): cw * fix: typo * fix: use custom got
This commit is contained in:
63
lib/v2/cw/utils.js
Normal file
63
lib/v2/cw/utils.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const { CookieJar } = require('tough-cookie');
|
||||
const cookieJar = new CookieJar();
|
||||
const config = require('@/config').value;
|
||||
|
||||
const baseUrl = 'https://www.cw.com.tw';
|
||||
|
||||
const got = require('@/utils/got').extend({
|
||||
headers: {
|
||||
'User-Agent': config.trueUA,
|
||||
},
|
||||
});
|
||||
|
||||
const parseList = ($, limit) =>
|
||||
$('.caption')
|
||||
.toArray()
|
||||
.map((item) => {
|
||||
item = $(item);
|
||||
return {
|
||||
title: item.find('h3').text(),
|
||||
link: item.find('h3 a').attr('href'),
|
||||
pubDate: parseDate(item.find('time').text()),
|
||||
};
|
||||
})
|
||||
.slice(0, limit);
|
||||
|
||||
const parseItems = (list, tryGet) =>
|
||||
Promise.all(
|
||||
list.map((item) =>
|
||||
tryGet(item.link, async () => {
|
||||
const { data: response } = await got(item.link, {
|
||||
cookieJar,
|
||||
});
|
||||
|
||||
const $ = cheerio.load(response);
|
||||
const meta = JSON.parse($('head script[type="application/ld+json"]').eq(0).text());
|
||||
$('.article__head .breadcrumb, .article__head h1, .article__provideViews, .ad').remove();
|
||||
$('img.lazyload').each((_, img) => {
|
||||
if (img.attribs['data-src']) {
|
||||
img.attribs.src = img.attribs['data-src'];
|
||||
delete img.attribs['data-src'];
|
||||
}
|
||||
});
|
||||
|
||||
item.title = $('head title').text();
|
||||
item.category = $('meta[name=keywords]').attr('content').split(',');
|
||||
item.pubDate = parseDate(meta.datePublished);
|
||||
item.author = meta.author.name.replace(',', ' ') || meta.publisher.name;
|
||||
item.description = $('.article__head .container').html() + $('.article__content').html();
|
||||
|
||||
return item;
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
module.exports = {
|
||||
baseUrl,
|
||||
cookieJar,
|
||||
got,
|
||||
parseList,
|
||||
parseItems,
|
||||
};
|
||||
Reference in New Issue
Block a user