-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(route): add route /hottoys (#16238) * fix: Fix issues identified in code review
- Loading branch information
Showing
2 changed files
with
69 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,63 @@ | ||
import { Route } from '@/types'; | ||
import { load } from 'cheerio'; | ||
import puppeteer from '@/utils/puppeteer'; | ||
|
||
export const route: Route = { | ||
path: '/', | ||
categories: ['shopping'], | ||
example: '/hottoys', | ||
radar: [ | ||
{ | ||
source: ['hottoys.com.hk/'], | ||
}, | ||
], | ||
name: 'Toys List', | ||
maintainers: ['jw0903'], | ||
handler, | ||
url: 'hottoys.com.hk/', | ||
features: { | ||
requirePuppeteer: true, | ||
}, | ||
}; | ||
|
||
async function handler() { | ||
const baseUrl = 'https://www.hottoys.com.hk'; | ||
|
||
// 导入 puppeteer 工具类并初始化浏览器实例 | ||
const browser = await puppeteer(); | ||
// 打开一个新标签页 | ||
const page = await browser.newPage(); | ||
// 拦截所有请求 | ||
await page.setRequestInterception(true); | ||
|
||
page.on('request', (request) => { | ||
// 在这次例子,我们只允许 HTML 请求 | ||
request.resourceType() === 'document' ? request.continue() : request.abort(); | ||
}); | ||
|
||
await page.goto(baseUrl, { | ||
waitUntil: 'domcontentloaded', | ||
}); | ||
const response = await page.content(); | ||
page.close(); | ||
const $ = load(response); | ||
const items = $('li.productListItem') | ||
.toArray() | ||
.map((item) => { | ||
const dom = $(item); | ||
const a = dom.find('a').first(); | ||
const img = dom.find('img').first(); | ||
return { | ||
title: img.attr('title') ?? 'hottoys', | ||
link: `${baseUrl}/${a.attr('href')}`, | ||
description: `<img src="${baseUrl}${img.attr('src')}" />`, | ||
guid: a.attr('href'), | ||
}; | ||
}); | ||
browser.close(); | ||
return { | ||
title: 'Hot Toys New Products', | ||
link: baseUrl, | ||
item: items, | ||
}; | ||
} |
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,6 @@ | ||
import type { Namespace } from '@/types'; | ||
|
||
export const namespace: Namespace = { | ||
name: 'Hot Toys', | ||
url: 'www.hottoys.com.hk', | ||
}; |