15 Ocak 2015

Dediğim gibi sadece kod. Verdiğiniz URL'nin google page rank değerini alabilirsiniz.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;

namespace ForFeed.Common
{
    public class SitePopularity
    {
        public class GooglePageRank
        {
            /// 
            /// Returns the PageRank of the given URL. Return values are 0 through 10 or
            /// -1 (N/A), which indicates there was an error or the URL is not in the
            /// Google index.
            /// 
            /// URL to test
            /// 
            public static int GetPageRank(string url)
            {
                var rank = -1;

                try
                {
                    url = String.Format("http://toolbarqueries.google.com/tbr?client=navclient-auto&features=Rank&ch={0}&q=info:{1}", ComputeHash(url), UrlEncode(url));

                    var request = (HttpWebRequest)WebRequest.Create(url);
                    var stream = new StreamReader(request.GetResponse().GetResponseStream());
                    string response = stream.ReadToEnd();

                    string[] arr = response.Split(':');
                    if (arr.Length == 3)
                        rank = Int32.Parse(arr[2]);
                }
                catch (Exception ex)
                {
                }
                return rank;
            }

            /// 
            /// URL-encodes the given URL. Handy when HttpUtility is not available
            /// 
            /// URL to encode
            /// 
            private static string UrlEncode(string url)
            {
                var builder = new StringBuilder();

                foreach (var c in url)
                {
                    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
                        builder.Append(c);
                    else if (c == ' ')
                        builder.Append('+');
                    else if ("()*-_.!".IndexOf(c) >= 0)
                        builder.Append(c);
                    else
                        builder.AppendFormat("%{0:X2}", (byte)c);
                }
                return builder.ToString();
            }

            /// 
            /// Computes a hash value required by Google
            /// 
            private static string ComputeHash(string url)
            {
                UInt32 b;
                UInt32 c = 0xE6359A60;
                int k = 0;

                // Modify URL
                url = String.Format("info:{0}", url);

                uint a = b = 0x9E3779B9;
                int len = url.Length;

                while (len >= 12)
                {
                    a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
                    b += (UInt32)(url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
                    c += (UInt32)(url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));
                    Mix(ref a, ref b, ref c);
                    k += 12;
                    len -= 12;
                }

                c += (UInt32)url.Length;
                switch (len)
                {
                    case 11:
                        c += (UInt32)(url[k + 10] << 24);
                        goto case 10;
                    case 10:
                        c += (UInt32)(url[k + 9] << 16);
                        goto case 9;
                    case 9:
                        c += (UInt32)(url[k + 8] << 8);
                        goto case 8;
                    case 8:
                        b += (UInt32)(url[k + 7] << 24);
                        goto case 7;
                    case 7:
                        b += (UInt32)(url[k + 6] << 16);
                        goto case 6;
                    case 6:
                        b += (UInt32)(url[k + 5] << 8);
                        goto case 5;
                    case 5:
                        b += url[k + 4];
                        goto case 4;
                    case 4:
                        a += (UInt32)(url[k + 3] << 24);
                        goto case 3;
                    case 3:
                        a += (UInt32)(url[k + 2] << 16);
                        goto case 2;
                    case 2:
                        a += (UInt32)(url[k + 1] << 8);
                        goto case 1;
                    case 1:
                        a += url[k + 0];
                        break;
                }
                Mix(ref a, ref b, ref c);
                return String.Format("6{0}", c);
            }

            /// 
            /// ComputeHash() helper method
            /// 
            private static void Mix(ref UInt32 a, ref UInt32 b, ref UInt32 c)
            {
                a -= b; a -= c; a ^= c >> 13;
                b -= c; b -= a; b ^= a << 8;
                c -= a; c -= b; c ^= b >> 13;
                a -= b; a -= c; a ^= c >> 12;
                b -= c; b -= a; b ^= a << 16;
                c -= a; c -= b; c ^= b >> 5;
                a -= b; a -= c; a ^= c >> 3;
                b -= c; b -= a; b ^= a << 10;
                c -= a; c -= b; c ^= b >> 15;
            }
        }

        public static class MultiDimensionalArrayExtensions
        {
            /// 
            ///   Orders the two dimensional array by the provided key in the key selector.
            /// 
            /// The type of the source two-dimensional array.
            /// The source two-dimensional array.
            /// The selector to retrieve the column to sort on.
            /// A new two dimensional array sorted on the key.
            public static T[,] OrderBy(this T[,] source, Func keySelector)
            {
                return source.ConvertToSingleDimension().OrderBy(keySelector).ConvertToMultiDimensional();
            }

            /// 
            ///   Orders the two dimensional array by the provided key in the key selector in descending order.
            /// 
            /// The type of the source two-dimensional array.
            /// The source two-dimensional array.
            /// The selector to retrieve the column to sort on.
            /// A new two dimensional array sorted on the key.
            public static T[,] OrderByDescending(this T[,] source, Func keySelector)
            {
                return source.ConvertToSingleDimension().
                  OrderByDescending(keySelector).ConvertToMultiDimensional();
            }

            /// 
            ///   Converts a two dimensional array to single dimensional array.
            /// 
            /// The type of the two dimensional array.
            /// The source two dimensional array.
            /// The repackaged two dimensional array as a single dimension based on rows.
            private static IEnumerable ConvertToSingleDimension(this T[,] source)
            {
                for (int row = 0; row < source.GetLength(0); ++row)
                {
                    var arRow = new T[source.GetLength(1)];

                    for (int col = 0; col < source.GetLength(1); ++col)
                        arRow[col] = source[row, col];

                    yield return arRow;
                }
            }

            /// 
            ///   Converts a collection of rows from a two dimensional array back into a two dimensional array.
            /// 
            /// The type of the two dimensional array.
            /// The source collection of rows to convert.
            /// The two dimensional array.
            private static T[,] ConvertToMultiDimensional(this IEnumerable source)
            {
                var arrayOfArray = source.ToArray();
                int numberofColumns = (arrayOfArray.Length > 0) ? arrayOfArray[0].Length : 0;
                var twoDimensional = new T[arrayOfArray.Length, numberofColumns];

                for (int row = 0; row < arrayOfArray.GetLength(0); ++row)
                    for (int col = 0; col < numberofColumns; ++col)
                        twoDimensional[row, col] = arrayOfArray[row][col];

                return twoDimensional;
            }
        }

        public int GetAlexaRank(string url)
        {
            try
            {
                var doc = XDocument.Load(url);
                var country = doc.XPathSelectElement("//COUNTRY[@RANK]");
                if (country == null)
                    return 0;

                return (int)country.Attribute("RANK");
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}

Tagged: ,

0 yorum:

Yorum Gönder