forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add route(chart) for openrice.comOpenrice (DIYgod#17431)
* 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
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |