1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pagerank Retrieval

Discussion in 'General Programming Chat' started by herron, Mar 10, 2010.

  1. herron

    herron Regular Member

    Joined:
    Apr 7, 2009
    Messages:
    217
    Likes Received:
    98
    Does anyone know of an updated script (any language) that retrieves the Google PageRank? Everything I have found online is before the latest update which seemed to have rendered the last method useless.
     
  2. WorkingDog

    WorkingDog Newbie

    Joined:
    Mar 8, 2010
    Messages:
    15
    Likes Received:
    13
    I dunno if this works, and even if it does I suspect Google will block your IP if you overuse it:

    http://www.pagerankcode.com/download-script.html
     
  3. herron

    herron Regular Member

    Joined:
    Apr 7, 2009
    Messages:
    217
    Likes Received:
    98
    Nah, I am aware of that script (and many that use the same technique) but it unfortunately doesn't work for all urls now. I have read that the method that script uses was changed around Sept 2009.

    I realise the IP situation but that is avoidable. All of the Firefox extensions and Keyword Tools like Market Samurai are still doing it, I just have to work out how :p
     
  4. grazor

    grazor Registered Member

    Joined:
    Apr 14, 2009
    Messages:
    57
    Likes Received:
    19
    I've got the same problem right now. I'm considering writing a scraper to collect seo4firefox data, but that seems pretty ghetto.
     
  5. herron

    herron Regular Member

    Joined:
    Apr 7, 2009
    Messages:
    217
    Likes Received:
    98
    Grazor, I think I got it working. I found the solution in this awesome piece of open source software - http://sourceforge.net/projects/seopanel/

    You will find the functions for calculating the checksum in seopanel/controllers/rank.ctrl.php

    I will check it properly tonight but it was returning PR for pages that were returning N/A in the previous code.

    That software has a heap of features for any SEO scripts you need to write.
     
  6. herron

    herron Regular Member

    Joined:
    Apr 7, 2009
    Messages:
    217
    Likes Received:
    98
    Please don't pick on my dodgy code hehe.

    Code:
    <?php
    
    function getPageRank($url){
        $pagerank = -1;
        $fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
        if($fp){
          $out = "GET /search?client=navclient-auto&ch=".CheckHash(hashURL($url))."&features=Rank&q=info:".$url."&num=100&filter=0 HTTP/1.1\r\n";
          $out .= "Host: www.google.com\r\n";
          $out .= "Connection: Close\r\n\r\n";
          fwrite($fp, $out);
          while (!feof($fp)){
            $data = fgets($fp, 128);
            $pos = strpos($data, "Rank_");
            if($pos === false){
            }else
              $pagerank = substr($data, $pos + 9);
          }
          fclose($fp);
        }
        return $pagerank;
    }
    function strToNum($Str, $Check, $Magic) 
    {
        $Int32Unit = 4294967296;
        $length = strlen($Str);
        for ($i = 0; $i < $length; $i++) {
            $Check *= $Magic;
            if ($Check >= $Int32Unit) {
                $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
                $Check = ($Check < -2147483648)? ($Check + $Int32Unit) : $Check;
            }
            $Check += ord($Str{$i});
        }
        return $Check;
    }
    
    function hashURL($String) 
    {
        $Check1 = strToNum($String, 0x1505, 0x21);
        $Check2 = strToNum($String, 0, 0x1003F);
    
        $Check1 >>= 2;
        $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
        $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
        $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);
    
        $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
        $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
    
        return ($T1 | $T2);
    }
    
    function checkHash($Hashnum) {
        $CheckByte = 0;
        $Flag = 0;
    
        $HashStr = sprintf('%u', $Hashnum) ;
        $length = strlen($HashStr);
    
        for ($i = $length - 1; $i >= 0; $i --) {
            $Re = $HashStr{$i};
            if (1 === ($Flag % 2)) {
                $Re += $Re;
                $Re = (int)($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;}
    
        
        
        echo 'Testing Pagerank<br>';
        echo '-----------------------------<br>';
        $site = 'http://www.google.com';    
        $pageRank = getPageRank($site);
        echo 'PR: ' . $pageRank;
    ?>
     
  7. grazor

    grazor Registered Member

    Joined:
    Apr 14, 2009
    Messages:
    57
    Likes Received:
    19
    Hey Herron, thanks for the code, I'll take a look at it tomorrow.
     
  8. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front