From 4474daaa6d1c8a37bb7a8e5b48e05a755b15e7b2 Mon Sep 17 00:00:00 2001 From: "pa@pcinpact.com" Date: Wed, 12 Jun 2013 15:55:22 +0200 Subject: [PATCH 1/3] Adding ASP.NET Handler Same purpose as sharrre.php but for ASP.NET applications --- ASP.NET/README-ASPNET.txt | 32 +++++++++++ ASP.NET/Sharrre.ashx | 1 + ASP.NET/Sharrre.ashx.cs | 116 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 ASP.NET/README-ASPNET.txt create mode 100644 ASP.NET/Sharrre.ashx create mode 100644 ASP.NET/Sharrre.ashx.cs diff --git a/ASP.NET/README-ASPNET.txt b/ASP.NET/README-ASPNET.txt new file mode 100644 index 0000000..61702d2 --- /dev/null +++ b/ASP.NET/README-ASPNET.txt @@ -0,0 +1,32 @@ +A TINY TUTORIAL +---------------- + +NB : You can use Sharrre.ashx in any ASP.NET applications (MVC or WebPages) + +ASP.NET Part +-------------- + +1) Create a folder "Handlers" in your project (but you can name it whatever you want) +2) Add Sharrre.ashx in this folder +3) If you're using MVC 4, add routes.IgnoreRoute("{resource}.ashx/{*pathInfo}"); in your RouteConfig.cs +4) You'll need two NuGet packages : HtmlAgilityPack and JSON.NET (JSON.NET is now included in all ASP.NET MVC 4 applications) + + +jQuery Configuration Part +------------------------- + +1) Insert your share JS files as usual +2) Specify "curlUrl". Ex : +$('#share').sharrre({ + share: { + googlePlus: true, + facebook: true, + twitter: true + }, + url: 'http://sharrre.com/', + urlCurl: '/Handlers/Sharrre.ashx' +}); + +3) Have a coffee + + diff --git a/ASP.NET/Sharrre.ashx b/ASP.NET/Sharrre.ashx new file mode 100644 index 0000000..e70b869 --- /dev/null +++ b/ASP.NET/Sharrre.ashx @@ -0,0 +1 @@ +<%@ WebHandler Language="C#" CodeBehind="Sharrre.ashx.cs" Class="Lidd.Handlers.Sharrre" %> diff --git a/ASP.NET/Sharrre.ashx.cs b/ASP.NET/Sharrre.ashx.cs new file mode 100644 index 0000000..e937377 --- /dev/null +++ b/ASP.NET/Sharrre.ashx.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Web; +using System.Xml; +using HtmlAgilityPack; +using Newtonsoft.Json; + +namespace Lidd.Handlers +{ + /// + /// ASP.NET Version of sharrre.php. Please view README-ASPNET.txt. + /// + public class Sharrre : IHttpHandler + { + + private Dictionary resultDictionary; + + public Sharrre() + : base() + { + resultDictionary = new Dictionary(); + resultDictionary.Add("url", string.Empty); + resultDictionary.Add("count", 0); + } + + public void ProcessRequest(HttpContext context) + { + string urlParameter = context.Request.QueryString["url"]; + string typeParameter = context.Request.QueryString["type"]; + + resultDictionary["url"] = urlParameter; + + Uri baseUri; + if (Uri.TryCreate(urlParameter, UriKind.Absolute, out baseUri)) + { + switch (typeParameter) + { + case "googlePlus": + GetGoogleInfo(baseUri); + break; + case "pinterest": + GetPinterestInfo(baseUri); + break; + case "stumbleupon": + GetStumbleInfo(baseUri); + break; + default: + throw new NotImplementedException(string.Format("Can't get informations for {0}", typeParameter)); + } + + + } + + var contentResult = JsonConvert.SerializeObject(resultDictionary); + context.Response.ContentType = "application/json"; + context.Response.Write(contentResult); + + } + + private void GetStumbleInfo(Uri baseUri) + { + var stumbleObject = GetJsonObject(string.Format("http://www.stumbleupon.com/services/1.01/badge.getinfo?url={0}", WebUtility.UrlEncode(baseUri.AbsoluteUri))); + resultDictionary["count"] = stumbleObject.result.views; + } + + private void GetPinterestInfo(Uri baseUri) + { + var pinObject = GetJsonObject(string.Format("http://api.pinterest.com/v1/urls/count.json?callback=&url={0}", WebUtility.UrlEncode(baseUri.AbsoluteUri)), PinterestJsonTransformer); + resultDictionary["count"] = pinObject.count; + } + + private string PinterestJsonTransformer(string baseJson) + { + return baseJson.Replace("(", string.Empty).Replace(")", string.Empty); + } + private void GetGoogleInfo(Uri baseUri) + { + var htmlDocument = GetHtmlDocument(string.Format("https://plusone.google.com/u/0/_/+1/fastbutton?url={0}&count=true", WebUtility.UrlEncode(baseUri.AbsoluteUri))); + var node = htmlDocument.DocumentNode.SelectSingleNode("//div[@id='aggregateCount']"); + if (node != null) + { + resultDictionary["count"] = node.InnerText.Replace(">", string.Empty); + } + } + + private HtmlDocument GetHtmlDocument(string path) + { + + var gplusUrl = string.Format(path); + var client = new HtmlWeb(); + var htmlDocument = client.Load(gplusUrl); + + return htmlDocument; + } + + private dynamic GetJsonObject(string path, Func jsonPretransformation = null) + { + WebClient webClient = new WebClient(); + var result = webClient.DownloadString(path); + if (jsonPretransformation != null) + { + result = jsonPretransformation.Invoke(result); + } + return JsonConvert.DeserializeObject(result); + } + public bool IsReusable + { + get + { + return false; + } + } + } +} \ No newline at end of file From 55315bdb3e257f66e0febfdcf9c4013c7ac10dc8 Mon Sep 17 00:00:00 2001 From: "pa@pcinpact.com" Date: Wed, 12 Jun 2013 16:02:23 +0200 Subject: [PATCH 2/3] Adding some try catch blocks --- ASP.NET/Sharrre.ashx.cs | 56 +++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/ASP.NET/Sharrre.ashx.cs b/ASP.NET/Sharrre.ashx.cs index e937377..183afb2 100644 --- a/ASP.NET/Sharrre.ashx.cs +++ b/ASP.NET/Sharrre.ashx.cs @@ -62,13 +62,28 @@ public void ProcessRequest(HttpContext context) private void GetStumbleInfo(Uri baseUri) { var stumbleObject = GetJsonObject(string.Format("http://www.stumbleupon.com/services/1.01/badge.getinfo?url={0}", WebUtility.UrlEncode(baseUri.AbsoluteUri))); - resultDictionary["count"] = stumbleObject.result.views; + try + { + resultDictionary["count"] = stumbleObject.result.views; + } + catch (Exception) + { + resultDictionary["count"] = 0; + } } - private void GetPinterestInfo(Uri baseUri) + private void GetPinterestInfo(Uri baseUri) { var pinObject = GetJsonObject(string.Format("http://api.pinterest.com/v1/urls/count.json?callback=&url={0}", WebUtility.UrlEncode(baseUri.AbsoluteUri)), PinterestJsonTransformer); - resultDictionary["count"] = pinObject.count; + try + { + resultDictionary["count"] = pinObject.count; + } + catch (Exception) + { + + resultDictionary["count"] = 0; + } } private string PinterestJsonTransformer(string baseJson) @@ -87,23 +102,38 @@ private void GetGoogleInfo(Uri baseUri) private HtmlDocument GetHtmlDocument(string path) { + try + { + var gplusUrl = string.Format(path); + var client = new HtmlWeb(); + var htmlDocument = client.Load(gplusUrl); - var gplusUrl = string.Format(path); - var client = new HtmlWeb(); - var htmlDocument = client.Load(gplusUrl); - - return htmlDocument; + return htmlDocument; + } + catch (Exception ex) + { + System.Diagnostics.Trace.TraceError(string.Format("Exception occur in Sharrre handler (GetHtmlDocument) : {0}", ex.ToString())); + return new HtmlDocument(); + } } private dynamic GetJsonObject(string path, Func jsonPretransformation = null) { - WebClient webClient = new WebClient(); - var result = webClient.DownloadString(path); - if (jsonPretransformation != null) + try + { + WebClient webClient = new WebClient(); + var result = webClient.DownloadString(path); + if (jsonPretransformation != null) + { + result = jsonPretransformation.Invoke(result); + } + return JsonConvert.DeserializeObject(result); + } + catch (Exception ex) { - result = jsonPretransformation.Invoke(result); + System.Diagnostics.Trace.TraceError(string.Format("Exception occur in Sharrre handler (GetJsonObject) : {0}", ex.ToString())); + return JsonConvert.DeserializeObject("{}"); } - return JsonConvert.DeserializeObject(result); } public bool IsReusable { From dbf4e5dd6cce840135cf681b21e506331c014107 Mon Sep 17 00:00:00 2001 From: Pierre-Alain David Date: Wed, 12 Mar 2014 09:43:15 +0100 Subject: [PATCH 3/3] Update Sharrre.ashx.cs Namespace Changes --- ASP.NET/Sharrre.ashx.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ASP.NET/Sharrre.ashx.cs b/ASP.NET/Sharrre.ashx.cs index 183afb2..a1870ba 100644 --- a/ASP.NET/Sharrre.ashx.cs +++ b/ASP.NET/Sharrre.ashx.cs @@ -7,7 +7,7 @@ using HtmlAgilityPack; using Newtonsoft.Json; -namespace Lidd.Handlers +namespace Sharrre.Handlers { /// /// ASP.NET Version of sharrre.php. Please view README-ASPNET.txt. @@ -143,4 +143,4 @@ public bool IsReusable } } } -} \ No newline at end of file +}