How to Check Alexa Rank in Java

  • 16 November 2017
  • ADM

 

How to Check Alexa Rank in Java - images/logos/java.jpg

 

This article will present how to check the Alexa Page Rank in Java using the undocumented API

http://data.alexa.com/data?cli=10&url=domainName

This API is used by the for example by the Chrome Alexa Traffic Rank extension. If you need more details about the page then you have to register on the Alexa home page.

If you open the link in the browser you can see the following XML as output:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Need more Alexa data?  Find our APIs here: https://aws.amazon.com/alexa/ -->
<ALEXA VER="0.9" URL="google.com/" HOME="0" AID="j7O2q1dI4820FF" IDN="google.com/">
	<SD>
		<POPULARITY URL="google.com/" TEXT="1" SOURCE="panel"/>
		<REACH RANK="2"/>
		<RANK DELTA="+0"/>
		<COUNTRY CODE="US" NAME="United States" RANK="1"/>
	</SD>
</ALEXA>

The Alexa rank is the “POPULARITY“ element, the “TEXT” attribute value.

Code

The following Java code will extract the Alexa rank from the XML output and will display the result in the console. The code will also contain functionality to add proxy settings if required.

package com.adm;

import java.io.InputStream;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class AlexaRankChecker {

    /**
     * The Alexa Rank API URL
     */
    private static String ALEXA_URL = "http://data.alexa.com/data?cli=10&url=";

    /**
     * The User Agent
     */
    private static final String agent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";

    private Proxy proxy;

    /**
     * The method will return the content in a {@link InputStream} object
     * 
     * @param domain
     *            The Domain name
     * 
     * @return the content as {@link InputStream} object
     * @throws Exception
     */
    private InputStream getContent(String domain) throws Exception {

	URL url = new URL(ALEXA_URL + domain);
	URLConnection connection = null;
	if (this.proxy != null)
	    connection = url.openConnection(this.proxy);
	else
	    connection = url.openConnection();

	connection.setRequestProperty("User-Agent", agent);
	return connection.getInputStream();
    }

    /**
     * 
     * Method used to add proxy settings
     *
     * @param ip
     *            the proxy IP
     * @param port
     *            the proxy port
     */
    public void setProxy(String ip, int port) {
	this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
    }

    /**
     * 
     * Method used to add proxy settings with authentication
     *
     * @param ip
     * @param port
     * @param username
     * @param password
     */
    public void setProxy(String ip, int port, String username, String password) {
	this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
	Authenticator authenticator = new Authenticator() {

	    public PasswordAuthentication getPasswordAuthentication() {
		return (new PasswordAuthentication(username, password.toCharArray()));
	    }
	};
	Authenticator.setDefault(authenticator);

    }

    /**
     * 
     * This method will get the content from the InputStream parameter and will
     * return a string
     *
     * @param input
     * @return
     * @throws Exception
     */
    private String parse(InputStream input) throws Exception {
	String result = "";
	DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	Document doc = dBuilder.parse(input);

	Element element = doc.getDocumentElement();

	NodeList nodeList = element.getElementsByTagName("POPULARITY");
	if (nodeList.getLength() > 0) {
	    Element elementAttribute = (Element) nodeList.item(0);
	    String ranking = elementAttribute.getAttribute("TEXT");
	    String url = elementAttribute.getAttribute("URL");
	    String sourse = elementAttribute.getAttribute("SOURCE");
	    String date = Calendar.getInstance().getTime().toString();
	    result = date + "|" + url + "|" + sourse + "|" + ranking;
	}

	NodeList nodeListC = element.getElementsByTagName("COUNTRY");
	if (nodeListC.getLength() > 0) {
	    for (int i = 0; i < nodeListC.getLength(); i++) {
		Element elementAttribute = (Element) nodeListC.item(i);
		String code = elementAttribute.getAttribute("CODE");
		String name = elementAttribute.getAttribute("NAME");
		String rank = elementAttribute.getAttribute("RANK");
		result += "|" + code + "|" + name + "|" + rank;
	    }
	}

	return result;
    }

    public String getRank(String domain) throws Exception {
	String result;
	InputStream is = getContent(domain);
	result = parse(is);
	is.close();
	return result;
    }

    public static void main(String[] args) throws Exception {
	System.out.println("Alexa Rank Reader");
	System.out.println();

	AlexaRankChecker alexa = new AlexaRankChecker();

	/**
	 * activate this line if you are behind a proxy server - change the settings
	 * accordingly
	 */
	alexa.setProxy("127.0.0.1", 7000);

	/**
	 * activate this line if you are behind a proxy server with authentication -
	 * change the settings accordingly
	 */
	// alexa.setProxy("127.0.0.1", 7000, "user", "passowrd");

	String domain1 = "google.com";
	String result1 = alexa.getRank(domain1);

	System.out.println("Domain name: " + domain1);
	System.out.println();
	System.out.println(result1);
	System.out.println();

	String domain2 = "admfactory.com";
	String result2 = alexa.getRank(domain2);

	System.out.println("Domain name: " + domain2);
	System.out.println();
	System.out.println(result2);
	System.out.println();

    }

}

Output

Alexa Rank Reader

Domain name: google.com

Wed Nov 15 16:32:29 GMT 2017|google.com/|panel|1|US|United States|1

Domain name: admfactory.com

Wed Nov 15 16:32:29 GMT 2017|admfactory.com/|panel|303196|GB|United Kingdom|23332

 

References