From e50b2c651591c59474bcc64ed1d87f1cecba9b79 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 26 Apr 2024 07:10:46 +0800 Subject: [PATCH] feat(route): add back kpmg (#15373) --- lib/routes-deprecated/kpmg/insights.js | 79 ------------ lib/routes/kpmg/insights.ts | 143 ++++++++++++++++++++++ lib/routes/kpmg/namespace.ts | 10 ++ lib/routes/kpmg/templates/description.art | 11 ++ 4 files changed, 164 insertions(+), 79 deletions(-) delete mode 100644 lib/routes-deprecated/kpmg/insights.js create mode 100644 lib/routes/kpmg/insights.ts create mode 100644 lib/routes/kpmg/namespace.ts create mode 100644 lib/routes/kpmg/templates/description.art diff --git a/lib/routes-deprecated/kpmg/insights.js b/lib/routes-deprecated/kpmg/insights.js deleted file mode 100644 index ed9fb666a94740..00000000000000 --- a/lib/routes-deprecated/kpmg/insights.js +++ /dev/null @@ -1,79 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - const link = 'https://home.kpmg/cn/zh/home/insights.html'; - - const response = await got({ - method: 'post', - url: `https://home.kpmg/esearch/cn-zh`, - headers: { - Referer: link, - }, - json: { - query: '', - filters: { - all: [ - { - kpmg_tab_type: ['Insights'], - }, - ], - }, - result_fields: { - kpmg_title: { - raw: {}, - }, - kpmg_article_date_time: { - raw: {}, - }, - kpmg_url: { - raw: {}, - }, - }, - page: { - size: 20, - current: 1, - }, - sort: { - kpmg_filter_date: 'desc', - }, - }, - }); - - const list = response.data.results.map((item) => ({ - title: item.kpmg_title.raw, - link: item.kpmg_url.raw, - pubDate: item.kpmg_article_date_time.raw, - })); - - const item = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - const response = await got.get(item.link); - const $ = cheerio.load(response.data); - - if ($('a.component-link')) { - const link = $('a.component-link').attr('href'); - const size = $('a.component-link .downloadSize').text(); - $('a.component-link').replaceWith( - `

- - 点击下载 PDF ${size} -

` - ); - } - - item.description = $('.module-touch-columncontrol').html() || $('.display-full-width').html(); - - return item; - }) - ) - ); - - ctx.state.data = { - title: '毕马威洞察', - link, - description: '欢迎浏览毕马威中国网站的知识库。在这里,你可以找到毕马威中国各类定期出版的通讯及各通讯过往的期号。', - item, - }; -}; diff --git a/lib/routes/kpmg/insights.ts b/lib/routes/kpmg/insights.ts new file mode 100644 index 00000000000000..f5a87c46f10f89 --- /dev/null +++ b/lib/routes/kpmg/insights.ts @@ -0,0 +1,143 @@ +import InvalidParameterError from '@/errors/types/invalid-parameter'; +import { Route } from '@/types'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; +import * as cheerio from 'cheerio'; +import { Context } from 'hono'; +import { parseDate } from '@/utils/parse-date'; +import { art } from '@/utils/render'; +import path from 'node:path'; +import { getCurrentPath } from '@/utils/helpers'; +const __dirname = getCurrentPath(import.meta.url); + +const baseUrl = 'https://kpmg.com'; +const payload = { + query: '', + filters: { + all: [{ kpmg_tab_type: ['Insights'] }, { kpmg_article_type: ['Article-General'] }, { kpmg_template_type: ['article-details-template', 'insights-flexible-template', 'editable-flex-template', 'editable-campaign-template'] }], + }, + result_fields: { + kpmg_description: { raw: {} }, + kpmg_banner_flag: { raw: {} }, + kpmg_primary_tag: { raw: {} }, + kpmg_article_date: { raw: {} }, + kpmg_contact_job_ttl: { raw: {} }, + kpmg_title: { raw: {} }, + kpmg_contact_city: { raw: {} }, + kpmg_event_start_time: { raw: {} }, + kpmg_article_date_time: { raw: {} }, + kpmg_tab_type: { raw: {} }, + kpmg_short_desc: { raw: {} }, + kpmg_image_alt: { raw: {} }, + kpmg_url: { raw: {} }, + kpmg_template_type: { raw: {} }, + kpmg_image: { raw: {} }, + kpmg_non_decorative_alt_text: { raw: {} }, + kpmg_article_readtime: { raw: {} }, + kpmg_contact_fn: { raw: {} }, + kpmg_contact_ln: { raw: {} }, + kpmg_event_type: { raw: {} }, + kpmg_contact_country: { raw: {} }, + kpmg_is_rendition_optimized: { raw: {} }, + kpmg_article_primary_format: { raw: {} }, + kpmg_article_type: { raw: {} }, + kpmg_event_startdate: { raw: {} }, + }, + page: { size: 20, current: 1 }, + sort: { kpmg_filter_date: 'desc' }, +}; +const endpoints = { + en: { + title: 'Insights - KPMG ', + link: `${baseUrl}/xx/en/home/insights.html`, + api: `${baseUrl}/esearch/xx-en`, + }, + zh: { + title: '洞察 - 毕马威', + link: `${baseUrl}/cn/zh/home/insights.html`, + api: `${baseUrl}/esearch/cn-zh`, + }, +}; +const render = (data) => art(path.join(__dirname, 'templates/description.art'), data); + +const handler = async (ctx: Context) => { + const { lang = 'en' } = ctx.req.param(); + const endpoint = endpoints[lang]; + if (!endpoint) { + throw new InvalidParameterError('Invalid language'); + } + const link = endpoint.link; + + const response = await ofetch(endpoint.api, { + method: 'POST', + body: payload, + }); + + const list = response.results.map((item) => ({ + title: item.kpmg_title.raw, + description: item.kpmg_description.raw, + link: item.kpmg_url.raw, + pubDate: parseDate(item.kpmg_article_date_time.raw), + image: item.kpmg_image.raw, + imageAlt: item.kpmg_image_alt?.raw, + })); + + const item = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const response = await ofetch(item.link); + const $ = cheerio.load(response); + + const content = $('.bodytext-data') + .toArray() + .map((item) => { + const element = $(item); + element.find('.hidden-xs, .sr-only').remove(); + return element.parent().html(); + }) + .join(''); + + const pdfDetails = $('.pdfdetails').parent().parent().parent(); + pdfDetails.find('.hidden-xs, .sr-only').remove(); + + item.description = render({ + image: item.image, + alt: item.imageAlt, + content, + pdf: pdfDetails.prop('outerHTML'), + }); + + return item; + }) + ) + ); + + return { + title: 'KPMG Insights', + link, + item, + }; +}; + +export const route: Route = { + path: '/insights/:lang?', + example: '/kpmg/insights', + parameters: { lang: 'Language, either `en` or `zh`' }, + radar: [ + { + source: ['kpmg.com/xx/en/home/insights.html'], + target: '/insights/en', + }, + { + source: ['kpmg.com/cn/zh/home/insights.html'], + target: '/insights/zh', + }, + ], + name: 'Insights', + maintainers: ['LogicJake'], + handler, + url: 'kpmg.com/xx/en/home/insights.html', + zh: { + name: '洞察', + }, +}; diff --git a/lib/routes/kpmg/namespace.ts b/lib/routes/kpmg/namespace.ts new file mode 100644 index 00000000000000..04ff3866e0a9d3 --- /dev/null +++ b/lib/routes/kpmg/namespace.ts @@ -0,0 +1,10 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'KPMG', + url: 'kpmg.com', + categories: ['other'], + zh: { + name: '毕马威', + }, +}; diff --git a/lib/routes/kpmg/templates/description.art b/lib/routes/kpmg/templates/description.art new file mode 100644 index 00000000000000..78ed654a1a406b --- /dev/null +++ b/lib/routes/kpmg/templates/description.art @@ -0,0 +1,11 @@ +{{ if image }} + +{{ /if }} + +{{ if content }} +
{{@ content }} +{{ /if }} + +{{ if pdf }} +
{{@ pdf }} +{{ /if }}