@xbrowser/shutterstock 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 +79 -0
- package/package.json +20 -0
package/index.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
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 shutterstock = xcli.createSite({
|
|
7
|
+
name: 'shutterstock',
|
|
8
|
+
url: 'https://www.shutterstock.com',
|
|
9
|
+
description: 'Shutterstock - Stock Photos, Images & Vectors',
|
|
10
|
+
requiresLogin: false,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
shutterstock.command('search-image', {
|
|
14
|
+
description: 'Shutterstock image search',
|
|
15
|
+
scope: 'browser',
|
|
16
|
+
parameters: z.object({
|
|
17
|
+
query: z.string().describe('Search query'),
|
|
18
|
+
limit: z.number().optional().default(10),
|
|
19
|
+
page: z.any().optional(),
|
|
20
|
+
timeout: z.number().optional().default(20000),
|
|
21
|
+
}),
|
|
22
|
+
result: z.any(),
|
|
23
|
+
handler: async (params, ctx) => {
|
|
24
|
+
const page = (params.page as import('playwright').Page)
|
|
25
|
+
|| (ctx as Record<string, unknown>).page as import('playwright').Page;
|
|
26
|
+
if (!page) throw new Error('需要浏览器页面');
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const url = `https://www.shutterstock.com/search/${encodeURIComponent(params.query)}`;
|
|
30
|
+
|
|
31
|
+
await page.goto(url, { waitUntil: 'networkidle', timeout: params.timeout });
|
|
32
|
+
await page.waitForTimeout(6000);
|
|
33
|
+
|
|
34
|
+
for (let i = 0; i < 6; i++) {
|
|
35
|
+
await page.evaluate(() => window.scrollBy(0, window.innerHeight));
|
|
36
|
+
await page.waitForTimeout(1500);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const results = await page.evaluate((limit: number) => {
|
|
40
|
+
const images: Array<{
|
|
41
|
+
title: string; thumbnailUrl: string; sourceUrl: string;
|
|
42
|
+
width: number; height: number;
|
|
43
|
+
}> = [];
|
|
44
|
+
|
|
45
|
+
let items = document.querySelectorAll('img[src*="shutterstock"], .search-results-grid img, img[src*="shutter"]');
|
|
46
|
+
if (items.length === 0) {
|
|
47
|
+
items = document.querySelectorAll('img');
|
|
48
|
+
}
|
|
49
|
+
items.forEach((img) => {
|
|
50
|
+
if (images.length >= limit) return;
|
|
51
|
+
const el = img as HTMLImageElement;
|
|
52
|
+
const src = el.src || '';
|
|
53
|
+
if (el.width < 30 || !src.startsWith('http')) return;
|
|
54
|
+
if (src.includes('logo') || src.includes('icon') || src.includes('avatar')) return;
|
|
55
|
+
|
|
56
|
+
const container = el.closest('a, [data-testid], .search-results-grid-item');
|
|
57
|
+
images.push({
|
|
58
|
+
title: el.alt || '',
|
|
59
|
+
thumbnailUrl: src,
|
|
60
|
+
sourceUrl: (container as HTMLAnchorElement)?.href || '',
|
|
61
|
+
width: el.naturalWidth || el.width || 0,
|
|
62
|
+
height: el.naturalHeight || el.height || 0,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return images.slice(0, limit);
|
|
67
|
+
}, params.limit);
|
|
68
|
+
|
|
69
|
+
return ok({
|
|
70
|
+
query: params.query,
|
|
71
|
+
engine: 'shutterstock',
|
|
72
|
+
results: results.map(r => ({ ...r, sourceSite: 'shutterstock' })),
|
|
73
|
+
}, [`Shutterstock "${params.query}",共 ${results.length} 张`]);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
return fail(error instanceof Error ? error.message : '未知错误');
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xbrowser/shutterstock",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Shutterstock - Stock Photos, Images & Vectors",
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"xbrowser",
|
|
8
|
+
"xbrowser-plugin",
|
|
9
|
+
"shutterstock"
|
|
10
|
+
],
|
|
11
|
+
"author": "dyyz1993",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"xbrowser": {
|
|
14
|
+
"site": "https://www.shutterstock.com",
|
|
15
|
+
"requiresLogin": false,
|
|
16
|
+
"commands": [
|
|
17
|
+
"search-image"
|
|
18
|
+
]
|
|
19
|
+
}
|
|
20
|
+
}
|