In my work with SeoTools I’m adding a Excel function for retreiving a page’s Google+ count (on request by @richardbaxterseo) and I found an excellent implementation in PHP.

In the spirit of sharing, here’s a port in C#:

private static Regex REGEX_GETURLCOUNT =
    new Regex(@"<div[^>]+id=""aggregateCount""[^>]+>(\d*)</div>");

public static int GetUrlCount(string url)
{
    string fetchUrl =
        "https://plusone.google.com/u/0/_/+1/fastbutton?url="+
        HttpUtility.UrlEncode(url)+"&count=true";
    HttpWebRequest request =
        (HttpWebRequest)WebRequest.Create(fetchUrl);
    string response = new StreamReader(request.GetResponse()
        .GetResponseStream()).ReadToEnd();
    Match match = REGEX_GETURLCOUNT.Match(response);
    if (match.Success)
    {
        return int.Parse(match.Groups[1].Value);
    }
    return 0;
}

And while I’m at it, here’s how to get the number of times an url has been tweeted:

private static Regex REGEX_GETURLCOUNT =
    new Regex(@"(\d+)");

public static int GetUrlCount(string url)
{
    string fetchUrl = "http://api.tweetmeme.com/url_info?url=" +
        HttpUtility.UrlEncode(url);

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

    Match match = REGEX_GETURLCOUNT.Match(response);
    if (match.Success)
    {
        return int.Parse(match.Groups[1].Value);
    }
    return 0;
}
Tagged with:
 

One Response to Retreive Google+ count using C#

  1. Domlia says:

    Thanks for sharing. Good work!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>