public void CreateUriAction(UriEx uriex, Action Finished) { uriex.Init(); uriex.UrlTweetCount = uriex.Uri.GetTweetCount(); uriex.UrlFacebookShareCount = uriex.Uri.GetFacebookShareCount(); if (uriex.IsHtmlContentUrl) { var doc = new HtmlAgilityPack.HtmlDocument(); try { var req = uriex.Uri.GetWebRequest(); using (var resp = req.GetResponse()) { using (var reader = new StreamReader(resp.GetResponseStream(), true)) { doc.Load(reader); } } } catch(Exception ex) { ex = ex; } if (doc.DocumentNode != null) { var nodes = doc.DocumentNode.SelectNodes("//title"); if (nodes != null && nodes.Count > 0) { uriex.Title = nodes.First().InnerText.Trim(); } nodes = doc.DocumentNode.SelectNodes("//link"); if (nodes != null && nodes.Count > 0) { var ogMeta = nodes .Where(m => m.Attributes.SingleOrDefault(a => a.Name.ToLower() == "rel" && a.Value.ToLower().StartsWith("image_src")) != null) .Select(m => new { Property = m.Attributes["rel"].Value.ToLower(), Content = m.Attributes["href"].Value }); if (ogMeta != null && ogMeta.Count() > 0) { uriex.Image = ogMeta.Where(x => x.Property == "image_src").Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); } } nodes = doc.DocumentNode.SelectNodes("//meta"); if (nodes != null && nodes.Count > 0) { var ogMeta = nodes .Where(m => m.Attributes.SingleOrDefault(a => a.Name.ToLower() == "property" && a.Value.ToLower().StartsWith("og:")) != null) .Select(m => new { Property = m.Attributes["property"].Value.ToLower(), Content = m.Attributes["content"] != null ? m.Attributes["content"].Value : (m.Attributes["value"] != null ? m.Attributes["value"].Value : "") }); if (ogMeta != null && ogMeta.Count() > 0) { uriex.Title = (ogMeta.Where(x => x.Property == "og:title" && !string.IsNullOrEmpty(x.Content)).Select(x => x.Content).FirstOrDefault() ?? "").Trim(); uriex.Description = ogMeta.Where(x => x.Property == "og:description" && !string.IsNullOrEmpty(x.Content)).Select(x => x.Content).FirstOrDefault() ?? ""; uriex.Image = ogMeta.Where(x => x.Property == "og:image" && !string.IsNullOrEmpty(x.Content)).Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); uriex.Video = ogMeta.Where(x => x.Property == "og:video" && !string.IsNullOrEmpty(x.Content)).Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); uriex.Video = CleanYouTube(uriex.Video); } var twitterMeta = nodes .Where(m => m.Attributes.SingleOrDefault(a => a.Name.ToLower() == "property" && a.Value.ToLower().StartsWith("twitter:")) != null) .Select(m => new { Property = m.Attributes["property"].Value.ToLower(), Content = m.Attributes["content"] != null ? m.Attributes["content"].Value : (m.Attributes["value"] != null ? m.Attributes["value"].Value : "") }); if (twitterMeta != null && twitterMeta.Count() > 0) { if(string.IsNullOrEmpty(uriex.Title)) uriex.Title = (twitterMeta.Where(x => x.Property == "twitter:title" && !string.IsNullOrEmpty(x.Content)).Select(x => x.Content).FirstOrDefault() ?? "").Trim(); if (string.IsNullOrEmpty(uriex.Description)) uriex.Description = twitterMeta.Where(x => x.Property == "twitter:description" && !string.IsNullOrEmpty(x.Content)).Select(x => x.Content).FirstOrDefault() ?? ""; if (uriex.Image == null) uriex.Image = twitterMeta.Where(x => x.Property == "twitter:image" && !string.IsNullOrEmpty(x.Content)).Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); if (uriex.Video == null) { uriex.Video = twitterMeta.Where(x => x.Property == "twitter:player" && !string.IsNullOrEmpty(x.Content)).Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); uriex.Video = CleanYouTube(uriex.Video); } } } if(uriex.Video == null) { nodes = doc.DocumentNode.SelectNodes("//iframe"); if (nodes != null && nodes.Count > 0) { var iframes = nodes .Where(i => i.Attributes["src"] != null && i.Attributes["src"].Value.ToLower().StartsWith(YOUTUBE_EMBED)) .Select(i => i.Attributes["src"].Value); if (iframes.Count() > 0) uriex.Video = new Uri(iframes.FirstOrDefault()); } } } } else if(uriex.IsImageContentUrl) { uriex.Image = uriex.Uri; uriex.Title = uriex.Uri.ToString(); } Finished(); }
public void CreateUriAction(UriEx uriex, Action Finished) { uriex.Init(); uriex.UrlTweetCount = uriex.Uri.GetTweetCount(); uriex.UrlFacebookShareCount = uriex.Uri.GetFacebookShareCount(); if (uriex.IsHtmlContentUrl) { var doc = new HtmlAgilityPack.HtmlDocument(); try { var req = uriex.Uri.GetWebRequest(); using (var resp = req.GetResponse()) { using (var reader = new StreamReader(resp.GetResponseStream(), true)) { doc.Load(reader); } } } catch (Exception ex) { ex = ex; } if (doc.DocumentNode != null) { var nodes = doc.DocumentNode.SelectNodes("//title"); if (nodes != null && nodes.Count > 0) { uriex.Title = nodes.First().InnerText.Trim(); } nodes = doc.DocumentNode.SelectNodes("//link"); if (nodes != null && nodes.Count > 0) { var ogMeta = nodes .Where(m => m.Attributes.SingleOrDefault(a => a.Name.ToLower() == "rel" && a.Value.ToLower().StartsWith("image_src")) != null) .Select(m => new { Property = m.Attributes["rel"].Value.ToLower(), Content = m.Attributes["href"].Value }); if (ogMeta != null && ogMeta.Count() > 0) { uriex.Image = ogMeta.Where(x => x.Property == "image_src").Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); } } nodes = doc.DocumentNode.SelectNodes("//meta"); if (nodes != null && nodes.Count > 0) { var ogMeta = nodes .Where(m => m.Attributes.SingleOrDefault(a => a.Name.ToLower() == "property" && a.Value.ToLower().StartsWith("og:")) != null) .Select(m => new { Property = m.Attributes["property"].Value.ToLower(), Content = m.Attributes["content"] != null ? m.Attributes["content"].Value : (m.Attributes["value"] != null ? m.Attributes["value"].Value : "") }); if (ogMeta != null && ogMeta.Count() > 0) { uriex.Title = (ogMeta.Where(x => x.Property == "og:title" && !string.IsNullOrEmpty(x.Content)).Select(x => x.Content).FirstOrDefault() ?? "").Trim(); uriex.Description = ogMeta.Where(x => x.Property == "og:description" && !string.IsNullOrEmpty(x.Content)).Select(x => x.Content).FirstOrDefault() ?? ""; uriex.Image = ogMeta.Where(x => x.Property == "og:image" && !string.IsNullOrEmpty(x.Content)).Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); uriex.Video = ogMeta.Where(x => x.Property == "og:video" && !string.IsNullOrEmpty(x.Content)).Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); uriex.Video = CleanYouTube(uriex.Video); } var twitterMeta = nodes .Where(m => m.Attributes.SingleOrDefault(a => a.Name.ToLower() == "property" && a.Value.ToLower().StartsWith("twitter:")) != null) .Select(m => new { Property = m.Attributes["property"].Value.ToLower(), Content = m.Attributes["content"] != null ? m.Attributes["content"].Value : (m.Attributes["value"] != null ? m.Attributes["value"].Value : "") }); if (twitterMeta != null && twitterMeta.Count() > 0) { if (string.IsNullOrEmpty(uriex.Title)) { uriex.Title = (twitterMeta.Where(x => x.Property == "twitter:title" && !string.IsNullOrEmpty(x.Content)).Select(x => x.Content).FirstOrDefault() ?? "").Trim(); } if (string.IsNullOrEmpty(uriex.Description)) { uriex.Description = twitterMeta.Where(x => x.Property == "twitter:description" && !string.IsNullOrEmpty(x.Content)).Select(x => x.Content).FirstOrDefault() ?? ""; } if (uriex.Image == null) { uriex.Image = twitterMeta.Where(x => x.Property == "twitter:image" && !string.IsNullOrEmpty(x.Content)).Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); } if (uriex.Video == null) { uriex.Video = twitterMeta.Where(x => x.Property == "twitter:player" && !string.IsNullOrEmpty(x.Content)).Select(x => CreateUriSafely(uriex.Uri, x.Content)).FirstOrDefault(); uriex.Video = CleanYouTube(uriex.Video); } } } if (uriex.Video == null) { nodes = doc.DocumentNode.SelectNodes("//iframe"); if (nodes != null && nodes.Count > 0) { var iframes = nodes .Where(i => i.Attributes["src"] != null && i.Attributes["src"].Value.ToLower().StartsWith(YOUTUBE_EMBED)) .Select(i => i.Attributes["src"].Value); if (iframes.Count() > 0) { uriex.Video = new Uri(iframes.FirstOrDefault()); } } } } } else if (uriex.IsImageContentUrl) { uriex.Image = uriex.Uri; uriex.Title = uriex.Uri.ToString(); } Finished(); }