diff --git a/package.json b/package.json index fdf5870..c3a7fe2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@getskye/suggest", - "version": "1.0.0", + "version": "1.0.1", "description": "Google, DuckDuckGo, Bing, and other autosuggestion APIs", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/engines/ddg.ts b/src/engines/ddg.ts new file mode 100644 index 0000000..d27aa77 --- /dev/null +++ b/src/engines/ddg.ts @@ -0,0 +1,137 @@ +import axios from 'axios'; +import { Engine, Result } from '../' + +export interface Icon { + Height: string; + URL: string; + Width: string; +} + +export interface Topic { + FirstURL: string; + Icon: Icon; + Result: string; + Text: string; +} + +export interface RelatedTopic { + FirstURL: string; + Icon: Icon; + Result: string; + Text: string; + Name: string; + Topics: Topic[]; +} + +export interface Developer { + name: string; + type: string; + url: string; +} + +export interface Maintainer { + github: string; +} + +export interface SrcOptions { + directory: string; + is_fanon: number; + is_mediawiki: number; + is_wikipedia: number; + language: string; + min_abstract_length: string; + skip_abstract: number; + skip_abstract_paren: number; + skip_end: string; + skip_icon: number; + skip_image_name: number; + skip_qr: string; + source_skip: string; + src_info: string; +} + +export interface Meta { + attribution?: any; + blockgroup?: any; + created_date?: any; + description: string; + designer?: any; + dev_date?: any; + dev_milestone: string; + developer: Developer[]; + example_query: string; + id: string; + is_stackexchange?: any; + js_callback_name: string; + live_date?: any; + maintainer: Maintainer; + name: string; + perl_module: string; + producer?: any; + production_state: string; + repo: string; + signal_from: string; + src_domain: string; + src_id: number; + src_name: string; + src_options: SrcOptions; + src_url?: any; + status: string; + tab: string; + topic: string[]; + unsafe: number; +} + +export interface Answer { + from: string + id: string + name: string + result: string + signal: string + templates: { + group: string, + options: { + content: string + } + } +} + +export interface RootObject { + Abstract: string; + AbstractSource: string; + AbstractText: string; + AbstractURL: string; + Answer: string | Answer; + AnswerType: string | "calc"; + Definition: string; + DefinitionSource: string; + DefinitionURL: string; + Entity: string; + Heading: string; + Image: string; + ImageHeight: number; + ImageIsLogo: number; + ImageWidth: number; + Infobox: string; + Redirect: string; + RelatedTopics: RelatedTopic[]; + Results: any[]; + Type: string; + meta: Meta; +} + +export interface DuckDuckGo { + instantAnswer: RootObject + suggestions: Result +} + +export default async (query: string): Promise => { + const [{ data: suggestions }, { data: instantAnswer }] = await Promise.all([ + axios.get<{ phrase: string }[]>(Engine.DDG + query), + axios.get('https://api.duckduckgo.com?q=' + query + "&format=json") + ]) + return { + instantAnswer, + suggestions: suggestions.map(x => x.phrase), + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 9c01e9d..9438b98 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,8 @@ import axios from "axios"; import { load } from "cheerio"; +import duckduckgo from './engines/ddg' -enum Engine { +export enum Engine { GOOGLE = "https://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=", DDG = "https://duckduckgo.com/ac/?kl=wt-wt&q=", BING = "https://www.bingapis.com/api/v7/suggestions?appid=6D0A9B8C5100E9ECC7E11A104ADD76C10219804B&q=", @@ -17,10 +18,7 @@ export const google = async (query: string): Promise => { return $("suggestion").toArray().map(x => x.attribs.data); } -export const ddg = async (query: string): Promise => { - const { data } = await axios.get<{ phrase: string }[]>(Engine.DDG + query); - return data.map(x => x.phrase); -} +export const ddg = duckduckgo export const yahoo = async (query: string): Promise => { const { data } = await axios.get<{ @@ -39,7 +37,7 @@ export const brave = async (query: string): Promise => { export const all = async (query: string): Promise => { const all = [ ...await google(query), - ...await ddg(query), + ...(await ddg(query)).suggestions, ...await yahoo(query), ...await brave(query), ];