@xbrowser/unsplash 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 +53 -0
- package/package.json +20 -0
package/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
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: 'unsplash', url: 'https://unsplash.com',
|
|
8
|
+
description: 'Unsplash - Free high-resolution photos', requiresLogin: false,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
site.command('search-image', {
|
|
12
|
+
description: 'Search Unsplash photos with metadata',
|
|
13
|
+
scope: 'browser',
|
|
14
|
+
parameters: z.object({
|
|
15
|
+
query: z.string(), limit: z.number().optional().default(20),
|
|
16
|
+
color: z.string().optional(), 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
|
+
let url = `https://unsplash.com/s/photos/${encodeURIComponent(params.query)}`;
|
|
24
|
+
if (params.color) url += `?color=${params.color}`;
|
|
25
|
+
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: params.timeout });
|
|
26
|
+
await page.waitForTimeout(3000);
|
|
27
|
+
for (let i = 0; i < Math.ceil(params.limit / 10); i++) {
|
|
28
|
+
await page.evaluate(() => window.scrollBy(0, 1000));
|
|
29
|
+
await page.waitForTimeout(800);
|
|
30
|
+
}
|
|
31
|
+
const results = await page.evaluate((limit: number) => {
|
|
32
|
+
const images: any[] = [];
|
|
33
|
+
const items = document.querySelectorAll('figure img, [data-testid="photo-grid-item"] img, .YEWhR img');
|
|
34
|
+
items.forEach((item, idx) => {
|
|
35
|
+
if (idx >= limit) return;
|
|
36
|
+
const img = item as HTMLImageElement;
|
|
37
|
+
const src = img.src || img.getAttribute('srcset')?.split(' ')[0] || '';
|
|
38
|
+
const originalUrl = src.replace(/w=\d+/, 'w=2400') || src;
|
|
39
|
+
images.push({
|
|
40
|
+
title: img.alt || '', thumbnailUrl: src,
|
|
41
|
+
sourceUrl: img.closest('a')?.href || '', originalUrl,
|
|
42
|
+
width: parseInt(img.getAttribute('width') || '0', 10),
|
|
43
|
+
height: parseInt(img.getAttribute('height') || '0', 10),
|
|
44
|
+
format: 'jpg', sourceSite: 'unsplash',
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
return images.slice(0, limit);
|
|
48
|
+
}, params.limit);
|
|
49
|
+
return ok({ query: params.query, engine: 'unsplash', results, total: results.length, timestamp: Date.now() }, [`Unsplash "${params.query}",共 ${results.length} 张`]);
|
|
50
|
+
} catch (error) { return { data: null, message: error instanceof Error ? error.message : '未知错误' }; }
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xbrowser/unsplash",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Unsplash - Free high-resolution photos",
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"xbrowser",
|
|
8
|
+
"xbrowser-plugin",
|
|
9
|
+
"unsplash"
|
|
10
|
+
],
|
|
11
|
+
"author": "dyyz1993",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"xbrowser": {
|
|
14
|
+
"site": "https://unsplash.com",
|
|
15
|
+
"requiresLogin": false,
|
|
16
|
+
"commands": [
|
|
17
|
+
"search-image"
|
|
18
|
+
]
|
|
19
|
+
}
|
|
20
|
+
}
|