Example #1
0
        /// <summary>
        /// Parses the URL asynchronous.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="userAgent">The user agent.</param>
        /// <param name="validateSpecification">if set to <c>true</c> [validate specification].</param>
        /// <returns><see cref="Task{OpenGraph}"/></returns>
        public static async Task <OpenGraph> ParseUrlAsync(Uri url, string userAgent = "facebookexternalhit", bool validateSpecification = false)
        {
            OpenGraph result = new OpenGraph {
                OriginalUrl = url
            };

            HttpDownloader downloader = new HttpDownloader(url, null, userAgent);
            string         html       = await downloader.GetPageAsync();

            return(ParseHtml(result, html, validateSpecification));
        }
Example #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="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 urlPropertyPatterns    = new[] { "image", "url^" };
            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;
                }

                foreach (var urlPropertyPattern in urlPropertyPatterns)
                {
                    if (Regex.IsMatch(property, urlPropertyPattern))
                    {
                        value = HtmlDecodeUrl(value);
                        break;
                    }
                }
                result._openGraphData.Add(property, value);
            }

            string type;

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

            string title;

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

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

            try
            {
                string url;
                result._openGraphData.TryGetValue("url", out url);
                result.Url = new Uri(url ?? string.Empty);
            }
            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);
        }
Example #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));
        }
Example #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);
        }