Пример #1
0
        /// <summary>
        /// Downloads the HTML of the specified URL and parses it for open graph content.
        /// </summary>
        /// <param name="url">The URL to download the HTML from.</param>
        /// <param name="userAgent">The user agent to use when downloading content.  The default is <c>"facebookexternalhit"</c> which is required for some site (like amazon) to include open graph data.</param>
        /// <param name="validateSpecification">if set to <c>true</c> verify that the document meets the required attributes of the open graph specification.</param>
        /// <returns><see cref="OpenGraph"/></returns>
        public static OpenGraph ParseUrl(Uri url, string userAgent = "facebookexternalhit", bool validateSpecification = false)
        {
            OpenGraph result = new OpenGraph {
                OriginalUrl = url
            };

            HttpDownloader downloader = new HttpDownloader(url, null, userAgent);
            string         html       = "";//downloader.GetPage();

            return(ParseHtml(result, html, validateSpecification));
        }
Пример #2
0
        /// <summary>
        /// Parses the HTML.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <param name="content">The content.</param>
        /// <param name="validateSpecification">if set to <c>true</c> [validate specification].</param>
        /// <returns><see cref="OpenGraph"/></returns>
        /// <exception cref="OpenGraph_Net.InvalidSpecificationException">The parsed HTML does not meet the open graph specification</exception>
        private static OpenGraph ParseHtml(OpenGraph result, string content, bool validateSpecification = false)
        {
            int    indexOfClosingHead = Regex.Match(content, "</head>").Index;
            string toParse            = content.Substring(0, indexOfClosingHead + 7);

            toParse = toParse + "<body></body></html>\r\n";

            HtmlDocument document = new HtmlDocument();

            document.LoadHtml(toParse);

            HtmlNodeCollection allMeta = document.DocumentNode.SelectNodes("//meta");

            var openGraphMetaTags = from meta in allMeta ?? new HtmlNodeCollection(null)
                                    where (meta.Attributes.Contains("property") && meta.Attributes["property"].Value.StartsWith("og:")) ||
                                    (meta.Attributes.Contains("name") && meta.Attributes["name"].Value.StartsWith("og:"))
                                    select meta;

            foreach (HtmlNode metaTag in openGraphMetaTags)
            {
                string value    = GetOpenGraphValue(metaTag);
                string property = GetOpenGraphKey(metaTag);
                if (string.IsNullOrWhiteSpace(value))
                {
                    continue;
                }

                if (result.openGraphData.ContainsKey(property))
                {
                    continue;
                }

                result.openGraphData.Add(property, value);
            }

            string type = string.Empty;

            result.openGraphData.TryGetValue("type", out type);
            result.Type = type ?? string.Empty;

            string title = string.Empty;

            result.openGraphData.TryGetValue("title", out title);
            result.Title = title ?? string.Empty;

            try
            {
                string image = string.Empty;
                result.openGraphData.TryGetValue("image", out image);
                result.Image = new Uri(image);
            }
            catch (UriFormatException)
            {
                // do nothing
            }
            catch (ArgumentException)
            {
                // do nothing
            }

            try
            {
                string url = string.Empty;
                result.openGraphData.TryGetValue("url", out url);
                result.Url = new Uri(url);
            }
            catch (UriFormatException)
            {
                // do nothing
            }
            catch (ArgumentException)
            {
                // do nothing
            }

            if (validateSpecification)
            {
                foreach (string required in RequiredMeta)
                {
                    if (!result.ContainsKey(required))
                    {
                        throw new InvalidSpecificationException("The parsed HTML does not meet the open graph specification");
                    }
                }
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Parses the HTML for open graph content.
        /// </summary>
        /// <param name="content">The HTML to parse.</param>
        /// <param name="validateSpecification">if set to <c>true</c> verify that the document meets the required attributes of the open graph specification.</param>
        /// <returns><see cref="OpenGraph"/></returns>
        public static OpenGraph ParseHtml(string content, bool validateSpecification = false)
        {
            OpenGraph result = new OpenGraph();

            return(ParseHtml(result, content, validateSpecification));
        }
Пример #4
0
        /// <summary>
        /// Makes the graph.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="type">The type.</param>
        /// <param name="image">The image.</param>
        /// <param name="url">The URL.</param>
        /// <param name="description">The description.</param>
        /// <param name="siteName">Name of the site.</param>
        /// <param name="audio">The audio.</param>
        /// <param name="video">The video.</param>
        /// <param name="locale">The locale.</param>
        /// <param name="localeAlternate">The locale alternate.</param>
        /// <param name="determiner">The determiner.</param>
        /// <returns><see cref="OpenGraph"/></returns>
        public static OpenGraph MakeGraph(
            string title,
            string type,
            string image,
            string url,
            string description             = "",
            string siteName                = "",
            string audio                   = "",
            string video                   = "",
            string locale                  = "",
            IList <string> localeAlternate = null,
            string determiner              = "")
        {
            var graph = new OpenGraph
            {
                Title = title,
                Type  = type,
                Image = new Uri(image, UriKind.Absolute),
                Url   = new Uri(url, UriKind.Absolute)
            };

            graph.openGraphData.Add("title", title);
            graph.openGraphData.Add("type", type);
            graph.openGraphData.Add("image", image);
            graph.openGraphData.Add("url", url);

            if (!string.IsNullOrWhiteSpace(description))
            {
                graph.openGraphData.Add("description", description);
            }

            if (!string.IsNullOrWhiteSpace(siteName))
            {
                graph.openGraphData.Add("site_name", siteName);
            }

            if (!string.IsNullOrWhiteSpace(audio))
            {
                graph.openGraphData.Add("audio", audio);
            }

            if (!string.IsNullOrWhiteSpace(video))
            {
                graph.openGraphData.Add("video", video);
            }

            if (!string.IsNullOrWhiteSpace(locale))
            {
                graph.openGraphData.Add("locale", locale);
            }

            if (!string.IsNullOrWhiteSpace(determiner))
            {
                graph.openGraphData.Add("determiner", determiner);
            }

            if (localeAlternate != null)
            {
                graph.localAlternatives = localeAlternate;
            }

            return(graph);
        }