From 524ccf4ba7fe1439ffa80c6db1708d0e78336b5e Mon Sep 17 00:00:00 2001 From: Luke Wendling Date: Thu, 27 Jun 2013 12:24:00 -0500 Subject: [PATCH] basic instant answer support --- .gitignore | 3 ++ .npmignore | 2 ++ README.md | 26 ++++++++++++++++ index.js | 1 + lib/ddg.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 13 ++++++++ 6 files changed, 133 insertions(+) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 README.md create mode 100644 index.js create mode 100644 lib/ddg.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93c21b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +npm-debug.log +node_modules +test.js \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..d18d285 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +node_modules/ +test.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..44a782d --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# ddg-api - DuckDuckGo instant answer API node module + +> Access the [DuckDuckGo](https://duckduckgo.com/api) API with [Nodejs](http://nodejs.org). + + +## Installation + +To install via NPM type the following: `npm install ddg-api` + +You can also install via git by cloning: + +```shell +git clone https://github.com/lukewendling/ddg-api.git /path/to/project` +``` + +## Usage + +```js +var DDG = require('ddg-api').DDG; + +var ddg = new DDG('my-app-name'); + +ddg.instantAnswer('superman', {skip_disambig: '0'}, function(err, response) { + console.log(response); +}); +``` \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..787fbcb --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +exports.DDG = require('./lib/ddg'); \ No newline at end of file diff --git a/lib/ddg.js b/lib/ddg.js new file mode 100644 index 0000000..5916ad7 --- /dev/null +++ b/lib/ddg.js @@ -0,0 +1,88 @@ +var url = require('url'); +var http; + +var DDG = function (appName, opts) { + var config; + + this.appName = appName; + + if (opts === undefined || opts === null) { + opts = {}; + } + + config = { + format: opts.format || 'json', + hostname: opts.hostname || 'api.duckduckgo.com', + port: opts.port || 443 + }; + + if (config.port === 443) { + http = require('https'); + } else { + http = require('http'); + } + + this.config = config; + + return this; +}; + +DDG.prototype.instantAnswer = function (q, opts, cb) { + var params; + + if (opts === undefined || opts === null) { + opts = {}; + } + + params = { + q: q, + pretty: opts.pretty || '1', + no_html: opts.no_html || '0', + no_redirect: opts.no_redirect || '0', + skip_disambig: opts.skip_disambig || '0' + }; + + console.log(params); + this.get(params, cb); +}; + +DDG.prototype.get = function (params, cb) { + var req, pathParts, options; + + params.format = this.config.format; + params.t = this.appName; + + pathParts = { + pathname: '/', + query: params + }; + + options = { + hostname: this.config.hostname, + port: this.config.port, + path: url.format(pathParts) + }; + + req = http.request(options, function (res) { + var data = []; + res + .on('data', function (chunk) { data.push(chunk); }) + .on('end', function () { + data = data.join('').trim(); + var result; + try { + result = JSON.parse(data); + } catch (exp) { + result = {'status_code': 500, 'status_text': 'JSON Parse Failed'}; + } + cb(null, result); + }); + }); + req.end(); + + req.on('error', function (e) { + cb(e); + }); +}; + +module.exports = DDG; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..76064e1 --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "ddg-api", + "version": "0.1.0", + "main": "./lib/index.js", + "author": "Luke Wendling ", + "license": "BSD", + "description": "A Node.JS module to query the duckduckgo instant answers API.", + "keywords": ["duckduckgo", "api"], + "repository": { + "type": "git", + "url": "git://github.com/lukewendling/ddg-api.git" + } +} \ No newline at end of file