From 1d637532d45e73f92ec9482fc098f91e3cf43740 Mon Sep 17 00:00:00 2001 From: After9 Date: Fri, 25 Oct 2024 12:03:24 +0800 Subject: [PATCH] feat: add route for openrice.com (#17276) * feat: add route for openrice.com * fix: Optimize the code according to the recommendations of review * fix: Remove the initial assignment of variable urlPath --- lib/routes/openrice/namespace.ts | 8 ++ lib/routes/openrice/offers.ts | 80 +++++++++++++++++++ lib/routes/openrice/promos.ts | 74 +++++++++++++++++ lib/routes/openrice/templates/description.art | 4 + 4 files changed, 166 insertions(+) create mode 100644 lib/routes/openrice/namespace.ts create mode 100644 lib/routes/openrice/offers.ts create mode 100644 lib/routes/openrice/promos.ts create mode 100644 lib/routes/openrice/templates/description.art diff --git a/lib/routes/openrice/namespace.ts b/lib/routes/openrice/namespace.ts new file mode 100644 index 000000000..f84c62ca7 --- /dev/null +++ b/lib/routes/openrice/namespace.ts @@ -0,0 +1,8 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Openrice開飯喇', + url: 'www.openrice.com', + categories: ['shopping'], + description: '美食網站Openrice相关資訊', +}; diff --git a/lib/routes/openrice/offers.ts b/lib/routes/openrice/offers.ts new file mode 100644 index 000000000..abe00683a --- /dev/null +++ b/lib/routes/openrice/offers.ts @@ -0,0 +1,80 @@ +import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { art } from '@/utils/render'; +import path from 'node:path'; +import { getCurrentPath } from '@/utils/helpers'; +const __dirname = getCurrentPath(import.meta.url); +const baseUrl = 'https://www.openrice.com'; + +export const route: Route = { + path: '/:lang/hongkong/offers', + maintainers: ['after9'], + handler, + categories: ['shopping'], + example: '/openrice/zh/hongkong/offers', + parameters: { lang: '语言,缺省为 zh' }, + name: '香港餐廳精選優惠券', + description: ` + | 简体 | 繁體 | EN | + | ----- | ------ | ----- | + | zh-cn | zh | en | + `, +}; + +async function handler(ctx) { + const lang = ctx.req.param('lang') ?? 'zh'; + + const apiPath = '/api/offers'; + let urlPath: string; + switch (lang) { + case 'zh-cn': + urlPath = '/zh-cn/hongkong/offers'; + break; + case 'en': + urlPath = '/en/hongkong/offers'; + break; + case 'zh': + default: + urlPath = '/zh/hongkong/offers'; + break; + } + const response = await ofetch(baseUrl + apiPath, { + headers: { + accept: 'application/json', + }, + query: { + uiLang: lang, + uiCity: 'hongkong', + page: 1, + sortBy: 'PublishTime', + couponTypeId: 1, + }, + }); + const pageInfo = response.pageInfo; + const highlightedOffers = response.highlightedOffers; + const normalOffers = response.searchResult.paginationResult.results; + const data = [...highlightedOffers, ...normalOffers]; + + const resultList = data.map((item) => { + const title = item.title ?? ''; + const link = baseUrl + item.urlUI; + const coverImg = item.doorPhotoUI.urls.full ?? ''; + const descriptionText = item.couponType === 0 ? item.poiNameUI : `${item.desc} (${item.startTimeUI} - ${item.expireTimeUI}) [${item.multiplePoiDistrictName}]`; + const description = art(path.join(__dirname, 'templates/description.art'), { + description: descriptionText, + image: coverImg, + }); + return { + title, + description, + link, + }; + }); + + return { + title: pageInfo.seoInfo.title ?? 'OpenRice Hong Kong Offers', + link: baseUrl + urlPath, + description: pageInfo.seoInfo.metadataDictionary.name.find((item: { key: string; value: string }) => item.key === 'description')?.value ?? 'OpenRice Hong Kong Offers', + item: resultList, + }; +} diff --git a/lib/routes/openrice/promos.ts b/lib/routes/openrice/promos.ts new file mode 100644 index 000000000..abd42d63d --- /dev/null +++ b/lib/routes/openrice/promos.ts @@ -0,0 +1,74 @@ +import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; +import { art } from '@/utils/render'; +import path from 'node:path'; +import { getCurrentPath } from '@/utils/helpers'; +const __dirname = getCurrentPath(import.meta.url); +const baseUrl = 'https://www.openrice.com'; + +export const route: Route = { + path: '/:lang/hongkong/promos', + maintainers: ['after9'], + handler, + categories: ['shopping'], + example: '/openrice/zh/hongkong/promos', + parameters: { lang: '语言,缺省为 zh' }, + name: '香港餐厅滋讯', + description: ` + | 简体 | 繁體 | EN | + | ----- | ------ | ----- | + | zh-cn | zh | en | + `, +}; + +async function handler(ctx) { + const lang = ctx.req.param('lang') ?? 'zh'; + + let urlPath; + switch (lang) { + case 'zh-cn': + urlPath = '/zh-cn/hongkong/promos'; + break; + case 'en': + urlPath = '/en/hongkong/promos'; + break; + case 'zh': + default: + urlPath = '/zh/hongkong/promos'; + break; + } + const response = await ofetch(baseUrl + urlPath, {}); + const $ = load(response); + + const title = $('title').text() ?? "Openrice - What's Hot"; + const description = $('meta[name="description"]').attr('content') ?? "What's Hot from Openrice"; + + const data = $('.article-listing-content-cell-wrapper'); + const resultList = data.toArray().map((item) => { + const $item = $(item); + const title = $item.find('.title-name').text() ?? ''; + const link = $item.find('a.sr1-listing-content-cell').attr('href') ?? ''; + const coverImg = + $item + .find('.cover-photo') + .attr('style') + ?.match(/url\(['"]?(.*?)['"]?\)/)?.[1] ?? null; + const description = art(path.join(__dirname, 'templates/description.art'), { + description: $item.find('.article-details .desc').text() ?? '', + image: coverImg, + }); + return { + title, + description, + link, + }; + }); + + return { + title, + link: baseUrl + urlPath, + description, + item: resultList, + }; +} diff --git a/lib/routes/openrice/templates/description.art b/lib/routes/openrice/templates/description.art new file mode 100644 index 000000000..9aa75c6f8 --- /dev/null +++ b/lib/routes/openrice/templates/description.art @@ -0,0 +1,4 @@ +{{ description }} +{{ if image }} + +{{ /if }} \ No newline at end of file