From 69a35d2f563e9c9e5e4400c1c635decba3c7806c Mon Sep 17 00:00:00 2001 From: quiniapiezoelectricity <73748843+quiniapiezoelectricity@users.noreply.github.com> Date: Fri, 18 Oct 2024 06:02:46 +0100 Subject: [PATCH] feat(route): add straits times route (#17163) * feat(route): add straits times route * Update lib/routes/straitstimes/index.ts --------- --- lib/routes/straitstimes/index.ts | 135 ++++++++++++++++++ lib/routes/straitstimes/namespace.ts | 7 + .../straitstimes/templates/description.art | 27 ++++ 3 files changed, 169 insertions(+) create mode 100644 lib/routes/straitstimes/index.ts create mode 100644 lib/routes/straitstimes/namespace.ts create mode 100644 lib/routes/straitstimes/templates/description.art diff --git a/lib/routes/straitstimes/index.ts b/lib/routes/straitstimes/index.ts new file mode 100644 index 000000000..f09952319 --- /dev/null +++ b/lib/routes/straitstimes/index.ts @@ -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, + }; +} diff --git a/lib/routes/straitstimes/namespace.ts b/lib/routes/straitstimes/namespace.ts new file mode 100644 index 000000000..c23481dd3 --- /dev/null +++ b/lib/routes/straitstimes/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'The Strait Times', + url: 'straitstimes.com', + description: '', +}; diff --git a/lib/routes/straitstimes/templates/description.art b/lib/routes/straitstimes/templates/description.art new file mode 100644 index 000000000..697ad38bc --- /dev/null +++ b/lib/routes/straitstimes/templates/description.art @@ -0,0 +1,27 @@ +{{ if images }} + {{ each images }} + {{if $value.url }} +
+ + {{ if $value.caption }} +
{{ $value.caption }}
+ {{ /if }} +
+ {{ /if }} + {{ /each }} +{{ /if }} +{{ if article }} + {{@ article }} +{{ /if }} \ No newline at end of file