2016-07-03 19:45:13 +03:00
|
|
|
import React from 'react';
|
2016-06-12 14:32:46 +03:00
|
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
|
|
|
import Q from 'q';
|
|
|
|
import 'whatwg-fetch';
|
|
|
|
|
2016-07-03 19:45:13 +03:00
|
|
|
import {TextualCompletion} from './Components';
|
|
|
|
|
2016-06-21 13:16:20 +03:00
|
|
|
const DDG_REGEX = /\/ddg\s+(.+)$/g;
|
2016-07-03 19:45:13 +03:00
|
|
|
const REFERRER = 'vector';
|
2016-06-12 14:32:46 +03:00
|
|
|
|
2016-06-20 11:22:55 +03:00
|
|
|
let instance = null;
|
|
|
|
|
2016-06-12 14:32:46 +03:00
|
|
|
export default class DuckDuckGoProvider extends AutocompleteProvider {
|
2016-06-21 13:16:20 +03:00
|
|
|
constructor() {
|
|
|
|
super(DDG_REGEX);
|
|
|
|
}
|
|
|
|
|
2016-06-12 14:32:46 +03:00
|
|
|
static getQueryUri(query: String) {
|
2016-07-04 23:07:01 +03:00
|
|
|
return `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}`
|
2016-07-03 19:45:13 +03:00
|
|
|
+ `&format=json&no_redirect=1&no_html=1&t=${encodeURIComponent(REFERRER)}`;
|
2016-06-12 14:32:46 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 13:16:20 +03:00
|
|
|
getCompletions(query: string, selection: {start: number, end: number}) {
|
2016-07-03 19:45:13 +03:00
|
|
|
let {command, range} = this.getCurrentCommand(query, selection);
|
|
|
|
if (!query || !command) {
|
2016-06-12 14:32:46 +03:00
|
|
|
return Q.when([]);
|
2016-07-03 19:45:13 +03:00
|
|
|
}
|
2016-06-12 14:32:46 +03:00
|
|
|
|
2016-06-21 13:16:20 +03:00
|
|
|
return fetch(DuckDuckGoProvider.getQueryUri(command[1]), {
|
2016-07-03 19:45:13 +03:00
|
|
|
method: 'GET',
|
2016-06-17 02:28:09 +03:00
|
|
|
})
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(json => {
|
|
|
|
let results = json.Results.map(result => {
|
|
|
|
return {
|
2016-07-03 19:45:13 +03:00
|
|
|
completion: result.Text,
|
|
|
|
component: (
|
|
|
|
<TextualCompletion
|
|
|
|
title={result.Text}
|
|
|
|
description={result.Result} />
|
|
|
|
),
|
|
|
|
range,
|
2016-06-17 02:28:09 +03:00
|
|
|
};
|
|
|
|
});
|
2016-07-03 19:45:13 +03:00
|
|
|
if (json.Answer) {
|
2016-06-17 02:28:09 +03:00
|
|
|
results.unshift({
|
2016-07-03 19:45:13 +03:00
|
|
|
completion: json.Answer,
|
|
|
|
component: (
|
|
|
|
<TextualCompletion
|
|
|
|
title={json.Answer}
|
|
|
|
description={json.AnswerType} />
|
|
|
|
),
|
|
|
|
range,
|
2016-06-17 02:28:09 +03:00
|
|
|
});
|
|
|
|
}
|
2016-07-03 19:45:13 +03:00
|
|
|
if (json.RelatedTopics && json.RelatedTopics.length > 0) {
|
2016-06-17 02:28:09 +03:00
|
|
|
results.unshift({
|
2016-07-03 19:45:13 +03:00
|
|
|
completion: json.RelatedTopics[0].Text,
|
|
|
|
component: (
|
|
|
|
<TextualCompletion
|
|
|
|
title={json.RelatedTopics[0].Text} />
|
|
|
|
),
|
|
|
|
range,
|
2016-06-17 02:28:09 +03:00
|
|
|
});
|
|
|
|
}
|
2016-07-03 19:45:13 +03:00
|
|
|
if (json.AbstractText) {
|
2016-06-17 02:28:09 +03:00
|
|
|
results.unshift({
|
2016-07-03 19:45:13 +03:00
|
|
|
completion: json.AbstractText,
|
|
|
|
component: (
|
|
|
|
<TextualCompletion
|
|
|
|
title={json.AbstractText} />
|
|
|
|
),
|
|
|
|
range,
|
2016-06-17 02:28:09 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return results;
|
2016-06-12 14:32:46 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getName() {
|
2016-08-17 14:57:19 +03:00
|
|
|
return '🔍 Results from DuckDuckGo';
|
2016-06-12 14:32:46 +03:00
|
|
|
}
|
2016-06-20 11:22:55 +03:00
|
|
|
|
|
|
|
static getInstance(): DuckDuckGoProvider {
|
2016-07-03 19:45:13 +03:00
|
|
|
if (instance == null) {
|
2016-06-20 11:22:55 +03:00
|
|
|
instance = new DuckDuckGoProvider();
|
2016-07-03 19:45:13 +03:00
|
|
|
}
|
2016-06-20 11:22:55 +03:00
|
|
|
return instance;
|
|
|
|
}
|
2016-08-22 22:06:31 +03:00
|
|
|
|
|
|
|
renderCompletions(completions: [React.Component]): ?React.Component {
|
|
|
|
return <div className="mx_Autocomplete_Completion_container_block">
|
|
|
|
{completions}
|
|
|
|
</div>;
|
|
|
|
}
|
2016-06-12 14:32:46 +03:00
|
|
|
}
|