/// <summary> /// Try to extract the EditUri (RSD file URI) from the passed DOM /// </summary> /// <param name="weblogDOM"></param> /// <returns></returns> private static string ExtractEditUri(string homepageUrl, BlogServiceDetectorBase.HomepageDom weblogDOM) { HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(weblogDOM.Body); // // look in the first HEAD tag // IHTMLElementCollection headElements = ((IHTMLDocument3)weblogDOM).getElementsByTagName( "HEAD" ) ; //if ( headElements.length > 0 ) //{ // // get the first head element // IHTMLElement2 firstHeadElement = (IHTMLElement2)headElements.item(0,0); // look for link tags within the head var linkNodes = htmlDoc.DocumentNode.Descendants("head")?.SingleOrDefault()?.Descendants("link"); if (linkNodes != null) { foreach (var element in linkNodes) { var linkElementRel = element.GetAttributeValue("rel", null); if (linkElementRel != null) { if (linkElementRel.ToLowerInvariant() == "edituri") { var hrefValue = element.GetAttributeValue("href", null); if (!string.IsNullOrWhiteSpace(hrefValue)) { return(UrlHelper.UrlCombineIfRelative(homepageUrl, hrefValue)); } } } // IHTMLLinkElement linkElement = element as IHTMLLinkElement ; //if ( linkElement != null ) //{ // string linkRel = linkElement.rel ; // if ( linkRel != null && (linkRel.ToUpperInvariant().Equals("EDITURI") ) ) // { // return UrlHelper.UrlCombineIfRelative(homepageUrl, linkElement.href); // } //} //} } } // couldn't find one return(null); }
public static async Task <RsdServiceDescription> DetectFromWeblogAsync(string homepage, BlogServiceDetectorBase.HomepageDom weblogDOM) { try { // get the edit uri string rsdFileUrl = ExtractEditUri(homepage, weblogDOM); // do the detection if (rsdFileUrl != null) { return(await DetectFromRsdUrl(rsdFileUrl)); } else { return(null); } } catch (Exception ex) { //Trace.Fail( "Unexpected error attempting to detect Blog service: " + ex.ToString() ); return(null); } }