Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 524ccf4
Showing
6 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
npm-debug.log | ||
node_modules | ||
test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.DDG = require('./lib/ddg'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |