wip: support wechat search

This commit is contained in:
Aaron Ji 2025-03-26 14:50:51 +08:00
parent c758ec5b51
commit 9d51458350
3 changed files with 24 additions and 3 deletions

View File

@ -90,8 +90,8 @@ export class SearcherHost extends RPCHost {
count: number,
@Param('type', { type: new Set(['web', 'images', 'news']), default: 'web' })
variant: 'web' | 'images' | 'news',
@Param('provider', { type: new Set(['google', 'bing']), default: 'google' })
searchEngine: 'google' | 'bing',
@Param('provider', { type: new Set(['google', 'bing', 'wechat']), default: 'google' })
searchEngine: 'google' | 'bing' | 'wechat',
@Param('num', { validate: (v: number) => v >= 0 && v <= 20 })
num?: number,
@Param('gl', { validate: (v: string) => WORLD_COUNTRY_CODES.includes(v?.toLowerCase()) }) gl?: string,
@ -171,6 +171,9 @@ export class SearcherHost extends RPCHost {
}
let chargeAmountScaler = 1;
if (searchEngine === 'wechat') {
this.threadLocal.set('wechat-preferred', true);
}
if (searchEngine === 'bing') {
this.threadLocal.set('bing-preferred', true);
chargeAmountScaler = 3;

View File

@ -7,6 +7,7 @@ import { SerperBingHTTP, SerperGoogleHTTP, SerperImageSearchResponse, SerperNews
import { BlackHoleDetector } from './blackhole-detector';
import { Context } from './registry';
import { ServiceBadAttemptError } from '../shared';
import { WechatSearchHTTP } from '../shared/3rd-party/wechat-search';
@singleton()
export class SerperSearchService extends AsyncService {
@ -15,6 +16,7 @@ export class SerperSearchService extends AsyncService {
serperGoogleSearchHTTP!: SerperGoogleHTTP;
serperBingSearchHTTP!: SerperBingHTTP;
wechatSearchHTTP!: WechatSearchHTTP;
constructor(
protected globalLogger: GlobalLogger,
@ -31,13 +33,18 @@ export class SerperSearchService extends AsyncService {
this.serperGoogleSearchHTTP = new SerperGoogleHTTP(this.secretExposer.SERPER_SEARCH_API_KEY);
this.serperBingSearchHTTP = new SerperBingHTTP(this.secretExposer.SERPER_SEARCH_API_KEY);
this.wechatSearchHTTP = new WechatSearchHTTP(this.secretExposer.WECHAT_SEARCH_API_KEY);
}
*iterClient() {
const preferBingSearch = this.threadLocal.get('bing-preferred');
const preferWechatSearch = this.threadLocal.get('wechat-preferred');
if (preferBingSearch) {
yield this.serperBingSearchHTTP;
}
if (preferWechatSearch) {
yield this.wechatSearchHTTP;
}
while (true) {
yield this.serperGoogleSearchHTTP;
}
@ -59,6 +66,17 @@ export class SerperSearchService extends AsyncService {
try {
this.logger.debug(`Doing external search`, query);
let r;
if (client instanceof WechatSearchHTTP) {
r = await client.blogSearch({
kw: query.q,
page: query.page
});
this.blackHoleDetector.itWorked();
return r.parsed;
}
switch (variant) {
case 'images': {
r = await client.imageSearch(query);

@ -1 +1 @@
Subproject commit 8c31e85dc52dfcc7d1d86df0328df3a94319b534
Subproject commit 00717c3b08eeaf425d3a0d4880b05714051c01bd