Tuesday, January 4, 2011

Page Rank Using Javascript

function getPageRank (URL)
{
           
            currentURLArray = URL.split('/');   
           
            var pageRankURL = currentURLArray[0] + "//" + currentURLArray[2] + "/";           
            var hashCode = CheckHash(HashURL(pageRankURL));   
            var xmlhttp = new XMLHttpRequest();               
            var baseURL = "http://toolbarqueries.google.com/search?client=navclient-auto&ie=UTF-8&oe=UTF-8&features=Rank&ch=" + hashCode + "&q=info:" + pageRankURL + "&num=100&filter=0";   
           
           xmlhttp.open("GET", baseURL, true);     
           xmlhttp.onreadystatechange = function()
           {
              if (xmlhttp.readyState == 4)
              {                      
                  var response = xmlhttp.responseText;   
                  alert("Page Rank=" + response);               
              }
           }
       
         xmlhttp.send(null); 
       
}

function StrToNum(str, check, magic)
{
  int32Unit = 4294967296; // 2^32

  for (i = 0; i < str.length; i++)
  {
    check *= magic;
    if (check >= int32Unit)
    {
      check = (check - int32Unit * parseInt (check / int32Unit));
      check = (check < -2147483648) ? (check + int32Unit) : check;
    }
    check += str.charCodeAt(i);
  }

  return check;
}


function bitwise_or (op1, op2)
{
  op1 = op1.toString(16);
  op2 = op2.toString(16);

  res = '';
  for (i1 = op1.length - 1, i2 = op2.length-1; i1 >= 0 || i2 >= 0; i1--,i2--) {
    var o1 = 0, o2 = 0;
    if (i1 >= 0) o1 = op1.charAt(i1);
    if (i2 >= 0) o2 = op2.charAt(i2);
    res = (parseInt(o1, 16) | parseInt (o2, 16)).toString(16) + res;
  }

  return parseInt (res, 16);
}


function HashURL(str)
{
  check1 = StrToNum (str, 0x1505, 0x21);
  check2 = StrToNum (str, 0, 0x1003F);

  check1 >>= 2;
  check1 = ((check1 >> 4) & 0x3FFFFC0 ) | (check1 & 0x3F);
  check1 = ((check1 >> 4) & 0x3FFC00 ) | (check1 & 0x3FF);
  check1 = ((check1 >> 4) & 0x3C000 ) | (check1 & 0x3FFF);


  t1 = bitwise_or (bitwise_or((check1 & 0x3C0) << 4, (check1 & 0x3C)) << 2, check2 & 0xF0F);


  t2_1 = parseInt ((((((check1 & 0xFFFFC000) << 4) | (check1 & 0x3C00)) ) * 4).toString (16) + '00', 16);
  t2_2 = (check2 & 0xF0F0000);
  t2   = bitwise_or (t2_1, t2_2);
 
  return bitwise_or (t1, t2);
}


function CheckHash (hashnum)
{
  checkByte = 0;
  flag      = 0;

  hashstr = ""+hashnum; // tostring

  for (i = hashstr.length - 1; i >= 0; i--)
  {
    re = parseInt (hashstr.charAt (i));
    if (1 === (flag % 2))
    {
      re += re;
      re = parseInt(re / 10) + (re % 10);
    }
    checkByte += re;
    flag ++;
  }

  checkByte %= 10;
  if (0 != checkByte)
  {
    checkByte = 10 - checkByte;
    if (1 === (flag % 2) )
    {
      if (1 === (checkByte % 2))
      {
        checkByte += 9;
      }
      checkByte >>= 1;
    }
  }

  return '7' + checkByte + hashstr;
}


Note - Getting page rank using Google is might be illegal as per Google policy. Go with privacy policy of Google before using it.

No comments:

Post a Comment