-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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 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
- Loading branch information
Showing
4 changed files
with
166 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,8 @@ | ||
import type { Namespace } from '@/types'; | ||
|
||
export const namespace: Namespace = { | ||
name: 'Openrice開飯喇', | ||
url: 'www.openrice.com', | ||
categories: ['shopping'], | ||
description: '美食網站Openrice相关資訊', | ||
}; |
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,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, | ||
}; | ||
} |
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,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, | ||
}; | ||
} |
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,4 @@ | ||
{{ description }} | ||
{{ if image }} | ||
<img src="{{ image }}"> | ||
{{ /if }} |