alexarank

A simple node package to get Alexa traffic rank for a domain or URL
Log | Files | Refs | README | LICENSE

commit 116895aa0f4f669df553b8f5f1d45fcf099143e9
Author: Frederic Cambus <fcambus@users.sourceforge.net>
Date:   Fri, 27 Dec 2013 23:56:36 +0100

Initial commit

Diffstat:
ALICENSE | 24++++++++++++++++++++++++
AREADME.md | 47+++++++++++++++++++++++++++++++++++++++++++++++
Abin/alexarank | 17+++++++++++++++++
Aindex.js | 1+
Alib/alexarank.js | 46++++++++++++++++++++++++++++++++++++++++++++++
Apackage.json | 34++++++++++++++++++++++++++++++++++
6 files changed, 169 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2013, Frederic Cambus +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md @@ -0,0 +1,47 @@ +# alexarank + +## Description + +A simple node package to get Alexa traffic rank for a domain or URL. + +## Installation + +Install the module : + + npm install -g alexarank + + +## Usage example + +Using the module is pretty straightforward : + + var alexa = require('alexarank'); + + alexa("http://www.echojs.com/", function(error, result) { + if (!error) { + console.log(JSON.stringify(result)); + } else { + console.log(error); + } + }); + + +## CLI tool + +There is a bundled `alexarank` CLI tool : + + USAGE : alexarank domain + + EXAMPLES : alexarank http://www.echojs.com + alexarank echojs.com + +## License + +alexarank is released under the BSD 2-Clause license. See `LICENSE` file for details. + +## Author + +alexarank is developed by Frederic Cambus + +- Site : http://www.cambus.net +- Twitter: http://twitter.com/fcambus diff --git a/bin/alexarank b/bin/alexarank @@ -0,0 +1,17 @@ +#!/usr/bin/env node + +var alexa = require('../lib/alexarank'); + +if (process.argv.length == 2) { + console.log('USAGE : alexarank domain\n'); + console.log('EXAMPLES : alexarank http://www.echojs.com\n alexarank echojs.com'); + process.exit(1); +} + +alexa(process.argv[2], function(error, result) { + if (!error) { + console.log(JSON.stringify(result)); + } else { + console.log(error); + } +}); diff --git a/index.js b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/alexarank'); diff --git a/lib/alexarank.js b/lib/alexarank.js @@ -0,0 +1,46 @@ +/*****************************************************************************/ +/* */ +/* alexarank 0.1.0 (c) by Frederic Cambus 2013 */ +/* https://github.com/fcambus/alexarank */ +/* */ +/* Created: 2013/12/14 */ +/* Last Updated: 2013/12/27 */ +/* */ +/* alexarank is released under the BSD 2-Clause license. */ +/* See LICENSE file for details. */ +/* */ +/*****************************************************************************/ + +var request = require('request'); +var xml2js = require('xml2js'); + +module.exports = function(url, callback) { + request('http://data.alexa.com/data?cli=10&url=' + url, function(error, response, body) { + if (error) { + callback(new Error('Cannot reach Alexa API')); + } else if (response.statusCode != 200) { + callback(new Error('Cannot fetch Alexa API Data')); + } else { + xml2js.parseString(body, { + normalizeTags: true, + explicitArray: false + }, function(error, result) { + if (error) { + callback(new Error('Cannot parse Alexa API Data')); + } else { + var alexa = {}; + + alexa.url = result.alexa.$.URL; + alexa.idn = result.alexa.$.IDN; + + if (typeof result.alexa.sd != "undefined") { + alexa.rank = result.alexa.sd.popularity.$.TEXT; + alexa.reach = result.alexa.sd.reach.$.RANK; + } + + callback(null, alexa); + } + }); + } + }); +}; diff --git a/package.json b/package.json @@ -0,0 +1,34 @@ +{ + "name": "alexarank", + "version": "0.1.0", + "description": "A simple node package to get Alexa traffic rank for a domain or URL", + "main": "index.js", + "bin": { + "alexarank": "./bin/alexarank" + }, + "repository": { + "type": "git", + "url": "https://github.com/fcambus/alexarank.git" + }, + "keywords": [ + "alexa", + "api", + "traffic", + "rank" + ], + "author": { + "name": "Frederic Cambus", + "url": "http://www.cambus.net" + }, + "licenses": [{ + "type": "BSD", + "url": "https://github.com/fcambus/alexarank/blob/master/LICENSE" + }], + "bugs": { + "url": "https://github.com/fcambus/alexarank/issues" + }, + "dependencies": { + "xml2js": "~0.4.0", + "request": "~2.30.0" + } +}