Skip to content

Commit

Permalink
instant answers
Browse files Browse the repository at this point in the history
  • Loading branch information
A-da-m authored Mar 6, 2022
1 parent 1c3a34c commit 9f12f71
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
137 changes: 137 additions & 0 deletions src/engines/ddg.ts
Original file line number Diff line number Diff line change
@@ -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<DuckDuckGo> => {
const [{ data: suggestions }, { data: instantAnswer }] = await Promise.all([
axios.get<{ phrase: string }[]>(Engine.DDG + query),
axios.get<RootObject>('https://api.duckduckgo.com?q=' + query + "&format=json")
])
return {
instantAnswer,
suggestions: suggestions.map(x => x.phrase),
}
}
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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=",
Expand All @@ -17,10 +18,7 @@ export const google = async (query: string): Promise<Result> => {
return $("suggestion").toArray().map(x => x.attribs.data);
}

export const ddg = async (query: string): Promise<Result> => {
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<Result> => {
const { data } = await axios.get<{
Expand All @@ -39,7 +37,7 @@ export const brave = async (query: string): Promise<Result> => {
export const all = async (query: string): Promise<Result> => {
const all = [
...await google(query),
...await ddg(query),
...(await ddg(query)).suggestions,
...await yahoo(query),
...await brave(query),
];
Expand Down

0 comments on commit 9f12f71

Please sign in to comment.