Files
jackwener__opencli/clis/substack/search.ts
jakevin 12176de1a5 refactor: simplify core modules (#784)
* refactor: simplify core modules — remove root shims, consolidate error classification, streamline cascade/interceptor, clean up synthesize

1. Remove root-level shim files (errors.ts, logger.ts, registry.ts, types.ts, utils.ts, launcher.ts) — update all ~840 adapter imports to reference src/ directly
2. Consolidate interceptor: reuse shared DISGUISE_FN in tap interceptor instead of reimplementing
3. Unify error classification: single ClassifiedError type with icon/exitCode/hint lookup table, eliminating duplicated pattern matching between resolveExitCode and renderError
4. Simplify cascade probe: replace repetitive switch cases with PROBE_OPTIONS lookup map
12. Clean up synthesize.ts: remove deprecated snake_case field aliases (recommended_args, recommended_columns, recommendedColumnsLegacy) and unnecessary constant aliases

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* docs: update import paths in contributor docs and skill templates

Update all documentation and skill files to reference src/ directly,
matching the shim removal in the previous commit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: update new Gemini adapter imports to use src/ paths

Fix imports in newly added deep-research adapter files that were
still referencing the deleted root shim files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: update xiaohongshu tests for /search_result/ URL change

Tests now expect /search_result/<id> for bare note IDs (matching
the note-helpers.ts change from PR #774) and updated empty-shell
hint assertion.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: update LessWrong and hupu adapter imports to use src/ paths

Fix imports in newly merged LessWrong and hupu/mentions adapter
files that were still referencing the deleted root shim files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* test(xueqiu): mock logger via src path

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 03:35:12 +08:00

93 lines
3.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { CommandExecutionError } from '../../src/errors.js';
import { cli, Strategy } from '../../src/registry.js';
type SubstackPostResult = {
title: string;
author: string;
date: string;
description: string;
url: string;
};
function headers(): HeadersInit {
return {
'User-Agent': 'Mozilla/5.0',
Accept: 'application/json',
};
}
function trim(value: unknown): string {
return typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : '';
}
function publicationBaseUrl(publication: any): string {
if (publication?.custom_domain) return `https://${publication.custom_domain}`;
if (publication?.subdomain) return `https://${publication.subdomain}.substack.com`;
return '';
}
async function searchPosts(keyword: string, limit: number): Promise<SubstackPostResult[]> {
const url = new URL('https://substack.com/api/v1/post/search');
url.searchParams.set('query', keyword);
url.searchParams.set('page', '0');
url.searchParams.set('includePlatformResults', 'true');
const resp = await fetch(url, { headers: headers() });
if (!resp.ok) throw new CommandExecutionError(`Substack post search failed: HTTP ${resp.status}`);
const data = await resp.json() as { results?: any[] };
const results = Array.isArray(data?.results) ? data.results : [];
return results.slice(0, limit).map((item, index) => ({
rank: index + 1,
title: trim(item?.title),
author: trim(item?.publishedBylines?.[0]?.name),
date: trim(item?.post_date).split('T')[0] || trim(item?.post_date),
description: trim(item?.description || item?.subtitle || item?.truncated_body_text).slice(0, 150),
url: trim(item?.canonical_url),
}));
}
async function searchPublications(keyword: string, limit: number): Promise<SubstackPostResult[]> {
const url = new URL('https://substack.com/api/v1/profile/search');
url.searchParams.set('query', keyword);
url.searchParams.set('page', '0');
const resp = await fetch(url, { headers: headers() });
if (!resp.ok) throw new CommandExecutionError(`Substack publication search failed: HTTP ${resp.status}`);
const data = await resp.json() as { results?: any[] };
const results = Array.isArray(data?.results) ? data.results : [];
return results.slice(0, limit).map((item, index) => {
const publication = item?.primaryPublication || item?.publicationUsers?.[0]?.publication || {};
return {
rank: index + 1,
title: trim(publication?.name || item?.name),
author: trim(item?.name),
date: '',
description: trim(publication?.hero_text || item?.bio).slice(0, 150),
url: publicationBaseUrl(publication),
};
});
}
cli({
site: 'substack',
name: 'search',
description: '搜索 Substack 文章和 Newsletter',
domain: 'substack.com',
strategy: Strategy.PUBLIC,
browser: false,
args: [
{ name: 'keyword', required: true, positional: true, help: '搜索关键词' },
{ name: 'type', default: 'posts', choices: ['posts', 'publications'], help: '搜索类型(posts=文章, publications=Newsletter)' },
{ name: 'limit', type: 'int', default: 20, help: '返回结果数量' },
],
columns: ['rank', 'title', 'author', 'date', 'description', 'url'],
func: async (_page, args) => {
const limit = Math.max(1, Math.min(Number(args.limit) || 20, 50));
return args.type === 'publications'
? searchPublications(args.keyword, limit)
: searchPosts(args.keyword, limit);
},
});