/
DuckDuckGoProvider.js
63 lines (55 loc) · 1.87 KB
/
DuckDuckGoProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import AutocompleteProvider from './AutocompleteProvider';
import Q from 'q';
import 'whatwg-fetch';
const DDG_REGEX = /\/ddg\s+(.+)$/;
const REFERER = 'vector';
let instance = null;
export default class DuckDuckGoProvider extends AutocompleteProvider {
static getQueryUri(query: String) {
return `http://api.duckduckgo.com/?q=${encodeURIComponent(query)}`
+ `&format=json&no_redirect=1&no_html=1&t=${encodeURIComponent(REFERER)}`;
}
getCompletions(query: String) {
let match = DDG_REGEX.exec(query);
if(!query || !match)
return Q.when([]);
return fetch(DuckDuckGoProvider.getQueryUri(match[1]), {
method: 'GET'
})
.then(response => response.json())
.then(json => {
let results = json.Results.map(result => {
return {
title: result.Text,
description: result.Result
};
});
if(json.Answer) {
results.unshift({
title: json.Answer,
description: json.AnswerType
});
}
if(json.RelatedTopics && json.RelatedTopics.length > 0) {
results.unshift({
title: json.RelatedTopics[0].Text
});
}
if(json.AbstractText) {
results.unshift({
title: json.AbstractText
});
}
// console.log(results);
return results;
});
}
getName() {
return 'Results from DuckDuckGo';
}
static getInstance(): DuckDuckGoProvider {
if(instance == null)
instance = new DuckDuckGoProvider();
return instance;
}
}