From d5721d4710ea2c3cbb9e2730d77c16ccd50a0949 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sat, 4 Oct 2025 18:30:24 +0800 Subject: [PATCH] feat(route): add The Indian Express Section (#20224) * feat(route): add The Indian Express Section * fix typo * fix typo --- lib/routes/indianexpress/namespace.ts | 10 +++ lib/routes/indianexpress/section.ts | 124 ++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 lib/routes/indianexpress/namespace.ts create mode 100644 lib/routes/indianexpress/section.ts diff --git a/lib/routes/indianexpress/namespace.ts b/lib/routes/indianexpress/namespace.ts new file mode 100644 index 000000000..e5430a859 --- /dev/null +++ b/lib/routes/indianexpress/namespace.ts @@ -0,0 +1,10 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'The Indian Express', + url: 'indianexpress.com', + categories: ['new-media'], + description: + 'Get latest and Breaking News on Politics, Business, Lifestyle, Entertainment and Sports along with News updates from around the world. Also, find English News, live coverage on Bollywood, Cricket, Technology, Celebrities and more on indianexpress.com', + lang: 'en', +}; diff --git a/lib/routes/indianexpress/section.ts b/lib/routes/indianexpress/section.ts new file mode 100644 index 000000000..81ca1edf5 --- /dev/null +++ b/lib/routes/indianexpress/section.ts @@ -0,0 +1,124 @@ +import { type Data, type DataItem, type Route, ViewType } from '@/types'; + +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; + +import { type CheerioAPI, load } from 'cheerio'; +import { type Context } from 'hono'; + +export const handler = async (ctx: Context): Promise => { + const { id = 'trending' } = ctx.req.param(); + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '50', 10); + + const apiSlug = 'wp-json/wp/v2'; + const baseUrl: string = 'https://indianexpress.com'; + const apiUrl = new URL(`${apiSlug}/article`, baseUrl).href; + const apiSearchUrl = new URL(`${apiSlug}/ie_section`, baseUrl).href; + + const searchResponse = await ofetch(apiSearchUrl, { + query: { + search: id, + }, + }); + + const sectionObj = searchResponse.find((c) => c.slug === id || c.name === id || id.includes(c.slug)); + const sectionId: number | undefined = sectionObj?.id ?? undefined; + const sectionLink: string | undefined = sectionObj?.link ?? undefined; + + const response = await ofetch(apiUrl, { + query: { + _embed: 'true', + per_page: limit, + ie_section: sectionId, + }, + }); + + const targetUrl: string = sectionLink ?? new URL(`section/${id.endsWith('/') ? id : `${id}/`}`, baseUrl).href; + + const targetResponse = await ofetch(targetUrl); + const $: CheerioAPI = load(targetResponse); + const language = $('html').attr('lang') ?? 'en'; + + let items: DataItem[] = []; + + items = response.slice(0, limit).map((item): DataItem => { + const title: string = item.title?.rendered ?? item.title; + const description: string | undefined = item.content.rendered; + const pubDate: number | string = item.date_gmt; + const linkUrl: string | undefined = item.link; + + const terminologies = item._embedded?.['wp:term']; + + const categories: string[] = terminologies?.flat().map((c) => c.name) ?? []; + const guid: string = item.guid?.rendered ?? item.guid; + const updated: number | string = item.modified_gmt ?? pubDate; + + const processedItem: DataItem = { + title, + description, + pubDate: pubDate ? parseDate(pubDate) : undefined, + link: linkUrl ?? guid, + category: categories, + guid, + id: guid, + content: { + html: description, + text: description, + }, + updated: updated ? parseDate(updated) : undefined, + language, + }; + + return processedItem; + }); + + const author: string | undefined = $('meta[property="og:site_name"]').attr('content') || $('meta[property="og:title"]').attr('content'); + + return { + title: `${author ? `${author} - ` : ''}${sectionObj?.name ?? id}`, + description: $('meta[property="og:description"]').attr('content'), + link: targetUrl, + item: items, + allowEmpty: true, + image: $('meta[property="og:image"]').attr('content'), + author, + language, + feedLink: $('link[type="application/rss+xml"]').attr('href'), + id: $('meta[property="og:url"]').attr('content'), + }; +}; + +export const route: Route = { + path: '/section/:id{.+}?', + name: 'Section', + url: 'indianexpress.com', + maintainers: ['nczitzk'], + handler, + example: '/indianexpress/section/explained', + parameters: { + id: { + description: 'Section ID, `trending` as Trending by default', + }, + }, + description: `:::tip +To subscribe to [Section](https://indianexpress.com/), where the source URL is \`https://indianexpress.com/\`, extract the certain parts from this URL to be used as parameters, resulting in the route as [\`/indianexpress/section/explained\`](https://rsshub.app/indianexpress/section/explained). +::: +`, + categories: ['new-media'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['indianexpress.com/section/:id'], + target: '/section/:id', + }, + ], + view: ViewType.Articles, +};