feat(route): cw (#10758)

* feat(route): cw

* fix: typo

* fix: use custom got
This commit is contained in:
Tony
2022-09-11 19:59:52 +02:00
committed by GitHub
parent 878266c535
commit b8e567b9e4
8 changed files with 210 additions and 0 deletions

63
lib/v2/cw/utils.js Normal file
View 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,
};