Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(route/SCMP): Use rss parser #15415

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 4 additions & 43 deletions lib/routes/scmp/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import { load } from 'cheerio';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import { parseItem } from './utils';
import parser from '@/utils/rss-parser';

export const route: Route = {
path: '/:category_id',
Expand Down Expand Up @@ -32,56 +30,19 @@ export const route: Route = {
async function handler(ctx) {
const categoryId = ctx.req.param('category_id');
const rssUrl = `https://www.scmp.com/rss/${categoryId}/feed`;
const { data: response } = await got(rssUrl);
const $ = load(response, {
xmlMode: true,
});

const list = $('item')
.toArray()
.map((elem) => {
const item = $(elem);
const enclosure = item.find('enclosure').first();
const mediaContent = item.find('media\\:content').toArray()[0];
const thumbnail = item.find('media\\:thumbnail').toArray()[0];
return {
title: item.find('title').text(),
description: item.find('description').text(),
link: item.find('link').text().split('?utm_source')[0],
author: item.find('author').text(),
pubDate: parseDate(item.find('pubDate').text()),
enclosure_url: enclosure?.attr('url'),
enclosure_length: enclosure?.attr('length'),
enclosure_type: enclosure?.attr('type'),
media: {
content: Object.keys(mediaContent.attribs).reduce((data, key) => {
data[key] = mediaContent.attribs[key];
return data;
}, {}),
thumbnail: thumbnail?.attribs
? Object.keys(thumbnail.attribs).reduce((data, attr) => {
data[attr] = thumbnail.attribs[attr];
return data;
}, {})
: undefined,
},
};
});
Comment on lines -53 to -69
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also remove the abilities to return enclosures and Media RSS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't enclosures included in the parsed rss feed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at the content of #15415 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The enclosure field is fixable. However in terms of media, the upstream rss-parser package does not support extracting it. It seems that this is fixed in the codebase in rbren/rss-parser@eb8a4ee , but it hasn't been made into a release.

So... Wait for upstream?

const rss = await parser.parseURL(rssUrl);

const items = await Promise.all(list.map((item) => cache.tryGet(item.link, () => parseItem(item))));
const items = await Promise.all(rss.items.map((item) => cache.tryGet(item.link, () => parseItem(item))));

ctx.set('json', {
items,
});

return {
title: $('channel > title').text(),
link: $('channel > link').text(),
description: $('channel > description').text(),
...rss,
item: items,
language: 'en-hk',
icon: 'https://assets.i-scmp.com/static/img/icons/scmp-icon-256x256.png',
logo: 'https://customerservice.scmp.com/img/logo_scmp@2x.png',
image: $('channel > image > url').text(),
};
}
4 changes: 2 additions & 2 deletions lib/routes/scmp/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { load } from 'cheerio';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const renderHTML = (node) => {
Expand Down Expand Up @@ -60,7 +60,7 @@ export const renderHTML = (node) => {
};

export const parseItem = async (item) => {
const { data: response, url } = await got(item.link);
const { _data: response, url } = await ofetch.raw(item.link);

if (new URL(url).hostname !== 'www.scmp.com') {
// e.g., https://multimedia.scmp.com/
Expand Down