Skip to content

Commit

Permalink
basic instant answer support
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewendling committed Jun 27, 2013
0 parents commit 524ccf4
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
npm-debug.log
node_modules
test.js
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
node_modules/
test.js
26 changes: 26 additions & 0 deletions 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);
});
```
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
exports.DDG = require('./lib/ddg');
88 changes: 88 additions & 0 deletions 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;
13 changes: 13 additions & 0 deletions package.json
@@ -0,0 +1,13 @@
{
"name": "ddg-api",
"version": "0.1.0",
"main": "./lib/index.js",
"author": "Luke Wendling <luke@lukewendling.com>",
"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"
}
}

0 comments on commit 524ccf4

Please sign in to comment.