The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 22, 2019, 11:57:10 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Google pagerank checksum in C++  (Read 8912 times)
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« on: July 21, 2007, 11:06:53 PM »

I know there are many opinions about pagerank these days but for a project I'm working on I use it as one of several key indicators so don't give me the old "pagerank is worthless" speech  Grin

Your typical pagerank query looks like this;
http://toolbarqueries.google.com/search?sourceid=navclient-ff&features=Rank&client=navclient-auto-ff&googleip=P;209.85.173.147&ch=8ece91f4d&q=info:www.google.com

Now most of this is a no brainer except the "ch=8ece91f4d" bit, that's the checksum. The 8 appears to be static so the actual checksum can be represented as ch=8 + the actual calculated checksum. Now the code to do this is available in PHP and Java form but I needed C++ so I ported bits of both and here is the end result.

The following is a little test rig that I made up (gpr.cpp), the important bit is the getCS() function which returns the checksum to append to "ch=8" in the URL.

Code:
// Written by TheDarkness

// Compile with;
//      g++ -Wall -O2 gpr.cpp -o gpr

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string getCS( string* url )
{
        string* google_hash =
                new string( "Mining PageRank is AGAINST GOOGLE'S TERMS OF SERVICE. Yes, I'm talking to you, scammer." );
        int key = 16909125;
        for( unsigned int i = 0; i < url->length(); ++i )
        {
                key = key ^ (int)google_hash->at( i % google_hash->length() ) ^ (int)url->at( i );

                // BEWARE, possibly non portable, depends on signed/unsigned padding,
                // one's complement, two's complement, etc.
                key = ((unsigned)key)>>23|((unsigned)key)<<9;

        }

        delete google_hash;

        ostringstream oss( "8" );
        oss.setf( ios::hex, ios::basefield );
        oss.width( 2 );
        oss.fill( '0' );
        oss << ( ((unsigned)key)>>(8&255) ) << (key&255);

        return oss.str();
}

int main( void )
{
        string* str = new string( "www.yahoo.com" );
        cout << getCS( str ) << endl;
        (*str) = "www.google.com";
        cout << getCS( str ) << endl;
        delete str;
        return 0;

}

Have fun,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
arms
Expert
****
Offline Offline

Posts: 235



View Profile
« Reply #1 on: July 22, 2007, 06:09:22 PM »

i don't use c++ but you reminded me to look for one in java. then i thought python would be even better. then i found one in python.
thanks.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: July 23, 2007, 08:43:12 AM »

Sweetness TD
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #3 on: September 03, 2007, 07:39:46 AM »

nice one using streams, nice bit of code
use static_cast<unsigned int>(var) instead of (unsigned)
its safer Wink
Logged

I am Tyler Durden
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #4 on: September 03, 2007, 01:16:18 PM »

Not putting you on the spot Monk but can you explain why? I'm a bit foggy in that area.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #5 on: September 04, 2007, 07:42:46 AM »

no worries dude i was just bein a code nazi lol

two reasons, firstly its a safer C++ way of doing things and secondly its damn ugly making you take more notice of why your actually doing the cast.

E.g. unsigned in most compilers will be an unsigned int, but it might not be, making it quite - not for this app really, but in general.

static_cast is used for swapping between basic types, int, unsigned int, char etc
dynamic_cast is for casting up inheritence hierarchies [theres a better way to explain that lol]
and reinterpret_cast casts between 2 completely different types.

there is also const_cast but thats BAAAAD! Smiley

so instead of (class b*) you would do dynamic_cast<class b*>(a)
its just generally good etc n wotnot

hope that helped explain a bit lol Smiley
Logged

I am Tyler Durden
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #6 on: September 05, 2007, 03:20:11 AM »


E.g. unsigned in most compilers will be an unsigned int, but it might not be, making it quite - not for this app really, but in general.


lol, I worked out what you mean eventually  ROFLMAO

Still not sure what the exact difference between (unsigned int) and static_cast<unsigned int> is but I've gone with static_cast<unsigned int> anyway lol
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #7 on: September 05, 2007, 05:52:44 AM »

the method i described is the C++ way of doing casts and is safer to use than (unsigned int)

heh yeah im bad at explaining things, i always miss out crucial bits of information Smiley
Logged

I am Tyler Durden
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #8 on: September 06, 2007, 02:19:59 AM »

Its cool man I appreciate the input. Don't think I don't.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #9 on: September 14, 2007, 04:01:20 PM »

never man its all good Smiley
Logged

I am Tyler Durden
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!