Beispiel #1
0
        /// <summary>
        /// Returns the absolute url of a link on a page. If you got the feed links via
        /// GetFeedUrlsFromUrl(url) and the url is relative, you can use this method to get the full url.
        /// </summary>
        /// <param name="pageUrl">the original url to the page</param>
        /// <param name="feedLink">a referenced feed (link)</param>
        /// <returns>a feed link</returns>
        /// <example>GetAbsoluteFeedUrl("codehollow.com", myRelativeFeedLink);</example>
        public static HtmlFeedLink GetAbsoluteFeedUrl(string pageUrl, HtmlFeedLink feedLink)
        {
            string tmpUrl = feedLink.Url.HtmlDecode();

            pageUrl = GetAbsoluteUrl(pageUrl);

            if (tmpUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
                tmpUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
            {
                return(feedLink);
            }

            if (tmpUrl.StartsWith("//", StringComparison.OrdinalIgnoreCase)) // special case
            {
                tmpUrl = "http:" + tmpUrl;
            }

            if (Uri.TryCreate(tmpUrl, UriKind.RelativeOrAbsolute, out Uri finalUri))
            {
                if (finalUri.IsAbsoluteUri)
                {
                    return(new HtmlFeedLink(feedLink.Title.HtmlDecode(), finalUri.ToString(), feedLink.FeedType));
                }
                else if (Uri.TryCreate(pageUrl + '/' + tmpUrl.TrimStart('/'), UriKind.Absolute, out finalUri))
                {
                    return(new HtmlFeedLink(feedLink.Title.HtmlDecode(), finalUri.ToString(), feedLink.FeedType));
                }
            }

            throw new UrlNotFoundException($"Could not get the absolute url out of {pageUrl} and {feedLink.Url}");
        }
Beispiel #2
0
        /// <summary>
        /// Returns a HtmlFeedLink object from a linktag (link href="" type="")
        /// only support application/rss and application/atom as type
        /// if type is not supported, null is returned
        /// </summary>
        /// <param name="input">link tag, e.g. &lt;link rel="alternate" type="application/rss+xml" title="codehollow &gt; Feed" href="https://codehollow.com/feed/" /&gt;</param>
        /// <returns>Parsed HtmlFeedLink</returns>
        public static HtmlFeedLink GetFeedLinkFromLinkTag(string input)
        {
            string linkTag = input.HtmlDecode();
            string type    = GetAttributeFromLinkTag("type", linkTag).ToLower();

            if (!type.Contains("application/rss") && !type.Contains("application/atom"))
            {
                return(null);
            }

            HtmlFeedLink hfl   = new HtmlFeedLink();
            string       title = GetAttributeFromLinkTag("title", linkTag);
            string       href  = GetAttributeFromLinkTag("href", linkTag);

            hfl.Title    = title;
            hfl.Url      = href;
            hfl.FeedType = type.Contains("rss") ? FeedType.Rss : FeedType.Atom;
            return(hfl);
        }