/// <summary> /// Gets the URL to share this post on all available social networks /// </summary> /// <param name="post">The blog post</param> /// <param name="url">Full URL to this post</param> /// <param name="shortUrl">Short URL to this post</param> /// <returns>Sharing URLs for this post</returns> public IEnumerable<KeyValuePair<ISocialNetwork, string>> ShareUrls(PostSummaryModel post, string url, string shortUrl) { foreach (var sharer in _socialShares) { yield return new KeyValuePair<ISocialNetwork, string>(sharer, sharer.GetShareUrl(post, url, shortUrl)); } }
/*public NginxWebCache(UrlHelper urlHelper) { _urlHelper = urlHelper; }*/ /// <summary> /// Clear the cache for this blog post /// </summary> /// <param name="post">The blog post</param> public void ClearCache(PostSummaryModel post) { ClearCache(_urlHelper.BlogPostAbsolute(post)); ClearCache(_urlHelper.ActionAbsolute(MVC.Site.Index())); ClearCache(_urlHelper.ActionAbsolute(MVC.Blog.Index())); // TODO: Clear tags and categories if they're ever cached }
/// <summary> /// Gets the number of times this URL has been shared on this social network. /// </summary> /// <param name="post">The blog post</param> /// <param name="url">Full URL to this post</param> /// <param name="shortUrl">Short URL to this post</param> /// <returns>Share count for this post</returns> public IDictionary<ISocialNetwork, int> ShareCounts(PostSummaryModel post, string url, string shortUrl) { IDictionary<ISocialNetwork, int> results = new Dictionary<ISocialNetwork, int>(_socialShares.Count); foreach (var sharer in _socialShares) { using (MiniProfiler.Current.Step("Sharing count for " + sharer.Name)) { int count; try { count = sharer.GetShareCount(post, url, shortUrl); } catch (Exception ex) { // If an error occured, just set the count to 0 and log it. // These aren't overly important - They shouldn't crash the page! Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Couldn't get social share count for " + sharer.Name, ex)); count = 0; } results.Add(sharer, count); } } return results; }
/// <summary> /// Gets a URL to edit the specified blog post /// </summary> /// <param name="urlHelper">The URL helper.</param> /// <param name="post">Blog post to link to</param> /// <returns>URL to edit this blog post</returns> public static string BlogPostEdit(this UrlHelper urlHelper, PostSummaryModel post) { // Post date needs to be padded with a 0 (eg. "01" for January) - T4MVC doesn't work in this // case because it's strongly-typed (can't pass a string for an int param) //return urlHelper.Action(MVC.Blog.View(post.Date.Month, post.Date.Year, post.Slug)); return urlHelper.Action("Edit", "Blog", new { area = "Admin", month = post.Date.Month.ToString("00"), year = post.Date.Year, slug = post.Slug }); }
/// <summary> /// Gets the URL to share this post on this social network. /// </summary> /// <param name="post">The blog post</param> /// <param name="url">Full URL to this post</param> /// <param name="shortUrl">Short URL to this post</param> /// <returns>Sharing URL for this post</returns> public string GetShareUrl(PostSummaryModel post, string url, string shortUrl) { return SHARE_URL + "?" + new Dictionary<string, object> { {"mini", "true"}, {"url", url}, {"title", post.Title}, {"source", "Daniel15"}, }.ToQueryString(); }
/// <summary> /// Gets the URL to share this post on this social network. /// </summary> /// <param name="post">The blog post</param> /// <param name="url">Full URL to this post</param> /// <param name="shortUrl">Short URL to this post</param> /// <returns>Sharing URL for this post</returns> public string GetShareUrl(PostSummaryModel post, string url, string shortUrl) { return SHARE_URL + "?" + new Dictionary<string, object> { {"text", post.Title}, {"original_referer", url}, {"url", shortUrl}, {"via", "Daniel15"}, {"related", "Daniel15"} }.ToQueryString(); }
/// <summary> /// Gets the number of times this URL has been shared on this social network. /// </summary> /// <param name="post">The blog post</param> /// <param name="url">Full URL to this post</param> /// <param name="shortUrl">Short URL to this post</param> /// <returns>Share count for this post</returns> public int GetShareCount(PostSummaryModel post, string url, string shortUrl) { var countUrl = COUNT_URL + "?" + new Dictionary<string, object> { {"url", url}, }.ToQueryString(); var response = Json.Decode(countUrl.GetJsonFromUrl()); if (response == null || response.count == null) return 0; return Convert.ToInt32(response.count); }
public void BlogPosts() { var post = new PostSummaryModel { Id = 123, Date = new DateTime(2012, 1, 1), Title = "Test Post", Slug = "test-post", Published = true, }; var mocks = new Mocks(); Assert.AreEqual("/blog/2012/01/test-post", BlogUrlHelperExtensions.BlogPost(mocks.UrlHelper, post)); Assert.AreEqual("/blog/2012/01/test-post/edit", BlogUrlHelperExtensions.BlogPostEdit(mocks.UrlHelper, post)); Assert.AreEqual("http://localhost/blog/2012/01/test-post", BlogUrlHelperExtensions.BlogPostAbsolute(mocks.UrlHelper, post)); }
/// <summary> /// Gets the number of times this URL has been shared on this social network. /// </summary> /// <param name="post">The blog post</param> /// <param name="url">Full URL to this post</param> /// <param name="shortUrl">Short URL to this post</param> /// <returns>Share count for this post</returns> public int GetShareCount(PostSummaryModel post, string url, string shortUrl) { var apiUrl = API_COUNT_URL + "?" + new Dictionary<string, object> { {"url", url} }.ToQueryString(); var responseText = apiUrl.GetJsonFromUrl(); // Ugly hack to get JSON data from the JavaScript method call // This API call is usually used client-side via JSONP... responseText = responseText.Replace("IN.Tags.Share.handleCount(", "").Replace(");", ""); var response = Json.Decode(responseText); if (response == null || response.count == null) return 0; return Convert.ToInt32(response.count); }
/// <summary> /// Gets the number of times this URL has been shared on this social network. /// </summary> /// <param name="post">The blog post</param> /// <param name="url">Full URL to this post</param> /// <param name="shortUrl">Short URL to this post</param> /// <returns>Share count for this post</returns> public int GetShareCount(PostSummaryModel post, string url, string shortUrl) { var total = 0; var apiUrl = API_URL + "?" + new Dictionary<string, object> { {"url", url} }.ToQueryString(); var data = Json.Decode(apiUrl.GetJsonFromUrl()); if (data == null || data.data == null || data.data.children == null) return 0; // Need to add up the points in every submission of this URL foreach (var child in data.data.children) { total += Convert.ToInt32(child.data.score); } return total; }
/// <summary> /// Gets the number of times this URL has been shared on this social network. /// </summary> /// <param name="post">The blog post</param> /// <param name="url">Full URL to this post</param> /// <param name="shortUrl">Short URL to this post</param> /// <returns>Share count for this post</returns> public int GetShareCount(PostSummaryModel post, string url, string shortUrl) { // Get the count using FQL. Returns *both* like count and share count. var query = string.Format(LINK_QUERY, url); var queryUrl = QUERY_URL + "?" + new Dictionary<string, object> { {"query", query} }.ToQueryString(); var xml = XDocument.Load(queryUrl); XNamespace ns = "http://api.facebook.com/1.0/"; if (xml.Root == null) return 0; var linkStat = xml.Root.Element(ns + "link_stat"); if (linkStat == null) return 0; var totalCount = linkStat.Element(ns + "total_count"); return totalCount == null ? 0 : Convert.ToInt32(totalCount.Value); }
/// <summary> /// Clear the cache for this blog post /// </summary> /// <param name="post">The blog post</param> public void ClearCache(PostSummaryModel post) { }
/// <summary> /// Gets the URL to share this post on this social network. /// </summary> /// <param name="post">The blog post</param> /// <param name="url">Full URL to this post</param> /// <param name="shortUrl">Short URL to this post</param> /// <returns>Sharing URL for this post</returns> public string GetShareUrl(PostSummaryModel post, string url, string shortUrl) { return SHARE_URL + "?" + new Dictionary<string, object> { {"u", url}, {"t", post.Title}, }.ToQueryString(); }
/// <summary> /// Gets an absolute URL to the specified blog post /// </summary> /// <param name="urlHelper">The URL helper.</param> /// <param name="post">Blog post to link to</param> /// <returns>URL to this blog post</returns> public static string BlogPostAbsolute(this UrlHelper urlHelper, PostSummaryModel post) { return urlHelper.Absolute(urlHelper.BlogPost(post)); }
/// <summary> /// Gets the short URL for this blog post /// </summary> /// <param name="post">Blog post</param> /// <returns>The short URL</returns> public string Shorten(PostSummaryModel post) { return Shorten(post.Id); }