使用delphi计算谷歌工具栏pagerank的校验和

时间:2012-12-26 14:05:01

标签: delphi pagerank

1 个答案:

答案 0 :(得分:5)

此页面声称包含校验和算法:http://automateeverything.tumblr.com/post/19951549637/google-page-rank-bash-script

C中有代码,但是将它移植到Delphi很简单。甚至只需将其编译为.obj并直接链接。

这是我对港口的快速尝试。也许你会发现它很有用。在使用它之前,我当然希望对此进行一些适当的测试。

{$OVERFLOWCHECKS OFF}
program pagerank;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function ConvertStrToInt(pStr: PAnsiChar; Init, Factor: Integer): Integer;
begin
  Result := Init;
  while pStr^<>#0 do
  begin
    Result := Result*Factor;
    inc(Result, ord(pStr^));
    inc(pStr);
  end;
end;

function HashURL(pStr: PAnsiChar): Integer;
var
  C1, C2, T1, T2: Cardinal;
begin
  C1 := ConvertStrToInt(pStr, $1505, $21);
  C2 := ConvertStrToInt(pStr, 0, $1003F);
  C1 := C1 shr 2;
  C1 := ((C1 shr 4) and $3FFFFC0) or (C1 and $3F);
  C1 := ((C1 shr 4) and $3FFC00) or (C1 and $3FF);
  C1 := ((C1 shr 4) and $3C000) or (C1 and $3FFF);

  T1 := (C1 and $3C0) shl 4;
  T1 := T1 or (C1 and $3C);
  T1 := (T1 shl 2) or (C2 and $F0F);

  T2 := (C1 and $FFFFC000) shl 4;
  T2 := T2 or (C1 and $3C00);
  T2 := (T2 shl $A) or (C2 and $F0F0000);

  Result := Integer(T1 or T2);
end;

function CheckHash(HashInt: Cardinal): AnsiChar;
var
  Check, Remainder: Integer;
  Flag: Boolean;
begin
  Check := 0;
  Flag := False;
  repeat
    Remainder := HashInt mod 10;
    HashInt := HashInt div 10;
    if Flag then
    begin
      inc(Remainder, Remainder);
      Remainder := (Remainder div 10) + (Remainder mod 10);
    end;
    inc(Check, Remainder);
    Flag := not Flag;
  until HashInt=0;

  Check := Check mod 10;
  if Check<>0 then
  begin
    Check := 10-Check;
    if Flag then
    begin
      if (Check mod 2)=1 then
        inc(Check, 9);
      Check := Check shr 1;
    end;
  end;
  inc(Check, $30);
  Result := AnsiChar(Check);
end;

function PageRankCheckSum(const URL: string): string;
var
  HashInt: Cardinal;
begin
  HashInt := Cardinal(HashURL(PAnsiChar(AnsiString(URL))));
  Result := Format('7%s%u', [CheckHash(HashInt), HashInt]);
end;

procedure Main;
begin
  if ParamCount<>1 then
  begin
    Writeln(Format('Usage: %s [URL]', 
      [ChangeFileExt(ExtractFileName(ParamStr(0)), '')]));
    exit;
  end;

  Writeln('Checksum='+PageRankCheckSum(ParamStr(1)));
end;

begin
  try
    Main;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.