Skip to content

Commit

Permalink
feat(route): Add query parameter to control full text fetching functi…
Browse files Browse the repository at this point in the history
…on (#13404)

* 澎湃新闻图片及视频与正文重复
Fixes #13401

* Save cache to different path for each mode.

* Update website/docs/routes/traditional-media.md

* Add text content to full text if the source is of video type.

---------
  • Loading branch information
dzx-dzx authored Sep 27, 2023
1 parent e5d9b52 commit fcff124
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/v2/thepaper/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ const defaultRssItem = (item) => ({

module.exports = {
ProcessItem: (item, ctx) => {
const useOldMode = ctx.query.old === 'yes';
if (item.link) {
// external link
return defaultRssItem(item);
}
const itemUrl = `https://m.thepaper.cn/detail/${item.contId}`;
return ctx.cache.tryGet(itemUrl, async () => {
return ctx.cache.tryGet(`${itemUrl}${useOldMode ? ':old' : ''}`, async () => {
const res = await got(itemUrl);
const data = JSON.parse(cheerio.load(res.data)('#__NEXT_DATA__').html());
const detailData = data.props.pageProps.detailData;
Expand All @@ -34,18 +35,23 @@ module.exports = {
let description = contentDetail.content || contentDetail.summary || contentDetail.desc || '';

if (contentDetail.videos) {
description =
art(path.join(__dirname, 'templates/video_detail.art'), {
// see https://nanmu.me/zh-cn/posts/2020/strange-html-video-tag-behavior-in-wechat/
// for video tag details
videos: contentDetail.videos,
}) + description;
description = description + contentDetail.summary;
}
if (contentDetail.images) {
description =
art(path.join(__dirname, 'templates/image_detail.art'), {
images: contentDetail.images,
}) + description;
if (useOldMode) {
if (contentDetail.videos) {
description =
art(path.join(__dirname, 'templates/video_detail.art'), {
// see https://nanmu.me/zh-cn/posts/2020/strange-html-video-tag-behavior-in-wechat/
// for video tag details
videos: contentDetail.videos,
}) + description;
}
if (contentDetail.images) {
description =
art(path.join(__dirname, 'templates/image_detail.art'), {
images: contentDetail.images,
}) + description;
}
}

const rss_item = {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/routes/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,8 @@ category 对应的关键词有

## 澎湃新闻 {#peng-pai-xin-wen}

以下所有路由可使用参数`old`以采取旧全文获取方法. 该方法会另外获取网页中的图片与视频资源. 在原始url追加`?old=yes`以启用.

### 首页头条 {#peng-pai-xin-wen-shou-ye-tou-tiao}

<Route author="HenryQW nczitzk bigfei" example="/thepaper/featured" path="/thepaper/featured"/>
Expand Down

0 comments on commit fcff124

Please sign in to comment.