/// <summary> /// Parse out an article by URL and return a strongly typed object /// </summary> /// <param name="url"></param> /// <returns></returns> public DiffBotArticleResultModel Article( string url, bool useHtml = false ) { var model = new DiffBotArticleResultModel( ); System.Uri uri; if (!IsValidUrl( url, out uri )) // no need to waste cycles on an incorrect url return model; try { var source = GetWebClient().DownloadString( GetEndpointUrl( EndPoints.Article, url, useHtml ) ); model = model.Create( source ); } catch // if it fails, try using a backup regex { var html = DownloadUrlAsString( url ); if (!string.IsNullOrWhiteSpace( url )) { model.title = GetTitleFromHtmlString( html ); } } return model; }
/// <summary> /// Gets a basic 300 word preview for this article, or if this is a video, returns the proper embed tag for the video contents /// </summary> /// <param name="article"></param> /// <returns></returns> public string ArticleGetPreview( DiffBotArticleResultModel article ) { var description = ""; // check for videos if (article.media.Count > 0) { foreach( var media in article.media ) { if ( string.IsNullOrEmpty(description) && media.type == "video" && (media.primary || article.media.Count == 1 ) ) { description = string.Format( VideoYouTubeTemplate, media.link ); continue; } } } if (string.IsNullOrWhiteSpace( article.text )) // don't parse if there's no text return description; var end = article.text.Length < 300 ? article.text.Length : 300; return description + "\n" + article.text.Substring( 0, end ); }
public JsonResult Article( string url, bool? html ) { var diffbot = new DiffbotAPI( ); var source = new WebClient( ).DownloadString ( diffbot.GetEndpointUrl ( DiffbotAPI.EndPoints.Article, url, html ?? false ) ); var model = new DiffBotArticleResultModel().Create( source ); return Json( model, JsonRequestBehavior.AllowGet ); }