Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Last active August 29, 2015 13:56
Show Gist options
  • Save mattbrailsford/9001041 to your computer and use it in GitHub Desktop.
Save mattbrailsford/9001041 to your computer and use it in GitHub Desktop.
Disqus
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);
}
}
}
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