📄 pr.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
/// PageRank tool in C# (converted from PHP)
/// Aaron Reynolds of http://www.aaronreynolds.co.uk
namespace PageRank
{
public class TGooglePR
{
/// <summary>
/// Gets the actual PR response from Google
/// </summary>
/// <param name="_G_URL">Full Request URL for the PR</param>
/// <returns>Google's PR response</returns>
private string GetResponse(string _G_URL)
{
try
{
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
HttpWebRequest _HttpWRQ = (HttpWebRequest)WebRequest.Create(_G_URL);
HttpWebResponse _HttpWRS = (HttpWebResponse)_HttpWRQ.GetResponse();
Stream _InStream = _HttpWRS.GetResponseStream();
string _TempString = null;
int _Count = 0;
do
{
_Count = _InStream.Read(buf, 0, buf.Length);
if (_Count != 0)
{
_TempString = Encoding.ASCII.GetString(buf);
sb.Append(_TempString);
}
}
while (_Count > 0);
return sb.ToString();
}
catch (NotSupportedException nse)
{
throw nse;
}
catch (WebException we)
{
throw we;
}
catch (ProtocolViolationException pve)
{
throw pve;
}
catch (InvalidOperationException ioe)
{
throw ioe;
}
catch (Exception ex)
{
return "0";
}
}
/// <summary>
/// Returns a hash-string from the site's URL
/// </summary>
/// <param name="_SiteURL">full URL as indexed by Google</param>
/// <returns>HASH for site as a string</returns>
private string GetHash(string _SiteURL)
{
try
{
long _Check1 = StrToNum(_SiteURL, 0x1505, 0x21);
long _Check2 = StrToNum(_SiteURL, 0, 0x1003F);
_Check1 >>= 2;
_Check1 = ((_Check1 >> 4) & 0x3FFFFC0) | (_Check1 & 0x3F);
_Check1 = ((_Check1 >> 4) & 0x3FFC00) | (_Check1 & 0x3FF);
_Check1 = ((_Check1 >> 4) & 0x3C000) | (_Check1 & 0x3FFF);
long T1 = ((((_Check1 & 0x3C0) << 4) | (_Check1 & 0x3C)) << 2) | (_Check2 & 0xF0F);
long T2 = ((((_Check1 & 0xFFFFC000) << 4) | (_Check1 & 0x3C00)) << 0xA) | (_Check2 & 0xF0F0000);
return Convert.ToString(T1 | T2);
}
catch
{
return "0";
}
}
/// <summary>
/// Checks the HASH-string returned and adds check numbers as necessary
/// </summary>
/// <param name="_HashNum">generated HASH-string</param>
/// <returns>modified HASH-string</returns>
private string CheckHash(string _HashNum)
{
try
{
long _CheckByte = 0;
long _Flag = 0;
long _tempI = Convert.ToInt64(_HashNum);
if (_tempI < 0)
{
_tempI = _tempI * (-1);
}
string _Hash = _tempI.ToString();
int _Length = _Hash.Length;
for (int x = _Length - 1; x >= 0; x--)
{
char _quick = _Hash[x];
long _Re = Convert.ToInt64(_quick.ToString());
if (1 == (_Flag % 2))
{
_Re += _Re;
_Re = (long)((_Re / 10) + (_Re % 10));
}
_CheckByte += _Re;
_Flag++;
}
_CheckByte %= 10;
if (0 != _CheckByte)
{
_CheckByte = 10 - _CheckByte;
if (1 == (_Flag % 2))
{
if (1 == (_CheckByte % 2))
{
_CheckByte >>= 1;
}
}
}
if (_Hash.Length == 9)
{
_CheckByte += 5;
}
return "7" + _CheckByte.ToString() + _Hash;
}
catch
{
return "0";
}
}
/// <summary>
/// Converts the string (site URL) into numbers for the HASH
/// </summary>
/// <param name="_str">Site URL as passed by GetHash()</param>
/// <param name="_Chk">Necessary passed value</param>
/// <param name="_Magic">Necessary passed value</param>
/// <returns>Long Integer manipulation of string passed</returns>
private long StrToNum(string _str, long _Chk, long _Magic)
{
try
{
long _Int64Unit = Convert.ToInt64(Math.Pow(2, 32));
int _StrLen = _str.Length;
for (int x = 0; x < _StrLen; x++)
{
_Chk *= _Magic;
if (_Chk >= _Int64Unit)
{
_Chk = (_Chk - (_Int64Unit * Convert.ToInt64(_Chk / _Int64Unit)));
_Chk = (_Chk < -2147483648) ? (_Chk + _Int64Unit) : _Chk;
}
_Chk += (long)_str[x];
}
}
catch { }
return _Chk;
}
private string _GooglePR_URL = "http://toolbarqueries.google.com/search?client=navclient-auto&ie=UTF-8&oe=UTF-8&features=Rank:&q=info:";
/// <summary>
/// Has two purposes:
/// 1. Can be used before search to specify a different address for the Google PR link
/// 2. Can be used after search to return a perma-link to the PR for the specified site
/// </summary>
public string GooglePR_URL
{
get { return _GooglePR_URL; }
set { _GooglePR_URL = value; }
}
/// <summary>
/// Returns the GooglePR as a string 0-10
/// </summary>
/// <param name="_SiteURL">Site URL as indexed in Google</param>
/// <returns>PageRank value 0-10 as a string</returns>
public string ReturnPageRank(string _SiteURL)
{
string _PR = "0";
try
{
_SiteURL = CleanURL(_SiteURL);
string _HashedURL = CheckHash(GetHash(_SiteURL));
_GooglePR_URL += _SiteURL + "&ch=" + _HashedURL;
// Open a GET
string _Response = GetResponse(_GooglePR_URL);
string _PageRank = null;
if (_Response != null)
{
string[] _Parts = _Response.Split(":".ToCharArray(), 3, StringSplitOptions.RemoveEmptyEntries);
try
{
_PageRank = _Parts[2].Replace("\r", "").Replace("\n", "").Replace("\0", "").Trim();
if (_PageRank != null)
{
if (_PageRank.Length > 2)
{
_PageRank = _PageRank.Substring(0, 2);
}
}
}
catch
{
_PageRank = null;
}
}
if (_PageRank == null)
{
_PR = "0";
}
else if (_PageRank == "")
{
_PR = "0";
}
else
{
_PR = _PageRank;
}
}
catch
{
_PR = "0";
}
return _PR;
}
private string CleanURL(string _SiteURL)
{
int _iAmp = _SiteURL.IndexOf('&');
if (_iAmp != -1 && _iAmp > 0)
{
_SiteURL = _SiteURL.Substring(0, _iAmp);
}
return _SiteURL;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -