PageRenderTime 50ms CodeModel.GetById 45ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/duckduck.js

https://github.com/gcr/ips-lurker
JavaScript | 83 lines | 64 code | 9 blank | 10 comment | 21 complexity | e8972c42f322428aa983e894981762f7 MD5 | raw file
 1/*
 2 * duckduckgo.js -- zero-click API for duck duck go
 3 * see http://duckduckgo.com/api.html
 4 *
 5 */
 6
 7var http = require('http'),
 8    querystring = require('querystring'),
 9    randomSay = require('../lib/plugin_glue').randomSay;
10
11
12function duckDuckGoQuery(query, cb) {
13  // run cb(wittyResponse) when DDG returns something special
14  http
15    .createClient(80, 'api.duckduckgo.com')
16    .request('GET', '/?'+querystring.stringify(
17        {q: query, format: "json", no_redirect: 1, no_html: 1}
18      ),
19      {'host':'api.duckduckgo.com'})
20    .on('response', function(res) {
21      // response could come in more than one packet, so create a buffer
22      // to hold it all
23      var body='';
24      res.on('data',function(data){body+=data;})
25          .on('end', function(end) {
26            try {
27              // Parse the object we get
28              var response = JSON.parse(body.toString().trim());
29              console.log(response);
30
31              if (response.Answer) {
32                return cb(response.Answer);
33              } else if (response.Abstract) {
34                if (response.AbstractURL) {
35                  return cb(response.Abstract+" [url=\""+response.AbstractURL+"\"](see also)[/url]");
36                } else {
37                  return cb(response.Abstract);
38                }
39              } else if (response.Definition) {
40                return cb(response.Definition);
41              } else if (response.RelatedTopics[0]) {
42                return cb(response.RelatedTopics[0].Text);
43              } else {
44                return cb();
45              }
46            } catch (err) {}
47          });
48      })
49      .end(); // Send our request
50}
51
52exports.init = function(chat) {
53
54  chat.on('message', function(msg, usr, uid) {
55      if (!chat.settled || uid == chat.userId) { return; }
56      if (msg.match(/lurker/i)) {
57          var query = msg.match(/what('s| is) *(the|an|a)* ([^?]*)/i);
58          // vim: '
59          if (query) {
60            var q = query[3];
61            if (q.length) {
62              if (q.match(/coitus/i) || q.match(/sex/i)) {
63                chat.say("It's what you wish you were having, "+usr+"!");
64              } else {
65                duckDuckGoQuery(q, function(text) {
66                    if (text) {
67                      chat.say(text);
68                    } else {
69                      randomSay([
70                          "no clue", "not sure", "magic 8-ball says: OUTCOME UNCERTAIN",
71                          "haven't the foggiest", "dunno", "your guess is as good as mine"]);
72                    }
73                  });
74              }
75            }
76          }
77        }
78
79  });
80
81};
82
83exports.duckDuckGoQuery = duckDuckGoQuery;