@xbrowser/pinterest 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +59 -0
- package/package.json +20 -0
package/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { XCLIAPI } from '@dyyz1993/xcli-core';
|
|
3
|
+
import { ok, fail } from '@dyyz1993/xcli-core';
|
|
4
|
+
|
|
5
|
+
export default function (xcli: XCLIAPI): void {
|
|
6
|
+
const site = xcli.createSite({
|
|
7
|
+
name: 'pinterest', url: 'https://www.pinterest.com',
|
|
8
|
+
description: 'Pinterest - Image sharing platform', requiresLogin: true,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
site.command('search-image', {
|
|
12
|
+
description: 'Pinterest image search (requires login via --cdp)',
|
|
13
|
+
scope: 'browser',
|
|
14
|
+
parameters: z.object({
|
|
15
|
+
query: z.string(), limit: z.number().optional().default(20),
|
|
16
|
+
page: z.any().optional(), timeout: z.number().optional().default(20000),
|
|
17
|
+
}),
|
|
18
|
+
result: z.any(),
|
|
19
|
+
handler: async (params, ctx) => {
|
|
20
|
+
const page = (params.page as import('playwright').Page) || (ctx as Record<string, unknown>).page as import('playwright').Page;
|
|
21
|
+
if (!page) throw new Error('需要浏览器页面');
|
|
22
|
+
try {
|
|
23
|
+
await page.goto(`https://www.pinterest.com/search/pins/?q=${encodeURIComponent(params.query)}`, { waitUntil: 'networkidle', timeout: params.timeout });
|
|
24
|
+
await page.waitForTimeout(3000);
|
|
25
|
+
|
|
26
|
+
const currentUrl = page.url();
|
|
27
|
+
if (currentUrl.includes('/login') || currentUrl.includes('/signup')) {
|
|
28
|
+
return fail('Pinterest 需要登录。请使用 --cdp http://localhost:9221 连接带登录态的浏览器');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < 5; i++) {
|
|
32
|
+
await page.evaluate(() => window.scrollBy(0, 1000));
|
|
33
|
+
await page.waitForTimeout(1000);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const results = await page.evaluate((limit: number) => {
|
|
37
|
+
const images: any[] = [];
|
|
38
|
+
const items = document.querySelectorAll('[data-test-id="pin"], [data-test-pin-id], .GrowthUnauthPin_image');
|
|
39
|
+
items.forEach((item, idx) => {
|
|
40
|
+
if (idx >= limit) return;
|
|
41
|
+
const el = item as HTMLElement;
|
|
42
|
+
const img = el.querySelector('img') as HTMLImageElement;
|
|
43
|
+
if (!img) return;
|
|
44
|
+
const linkEl = el.querySelector('a[href*="/pin/"]') || el.closest('a');
|
|
45
|
+
images.push({
|
|
46
|
+
title: img.alt || '', thumbnailUrl: img.src,
|
|
47
|
+
sourceUrl: linkEl?.href || '', originalUrl: img.src,
|
|
48
|
+
width: img.naturalWidth || 0, height: img.naturalHeight || 0,
|
|
49
|
+
format: 'jpg', sourceSite: 'pinterest',
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
return images.slice(0, limit);
|
|
53
|
+
}, params.limit);
|
|
54
|
+
|
|
55
|
+
return ok({ query: params.query, engine: 'pinterest', results, total: results.length, timestamp: Date.now() }, [`Pinterest "${params.query}",共 ${results.length} 张`]);
|
|
56
|
+
} catch (error) { return fail(error instanceof Error ? error.message : '未知错误'); }
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xbrowser/pinterest",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Pinterest - Image sharing platform",
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"xbrowser",
|
|
8
|
+
"xbrowser-plugin",
|
|
9
|
+
"pinterest"
|
|
10
|
+
],
|
|
11
|
+
"author": "dyyz1993",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"xbrowser": {
|
|
14
|
+
"site": "https://www.pinterest.com",
|
|
15
|
+
"requiresLogin": true,
|
|
16
|
+
"commands": [
|
|
17
|
+
"search-image"
|
|
18
|
+
]
|
|
19
|
+
}
|
|
20
|
+
}
|