alexarank

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

alexarank.js (1894B)


      1 'use strict';
      2 
      3 /*****************************************************************************/
      4 /*                                                                           */
      5 /* alexarank 0.2.0                                                           */
      6 /* Copyright (c) 2013-2017, Frederic Cambus                                  */
      7 /* https://github.com/fcambus/alexarank                                      */
      8 /*                                                                           */
      9 /* Created: 2013-12-14                                                       */
     10 /* Last Updated: 2017-02-03                                                  */
     11 /*                                                                           */
     12 /* alexarank is released under the BSD 2-Clause license.                     */
     13 /* See LICENSE file for details.                                             */
     14 /*                                                                           */
     15 /*****************************************************************************/
     16 
     17 require('es6-promise').polyfill();
     18 var fetch = require('isomorphic-fetch');
     19 var xml2js = require('xml2js');
     20 
     21 module.exports = function(url, callback) {
     22 	fetch('https://data.alexa.com/data?cli=10&url=' + encodeURIComponent(url))
     23 	.then(function (res) {
     24 		return res.text();
     25 	})
     26 	.then(function (body) {
     27 		xml2js.parseString(body, {
     28 			normalizeTags: true,
     29 			explicitArray: false
     30 		}, function(error, result) {
     31 			if (error) {
     32 				callback(new Error('Cannot parse Alexa API Data'));
     33 			} else {
     34 				var alexa = {};
     35 
     36 				alexa.url = result.alexa.$.URL;
     37 				alexa.idn = result.alexa.$.IDN;
     38 
     39 				if (typeof result.alexa.sd != "undefined") {
     40 					alexa.rank = result.alexa.sd.popularity.$.TEXT;
     41 					alexa.reach = result.alexa.sd.reach.$.RANK;
     42 				}
     43 
     44 				callback(null, alexa);
     45 			}
     46 		});
     47 	})
     48 	.catch(function (err) {
     49 		callback(err);
     50 	});
     51 };