Skip to content

Commit 69197f8

Browse files
authored
Merge pull request #1 from getskye/master
instant answers
2 parents 44df8c2 + 9f12f71 commit 69197f8

File tree

3 files changed

+142
-7
lines changed

3 files changed

+142
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@getskye/suggest",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Google, DuckDuckGo, Bing, and other autosuggestion APIs",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/engines/ddg.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import axios from 'axios';
2+
import { Engine, Result } from '../'
3+
4+
export interface Icon {
5+
Height: string;
6+
URL: string;
7+
Width: string;
8+
}
9+
10+
export interface Topic {
11+
FirstURL: string;
12+
Icon: Icon;
13+
Result: string;
14+
Text: string;
15+
}
16+
17+
export interface RelatedTopic {
18+
FirstURL: string;
19+
Icon: Icon;
20+
Result: string;
21+
Text: string;
22+
Name: string;
23+
Topics: Topic[];
24+
}
25+
26+
export interface Developer {
27+
name: string;
28+
type: string;
29+
url: string;
30+
}
31+
32+
export interface Maintainer {
33+
github: string;
34+
}
35+
36+
export interface SrcOptions {
37+
directory: string;
38+
is_fanon: number;
39+
is_mediawiki: number;
40+
is_wikipedia: number;
41+
language: string;
42+
min_abstract_length: string;
43+
skip_abstract: number;
44+
skip_abstract_paren: number;
45+
skip_end: string;
46+
skip_icon: number;
47+
skip_image_name: number;
48+
skip_qr: string;
49+
source_skip: string;
50+
src_info: string;
51+
}
52+
53+
export interface Meta {
54+
attribution?: any;
55+
blockgroup?: any;
56+
created_date?: any;
57+
description: string;
58+
designer?: any;
59+
dev_date?: any;
60+
dev_milestone: string;
61+
developer: Developer[];
62+
example_query: string;
63+
id: string;
64+
is_stackexchange?: any;
65+
js_callback_name: string;
66+
live_date?: any;
67+
maintainer: Maintainer;
68+
name: string;
69+
perl_module: string;
70+
producer?: any;
71+
production_state: string;
72+
repo: string;
73+
signal_from: string;
74+
src_domain: string;
75+
src_id: number;
76+
src_name: string;
77+
src_options: SrcOptions;
78+
src_url?: any;
79+
status: string;
80+
tab: string;
81+
topic: string[];
82+
unsafe: number;
83+
}
84+
85+
export interface Answer {
86+
from: string
87+
id: string
88+
name: string
89+
result: string
90+
signal: string
91+
templates: {
92+
group: string,
93+
options: {
94+
content: string
95+
}
96+
}
97+
}
98+
99+
export interface RootObject {
100+
Abstract: string;
101+
AbstractSource: string;
102+
AbstractText: string;
103+
AbstractURL: string;
104+
Answer: string | Answer;
105+
AnswerType: string | "calc";
106+
Definition: string;
107+
DefinitionSource: string;
108+
DefinitionURL: string;
109+
Entity: string;
110+
Heading: string;
111+
Image: string;
112+
ImageHeight: number;
113+
ImageIsLogo: number;
114+
ImageWidth: number;
115+
Infobox: string;
116+
Redirect: string;
117+
RelatedTopics: RelatedTopic[];
118+
Results: any[];
119+
Type: string;
120+
meta: Meta;
121+
}
122+
123+
export interface DuckDuckGo {
124+
instantAnswer: RootObject
125+
suggestions: Result
126+
}
127+
128+
export default async (query: string): Promise<DuckDuckGo> => {
129+
const [{ data: suggestions }, { data: instantAnswer }] = await Promise.all([
130+
axios.get<{ phrase: string }[]>(Engine.DDG + query),
131+
axios.get<RootObject>('https://api.duckduckgo.com?q=' + query + "&format=json")
132+
])
133+
return {
134+
instantAnswer,
135+
suggestions: suggestions.map(x => x.phrase),
136+
}
137+
}

src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import axios from "axios";
22
import { load } from "cheerio";
3+
import duckduckgo from './engines/ddg'
34

4-
enum Engine {
5+
export enum Engine {
56
GOOGLE = "https://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=",
67
DDG = "https://duckduckgo.com/ac/?kl=wt-wt&q=",
78
BING = "https://www.bingapis.com/api/v7/suggestions?appid=6D0A9B8C5100E9ECC7E11A104ADD76C10219804B&q=",
@@ -17,10 +18,7 @@ export const google = async (query: string): Promise<Result> => {
1718
return $("suggestion").toArray().map(x => x.attribs.data);
1819
}
1920

20-
export const ddg = async (query: string): Promise<Result> => {
21-
const { data } = await axios.get<{ phrase: string }[]>(Engine.DDG + query);
22-
return data.map(x => x.phrase);
23-
}
21+
export const ddg = duckduckgo
2422

2523
export const yahoo = async (query: string): Promise<Result> => {
2624
const { data } = await axios.get<{
@@ -39,7 +37,7 @@ export const brave = async (query: string): Promise<Result> => {
3937
export const all = async (query: string): Promise<Result> => {
4038
const all = [
4139
...await google(query),
42-
...await ddg(query),
40+
...(await ddg(query)).suggestions,
4341
...await yahoo(query),
4442
...await brave(query),
4543
];

0 commit comments

Comments
 (0)