@xbrowser/tumblr 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.
Files changed (2) hide show
  1. package/index.ts +77 -0
  2. package/package.json +23 -0
package/index.ts ADDED
@@ -0,0 +1,77 @@
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 tumblr = xcli.createSite({
7
+ name: 'tumblr',
8
+ url: 'https://www.tumblr.com',
9
+ description: 'Tumblr 图片搜索',
10
+ requiresLogin: false,
11
+ });
12
+
13
+ tumblr.command('search-image', {
14
+ description: 'Tumblr 图片搜索 - 搜索 Tumblr 上的图片内容',
15
+ scope: 'browser',
16
+ parameters: z.object({
17
+ query: z.string().describe('搜索关键词'),
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.tumblr.com/search/${encodeURIComponent(params.query)}`;
30
+ await page.goto(url, { waitUntil: 'networkidle', timeout: params.timeout });
31
+ await page.waitForTimeout(6000);
32
+ for (let i = 0; i < 5; i++) {
33
+ await page.evaluate(() => window.scrollBy(0, window.innerHeight));
34
+ await page.waitForTimeout(1200);
35
+ }
36
+
37
+ const results = await page.evaluate((limit: number) => {
38
+ const images: Array<{
39
+ title: string; thumbnailUrl: string; sourceUrl: string;
40
+ width: number; height: number;
41
+ }> = [];
42
+
43
+ let imgs = document.querySelectorAll('img[src*="tumblr"], img[src*="media.tumblr"]');
44
+ if (imgs.length === 0) {
45
+ imgs = document.querySelectorAll('img');
46
+ }
47
+ imgs.forEach((img) => {
48
+ if (images.length >= limit) return;
49
+ const el = img as HTMLImageElement;
50
+ const src = el.src || '';
51
+ if (el.width < 30 || !src.startsWith('http')) return;
52
+ if (src.includes('logo') || src.includes('icon') || src.includes('avatar')) return;
53
+ images.push({
54
+ title: el.alt || '',
55
+ thumbnailUrl: src,
56
+ sourceUrl: el.closest('a')?.getAttribute('href') || '',
57
+ width: el.naturalWidth || el.width,
58
+ height: el.naturalHeight || el.height,
59
+ });
60
+ });
61
+
62
+ return images;
63
+ }, params.limit);
64
+
65
+ return ok({
66
+ query: params.query,
67
+ engine: 'tumblr',
68
+ results: results.map(r => ({ ...r, sourceSite: 'tumblr' })),
69
+ total: results.length,
70
+ timestamp: Date.now(),
71
+ });
72
+ } catch (error) {
73
+ return fail(error instanceof Error ? error.message : '未知错误');
74
+ }
75
+ },
76
+ });
77
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@xbrowser/tumblr",
3
+ "version": "1.0.0",
4
+ "description": "Tumblr SEO 外链 - 博客平台 (DA 86, dofollow)",
5
+ "main": "index.ts",
6
+ "keywords": [
7
+ "xbrowser",
8
+ "xbrowser-plugin",
9
+ "tumblr.com"
10
+ ],
11
+ "author": "dyyz1993",
12
+ "license": "MIT",
13
+ "xbrowser": {
14
+ "site": "https://www.tumblr.com",
15
+ "requiresLogin": true,
16
+ "commands": [
17
+ "login",
18
+ "publish",
19
+ "update-profile",
20
+ "reblog"
21
+ ]
22
+ }
23
+ }