Last active
August 29, 2015 13:56
-
-
Save mattbrailsford/9001041 to your computer and use it in GitHub Desktop.
Disqus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using TheOutfield.Website.XsltExtensions; | |
namespace TheOutfield.Website.Controllers | |
{ | |
public class DisqusController : Controller | |
{ | |
[HttpGet] | |
[OutputCache(Duration = 300, VaryByParam = "threadId")] | |
public JsonResult GetCommentCount(int threadId) | |
{ | |
return Json(DisqusExtensions.GetCommentCount(threadId), "text/html", JsonRequestBehavior.AllowGet); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using TheOutfield.Website.Extensions; | |
using umbraco; | |
namespace TheOutfield.Website.XsltExtensions | |
{ | |
[XsltExtension("Disqus")] | |
public class DisqusExtensions | |
{ | |
public static int GetCommentCount(int threadId) | |
{ | |
var url = string.Format("https://disqus.com/api/3.0/threads/details.json?forum={0}&thread:ident={1}&api_secret={2}", | |
ConfigurationManager.AppSettings["disqusForumShortName"], | |
threadId, | |
ConfigurationManager.AppSettings["disqusApiSecret"]); | |
try | |
{ | |
using (var webClient = new System.Net.WebClient()) | |
{ | |
var jsonString = webClient.DownloadString(url); | |
var jsonObj = jsonString.DeserializeJsonTo<JsonResponse<ThreadDetailsResponse>>(); | |
return jsonObj.response.posts; | |
} | |
} | |
catch { } | |
return 0; | |
} | |
} | |
public class JsonResponse<T> | |
{ | |
public int code { get; set; } | |
public T response { get; set; } | |
} | |
public class ThreadDetailsResponse | |
{ | |
public int posts { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment