Skip to content

Commit

Permalink
fix(route): chinafactcheck ua (#13602)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Oct 21, 2023
1 parent 8aa7d7c commit 74fb6c6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
41 changes: 20 additions & 21 deletions lib/v2/chinafactcheck/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,34 @@ const cheerio = require('cheerio');
const got = require('@/utils/got');

module.exports = async (ctx) => {
const rssTitle = '有据|国际新闻事实核查';

const response = await got('https://chinafactcheck.com');
const response = await got(utils.siteLink, {
headers: {
'user-agent': utils.trueUA,
},
});
const $ = cheerio.load(response.data);

const articlesLinkListNode = $('.post-info-box .post-thumb a')
.map((_, item) => $(item).attr('href'))
.get();
const articlesLinkList = Array.from(new Set(articlesLinkListNode));
const articlesLink = $('.post-info-box .post-thumb a')
.toArray()
.map((item) => ({ link: $(item).attr('href') }));

const articles = await Promise.all(
articlesLinkList.map(async (item) => {
const articlesLink = item;

const detail = await ctx.cache.tryGet(articlesLink, () => utils.getArticleDetail(articlesLink));
articlesLink.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { title, author, pubDate, description, category } = await utils.getArticleDetail(item.link);

const element = {
title: detail.title,
author: detail.author,
pubDate: detail.time,
description: detail.description,
link: articlesLink,
};
return element;
})
item.title = title;
item.author = author;
item.pubDate = pubDate;
item.description = description;
item.category = category;
return item;
})
)
);

ctx.state.data = {
title: rssTitle,
title: $('head title').text(),
link: utils.siteLink,
item: articles,
};
Expand Down
1 change: 1 addition & 0 deletions lib/v2/chinafactcheck/maintainer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
'': ['kdanfly'],
'/': ['kdanfly'],
};
1 change: 0 additions & 1 deletion lib/v2/chinafactcheck/templates/description.art

This file was deleted.

32 changes: 16 additions & 16 deletions lib/v2/chinafactcheck/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
const { art } = require('@/utils/render');
const path = require('path');
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const { trueUA } = require('@/config').value;

const siteLink = 'https://chinafactcheck.com';

const renderDesc = (desc) =>
art(path.join(__dirname, 'templates/description.art'), {
desc,
});

const cleanDom = (dom) => {
dom('*[style]').removeAttr('style');
dom('br').remove();
Expand All @@ -30,31 +24,37 @@ const cleanDom = (dom) => {
};

const getArticleDetail = async (link) => {
const response = await got(link);
const response = await got(link, {
headers: {
'user-agent': trueUA,
},
});
const $ = cleanDom(cheerio.load(response.data));

const title = $('.content-head h2').text().trim();
const author = $('.content-persons p span:last').text().trim();
const time = parseDate($('.content-time').text().trim(), 'YYYY-MM-DD');
const pubDate = parseDate($('.content-time').text().trim(), 'YYYY-MM-DD');

const description = {
article: $('div[class=content-list-box]').html(),
};
return new ArticleDetail(title, author, time, renderDesc(description));
const description = $('div[class=content-list-box]').html();
const category = $('.content-tags a[rel="tag"]')
.toArray()
.map((item) => $(item).text().trim());
return new ArticleDetail(title, author, pubDate, description, category);
};
class ArticleDetail {
constructor(title, author, time, description) {
constructor(title, author, pubDate, description, category) {
this.title = title;
this.author = author;
this.time = time;
this.pubDate = pubDate;
this.description = description;
this.category = category;
}
}

module.exports = {
siteLink,
renderDesc,
cleanDom,
getArticleDetail,
ArticleDetail,
trueUA,
};

0 comments on commit 74fb6c6

Please sign in to comment.