Skip to content

Commit

Permalink
feat: add route(chart) for openrice.comOpenrice (DIYgod#17431)
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

* feat: add route(chart) for openrice.com

* Update lib/routes/openrice/namespace.ts

update lang value following the type Namespace

Co-authored-by: Tony <[email protected]>

* Update lib/routes/openrice/chart.ts

Co-authored-by: Tony <[email protected]>

* Update lib/routes/openrice/chart.ts

Co-authored-by: Tony <[email protected]>

* remove blank option in ofetch

Co-authored-by: Tony <[email protected]>

---------
  • Loading branch information
after9 authored Nov 4, 2024
1 parent 125124c commit 9783660
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
68 changes: 68 additions & 0 deletions lib/routes/openrice/chart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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/explore/chart/:category',
maintainers: ['after9'],
handler,
categories: ['shopping'],
example: '/openrice/zh/hongkong/explore/chart/most-bookmarked',
parameters: { lang: '语言,缺省为 zh', category: '类别,缺省为 most-bookmarked' },
name: '香港餐廳排行榜',
description: `
| 简体 | 繁體 | EN |
| ----- | ------ | ----- |
| zh-cn | zh | en |
| 最多收藏 | 每周最高评分 | 最高浏览 | 最佳甜品餐厅 |
| ----- | ------ | ----- | ----- |
| most-bookmarked | best-rating | most-popular | best-dessert |
`,
};

async function handler(ctx) {
const lang = ctx.req.param('lang') ?? 'zh';
const category = ctx.req.param('category') ?? 'most-bookmarked';

const urlPath: string = `/${lang}/hongkong/explore/chart/${category}`;
const response = await ofetch(baseUrl + urlPath);
const $ = load(response);

const title = $('title').text() ?? 'Hong Kong Restaurant Chart';
const description = $('title').text() ?? 'Hong Kong Restaurant Chart';

const data = $('.poi-chart-main-grid-item-desktop-wrapper');
const resultList = data.toArray().map((item) => {
const $item = $(item);
const rankClass = $item.find('.rank-icon').attr('class');
const rankNumber = rankClass?.match(/rank-(\d+)/)?.[1] ?? '';
const desTags = $item.find('.pcmgidtr-left-section-poi-info-details .pcmgidtrls-poi-info-details-text');
const desTagsArray: string[] = desTags.toArray().map((tag) => $(tag).text());
const title = $item.find('.pcmgidtr-left-section-poi-info-name .link').text() ?? '';
const link = $item.find('.pcmgidtr-left-section-poi-info-name .link').attr('href') ?? '';
const coverImg = $item.find('.pcmgidtr-left-section-door-photo img').attr('src') ?? null;
const description = art(path.join(__dirname, 'templates/chart.art'), {
description: desTagsArray ?? [],
rankNumber,
image: coverImg,
});
return {
title,
description,
link,
};
});

return {
title,
link: baseUrl + urlPath,
description,
item: resultList,
};
}
9 changes: 9 additions & 0 deletions lib/routes/openrice/templates/chart.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h3>Rank: {{ rankNumber }} / {{ title }}</h3>
<p>
{{ each description }}
{{ $value }}
{{ /each }}
</p>
{{ if image }}
<img src="{{ image }}">
{{ /if }}

0 comments on commit 9783660

Please sign in to comment.