Skip to content

Commit

Permalink
feat(route): add route /hottoys (#16238) (#16457)
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
jw0903 authored Aug 18, 2024
1 parent 77acba5 commit 3656d68
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lib/routes/hottoys/index.ts
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,
};
}
6 changes: 6 additions & 0 deletions lib/routes/hottoys/namespace.ts
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',
};

0 comments on commit 3656d68

Please sign in to comment.