PageRenderTime 75ms CodeModel.GetById 43ms app.highlight 5ms RepoModel.GetById 25ms app.codeStats 0ms

/plugins/duckduckgo.js

https://github.com/gildean/dunn
JavaScript | 86 lines | 64 code | 11 blank | 11 comment | 15 complexity | 43d02f3a3d510d73adc4c234ab70a45b MD5 | raw file
 1/*
 2 * @Plugin        	DuckDuckGo
 3 * @Description   	Use the duckduckgo api
 4 * @Trigger       	.ddg
 5 *
 6 * @Author        	Olli K
 7 * @Website      	github.com/gildean
 8 * @License       	MIT
 9 * @Copyright    	-
10 *
11 */
12
13var qs = require('querystring');
14var url = require('url');
15
16function Ddg(irc) {
17    "use strict";
18    var trigger = 'ddg';
19    var mesLen = trigger.length + irc.command.length + 1;
20
21    var question = function (pla, channel, nick, par, message) {
22
23        function isValidJson(json) {
24            try {
25                return JSON.parse(json);
26            } catch (e) {
27                return false;
28            }
29        }
30
31        function sendToIrc(err, links) {
32            return irc.send(channel, nick + ': ' + ((!err) ? links : err));
33        }
34        
35        function getDataFromJson(jsonn, query, cb) {
36            var json = isValidJson(jsonn);
37            if (json) {
38                var abstractText = (json.AbstractText) ? (json.AbstractText.replace(/\n/ig, ' ').replace(/<pre>.+?<\/pre>/ig, '')) + ' || ' : '';
39                var abstractSource = (json.AbstractSource && json.AbstractURL) ? json.AbstractSource + ': ' + json.AbstractURL + ' || ' : '';
40                var answerText = (json.Answer) ? json.Answer + ' || ' : '';
41                var definitionText = (json.DefinitionText && json.DefinitionURL) ? 'Definition ( ' + json.DefinitionURL + ' ): ' + json.DefinitionText + ' || ' : '';
42                var redirect = (json.Redirect) ? url.resolve('https://duckduckgo.com', json.Redirect) + ' || ' : '';
43                var links = abstractText + abstractSource + answerText + definitionText + redirect;
44                if (links === '') {
45                    links = 'https://duckduckgo.com/?q=' + query + '    ';
46                }
47                return cb(null, links.substring(0, links.length - 4));
48            } else {
49                return cb('No valid answer', null);
50            }
51        }
52
53        function handleAnswer(err, answer, query) {
54            if (!err) {
55                return getDataFromJson(answer, query, sendToIrc);
56            } else {
57                return sendToIrc(err);
58            }
59        }
60
61        (function searchDdg(message, cb) {
62            var msg = qs.escape(message.substring(mesLen).trim());
63            if (msg !== '') {
64                var qPath = '/?q=' + msg + '&format=json&no_redirect=1&no_html=1';
65                var options = {
66                    hostname: 'api.duckduckgo.com',
67                    path: qPath
68                };
69                var req = irc.httpGet(options, function (err, res, json) {
70                    if (!err) {
71                        return cb(null, json, msg);
72                    } else {
73                        return cb(err.message, null);
74                    }
75                });
76            } else {
77                return cb('DuckDuckGo e.g. \'' + irc.command + trigger + ' define google\' or \'' + irc.command + trigger + ' 10+20*50\'', null);
78            }
79        }(message, handleAnswer));
80
81    };
82
83    irc.addTrigger(trigger, question);
84}
85
86exports.Plugin = Ddg;