Skip to content

Commit

Permalink
feat: add route for openrice.com (#17276)
Browse files Browse the repository at this point in the history
* feat: add route for openrice.com

* fix: Optimize the code according to the recommendations of review

* fix: Remove the initial assignment of variable urlPath
  • Loading branch information
after9 authored Oct 25, 2024
1 parent 1600de0 commit 1d63753
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/routes/openrice/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'Openrice開飯喇',
url: 'www.openrice.com',
categories: ['shopping'],
description: '美食網站Openrice相关資訊',
};
80 changes: 80 additions & 0 deletions lib/routes/openrice/offers.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
74 changes: 74 additions & 0 deletions lib/routes/openrice/promos.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
4 changes: 4 additions & 0 deletions lib/routes/openrice/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ description }}
{{ if image }}
<img src="{{ image }}">
{{ /if }}

0 comments on commit 1d63753

Please sign in to comment.