9b395f5cc3
Build Chrome Extension / build (push) Waiting to run
Trigger Website Rebuild (Docs Updated) / dispatch (push) Waiting to run
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
53 lines
2.0 KiB
JavaScript
53 lines
2.0 KiB
JavaScript
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
function normalize(value) {
|
|
return typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : '';
|
|
}
|
|
function stripHtml(value) {
|
|
return value.replace(/<[^>]+>/g, '');
|
|
}
|
|
async function searchSinaBlog(keyword, limit) {
|
|
const url = new URL('https://search.sina.com.cn/api/search');
|
|
url.searchParams.set('q', keyword);
|
|
url.searchParams.set('tp', 'mix');
|
|
url.searchParams.set('sort', '0');
|
|
url.searchParams.set('page', '1');
|
|
url.searchParams.set('size', String(Math.max(limit, 10)));
|
|
url.searchParams.set('from', 'search_result');
|
|
const resp = await fetch(url, {
|
|
headers: {
|
|
'User-Agent': 'Mozilla/5.0',
|
|
Accept: 'application/json',
|
|
},
|
|
});
|
|
if (!resp.ok)
|
|
throw new Error(`Sina blog search failed: HTTP ${resp.status}`);
|
|
const data = await resp.json();
|
|
const list = Array.isArray(data?.data?.list) ? data.data.list : [];
|
|
return list
|
|
.filter((item) => normalize(item?.url).includes('blog.sina.com.cn/s/blog_'))
|
|
.slice(0, limit)
|
|
.map((item, index) => ({
|
|
rank: index + 1,
|
|
title: normalize(stripHtml(item?.title || '')),
|
|
author: normalize(item?.media_show || item?.author),
|
|
date: normalize(item?.time || item?.dataTime),
|
|
description: normalize(item?.intro || item?.searchSummary).slice(0, 150),
|
|
url: normalize(item?.url),
|
|
}));
|
|
}
|
|
cli({
|
|
site: 'sinablog',
|
|
name: 'search',
|
|
access: 'read',
|
|
description: '搜索新浪博客文章(通过新浪搜索)',
|
|
domain: 'blog.sina.com.cn',
|
|
strategy: Strategy.PUBLIC,
|
|
browser: false,
|
|
args: [
|
|
{ name: 'keyword', required: true, positional: true, help: '搜索关键词' },
|
|
{ name: 'limit', type: 'int', default: 20, help: '返回的文章数量' },
|
|
],
|
|
columns: ['rank', 'title', 'author', 'date', 'description', 'url'],
|
|
func: async (args) => searchSinaBlog(args.keyword, Math.max(1, Math.min(Number(args.limit) || 20, 50))),
|
|
});
|