pull/643/head^2
Travis Fischer 2024-05-26 19:51:11 -05:00
rodzic b9d125191c
commit 4b9e9aed35
2 zmienionych plików z 36 dodań i 15 usunięć

Wyświetl plik

@ -4,9 +4,11 @@ import 'dotenv/config'
import { gracefulExit } from 'exit-hook'
import restoreCursor from 'restore-cursor'
import { SearxngClient } from '../src/services/searxng-client.js'
// import { ClearbitClient } from '../src/index.js'
// import { ProxycurlClient } from '../src/services/proxycurl-client.js'
import { WikipediaClient } from '../src/services/wikipedia-client.js'
// import { WikipediaClient } from '../src/services/wikipedia-client.js'
/**
* Scratch pad for testing.
@ -27,9 +29,16 @@ async function main() {
// })
// console.log(JSON.stringify(res, null, 2))
const wikipedia = new WikipediaClient()
const res = await wikipedia.getPageSummary({
title: 'Naruto_(TV_series)'
// const wikipedia = new WikipediaClient()
// const res = await wikipedia.getPageSummary({
// title: 'Naruto_(TV_series)'
// })
// console.log(JSON.stringify(res, null, 2))
const searxng = new SearxngClient()
const res = await searxng.search({
query: '4 + 2 * 10'
// engines: ['google']
})
console.log(JSON.stringify(res, null, 2))

Wyświetl plik

@ -227,11 +227,15 @@ export namespace searxng {
content?: string
author?: string
iframe_src?: string
category?: SearchCategory
engine?: SearchEngine
publishedDate?: string
}
export interface SearchResponse {
results: SearchResult[]
suggestions: string[]
query: string
}
}
@ -240,11 +244,9 @@ export namespace searxng {
*/
export class SearxngClient {
readonly ky: KyInstance
readonly apiKey: string
readonly apiBaseUrl: string
constructor({
apiKey = getEnv('SEARXNG_API_KEY'),
apiBaseUrl = getEnv('SEARXNG_API_BASE_URL'),
ky = defaultKy
}: {
@ -252,33 +254,43 @@ export class SearxngClient {
apiBaseUrl?: string
ky?: KyInstance
} = {}) {
assert(
apiKey,
'SearxngClient missing required "apiKey" (defaults to "SEARXNG_API_KEY")'
)
assert(
apiBaseUrl,
'SearxngClient missing required "apiBaseUrl" (defaults to "SEARXNG_API_BASE_URL")'
)
this.apiKey = apiKey
this.apiBaseUrl = apiBaseUrl
this.ky = ky.extend({ prefixUrl: apiBaseUrl })
}
async search(opts: searxng.SearchOptions): Promise<searxng.SearchResponse> {
async search({
query,
...opts
}: searxng.SearchOptions): Promise<searxng.SearchResponse> {
const res = await this.ky
.get('search', {
searchParams: pruneUndefined({
...omit(opts, 'categories', 'engines'),
q: query,
...opts,
categories: opts.categories?.join(','),
engines: opts.categories?.join(','),
engines: opts.engines?.join(','),
format: 'json'
})
})
.json<searxng.SearchResponse>()
return pick(res, 'results', 'suggestions')
res.results = res.results?.map(
(result: any) =>
omit(
result,
'parsed_url',
'engines',
'positions',
'template'
) as searxng.SearchResult
)
return pick(res, 'results', 'suggestions', 'query')
}
}