feat(route): add straits times route (#17163)
* feat(route): add straits times route * Update lib/routes/straitstimes/index.ts ---------
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import { Route } from '@/types';
|
||||
import cache from '@/utils/cache';
|
||||
import got from '@/utils/got';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { getCurrentPath } from '@/utils/helpers';
|
||||
import path from 'node:path';
|
||||
import { art } from '@/utils/render';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/:category?/:section?',
|
||||
categories: ['traditional-media'],
|
||||
example: '/straitstimes/singapore',
|
||||
parameters: {
|
||||
category: 'Category, see below for more information',
|
||||
section: 'Section, see below for more information',
|
||||
},
|
||||
features: {
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
requireConfig: false,
|
||||
},
|
||||
name: 'News',
|
||||
maintainers: ['quiniapiezoelectricity'],
|
||||
handler,
|
||||
description: `
|
||||
| Category | \`:category\` |
|
||||
| ---------------------- | --------------------------- |
|
||||
| Singapore | \`singapore\` |
|
||||
| Asia | \`asia\` |
|
||||
| World | \`world\` |
|
||||
| Opinion | \`opinion\` |
|
||||
| Life | \`life\` |
|
||||
| Business | \`business\` |
|
||||
| Jobs | \`jobs\` |
|
||||
| Parenting & Education | \`parenting-and-education\` |
|
||||
| Food | \`food\` |
|
||||
| Tech | \`tech\` |
|
||||
| Sport | \`sport\` |
|
||||
| Podcasts | \`podcasts\` |,
|
||||
|
||||
| Section | \`:section\` |
|
||||
| ---------------------- | --------------------------- |
|
||||
| Top Stories | \`top-stories\` |
|
||||
| Latest | \`latest\` |`,
|
||||
radar: [
|
||||
{
|
||||
source: ['www.straitstimes.com/:category'],
|
||||
target: '/:category',
|
||||
},
|
||||
{
|
||||
source: ['www.straitstimes.com'],
|
||||
target: '/',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async function handler(ctx) {
|
||||
const category = ctx.req.param('category') ? ctx.req.param('category').toLowerCase() : 'singapore';
|
||||
const section = ctx.req.param('section') ? ctx.req.param('section').toLowerCase() : undefined;
|
||||
const apiKey = 'T9XUJM9rAZoLOd2CAx2wCBSTrm3xoyPw';
|
||||
const platform = 'iosflex';
|
||||
const __dirname = getCurrentPath(import.meta.url);
|
||||
let feed;
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: `https://newsapi.sphdigital.com/v2/feed/section/st?page=1&platform=${platform}§ion=${category}/star&subscribed=false&version=4.0`,
|
||||
headers: {
|
||||
'x-api-key': apiKey,
|
||||
},
|
||||
});
|
||||
feed = response.data.data;
|
||||
const sections = new Set(feed.filter((item) => item.items[0].itemType === 'SectionFooter').map((items) => items.items[0].sectionFooterData.link.id));
|
||||
let slug = section && sections.has(`${category}-${section}-more/star`) ? `${category}-${section}-more/star` : undefined;
|
||||
if (section === undefined) {
|
||||
for (const section of ['latest', 'top-picks', 'top-stories']) {
|
||||
if (sections.has(`${category}-${section}-more/star`)) {
|
||||
slug = `${category}-${section}-more/star`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (slug) {
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: `https://newsapi.sphdigital.com/v2/feed/section/st?page=1&platform=${platform}§ion=${slug}&subscribed=false&version=4.0`,
|
||||
headers: {
|
||||
'x-api-key': apiKey,
|
||||
},
|
||||
});
|
||||
feed = response.data.data;
|
||||
}
|
||||
const articles = feed.filter((item) => item.items[0].itemType === 'Article');
|
||||
const items = await Promise.all(
|
||||
articles.map((item) =>
|
||||
cache.tryGet(item.items[0].articleData.url, async () => {
|
||||
const article = item.items[0].articleData;
|
||||
if (article.authors) {
|
||||
item.author = article.authors.map((author) => author.name).join(', ');
|
||||
}
|
||||
if (article.keywords) {
|
||||
item.category = article.keywords.map((keyword) => keyword.name);
|
||||
}
|
||||
item.title = article.headline;
|
||||
item.pubDate = parseDate(article.publicationTime, 'X');
|
||||
item.updated = parseDate(article.updatedTime, 'X');
|
||||
item.link = article.url;
|
||||
let content = article.teaser;
|
||||
if (article.documentId) {
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: `https://newsapi.sphdigital.com/v2/feed/article/st/${article.documentId}?platform=${platform}&version=4.0`,
|
||||
headers: {
|
||||
'x-api-key': apiKey,
|
||||
},
|
||||
});
|
||||
content = response.data.data.body;
|
||||
}
|
||||
item.description = art(path.join(__dirname, 'templates/description.art'), {
|
||||
images: article.images ?? [],
|
||||
article: content,
|
||||
});
|
||||
return item;
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
return {
|
||||
title: `The Strait Times - ${category.replaceAll('-', ' ').toUpperCase()}`,
|
||||
link: `https://www.straitstimes.com/${category.toLowerCase()}`,
|
||||
item: items,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: 'The Strait Times',
|
||||
url: 'straitstimes.com',
|
||||
description: '',
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
{{ if images }}
|
||||
{{ each images }}
|
||||
{{if $value.url }}
|
||||
<figure>
|
||||
<img src=
|
||||
{{ if $value.large }}
|
||||
"{{ $value.large }}"
|
||||
{{ else }}
|
||||
"{{ $value.url }}"
|
||||
{{ /if }}
|
||||
{{ if $value.height }}
|
||||
height="{{ $value.height }}"
|
||||
{{ /if }}
|
||||
{{ if $value.width }}
|
||||
width="{{ $value.width }}"
|
||||
{{ /if }}
|
||||
>
|
||||
{{ if $value.caption }}
|
||||
<figcaption>{{ $value.caption }}</figcaption>
|
||||
{{ /if }}
|
||||
</figure>
|
||||
{{ /if }}
|
||||
{{ /each }}
|
||||
{{ /if }}
|
||||
{{ if article }}
|
||||
{{@ article }}
|
||||
{{ /if }}
|
||||
Reference in New Issue
Block a user