Example #1
0
        public async Task <List <ViewModel.NewsItem> > GetNewsAsync(Configuration.Configuration config)
        {
            List <ViewModel.NewsItem> newsItems = new List <ViewModel.NewsItem>();

            foreach (var creator in config.NewsFeeds)
            {
                var feed = await _SyndicationClient.RetrieveFeedAsync(creator.Url);

                if (feed != null && feed.Items != null && feed.Items.Count > 0)
                {
                    foreach (var item in feed.Items)
                    {
                        ViewModel.NewsItem rssItem = new ViewModel.NewsItem()
                        {
                            Source = creator.Name,
                            Title  = item.Title?.Text ?? string.Empty,
                        };
                        string rawContent = item.Content?.Text ?? item.Summary?.Text ?? string.Empty;
                        rssItem.ContentRaw = rawContent ?? rssItem.ContentRaw;
                        rssItem.Created    = item.PublishedDate.DateTime;

                        rssItem = await creator.CreateItem(rssItem, item);

                        rssItem.GenerateID();

                        newsItems.Add(rssItem);
                    }
                }
            }

            newsItems.Sort((A, B) => A.Created.CompareTo(B.Created));

            return(newsItems);
        }
Example #2
0
#pragma warning disable CS1998 // Bei der asynchronen Methode fehlen "await"-Operatoren. Die Methode wird synchron ausgeführt.
        public async Task <ViewModel.NewsItem> CreateItem(ViewModel.NewsItem result, SyndicationItem item)
#pragma warning restore CS1998 // Bei der asynchronen Methode fehlen "await"-Operatoren. Die Methode wird synchron ausgeführt.
        {
            result.URIToSource = item.Id;

            return(result);
        }
Example #3
0
        public async Task<List<ViewModel.NewsItem>> GetNewsAsync(Configuration.Configuration config)
        {
            List<ViewModel.NewsItem> newsItems = new List<ViewModel.NewsItem>();
            foreach (var creator in config.NewsFeeds)
            {
                var feed = await _SyndicationClient.RetrieveFeedAsync(creator.Url);

                if (feed != null && feed.Items != null && feed.Items.Count > 0)
                {
                    foreach (var item in feed.Items)
                    {
                        ViewModel.NewsItem rssItem = new ViewModel.NewsItem()
                        {
                            Source = creator.Name,
                            Title = item.Title?.Text ?? string.Empty,
                        };
                        string rawContent = item.Content?.Text ?? item.Summary?.Text ?? string.Empty;
                        rssItem.ContentRaw = rawContent ?? rssItem.ContentRaw;
                        rssItem.Created = item.PublishedDate.DateTime;

                        rssItem = await creator.CreateItem(rssItem, item);

                        rssItem.GenerateID();

                        newsItems.Add(rssItem);                        
                    }
                }
            }

            newsItems.Sort((A, B) => A.Created.CompareTo(B.Created));

            return newsItems;
        }
Example #4
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public async Task <ViewModel.NewsItem> CreateItem(ViewModel.NewsItem result, SyndicationItem item)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            if (!string.IsNullOrEmpty(result.ContentRaw))
            {
                result.ContentRaw = string.Format(GenericHtml.HTML, result.ContentRaw);
            }
            else if (!string.IsNullOrEmpty(result.Content))
            {
                result.ContentRaw = string.Format(GenericHtml.HTML, result.Content);
            }


            return(result);
        }
Example #5
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public async Task <ViewModel.NewsItem> CreateItem(ViewModel.NewsItem result, SyndicationItem item)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            result.URIToSource = item.Id;

            if (!string.IsNullOrEmpty(result.ContentRaw))
            {
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(result.ContentRaw);

                foreach (HtmlNode link in doc.DocumentNode.Descendants("img"))
                {
                    int width  = Convert.ToInt32(link.Attributes["width"]?.Value ?? "9999");
                    int height = Convert.ToInt32(link.Attributes["height"]?.Value ?? "9999");

                    if (width > 10 && height > 10)
                    {
                        result.Image = link.Attributes["src"].Value; break;
                    }
                }

                result.Content = "";

                var p = doc.DocumentNode.Descendants("p").FirstOrDefault();
                if (p != null)
                {
                    result.Content = HtmlEntity.DeEntitize(p.InnerText).Replace("\n", "").Replace("\r\n", "").Trim();
                }

                if (string.IsNullOrWhiteSpace(result.Content))
                {
                    result.Content = HtmlEntity.DeEntitize(doc.DocumentNode.InnerText).Replace("\n", "").Replace("\r\n", "").Trim();
                }

                if (!string.IsNullOrEmpty(result.Content))
                {
                    result.Content = result.Content.Length > MAX_NEWS_LEN?result.Content.Substring(0, MAX_NEWS_LEN) : result.Content;
                }

                // Format Raw
                result.ContentRaw = string.Format(GenericHtml.HTML, result.ContentRaw);
            }

            return(result);
        }
Example #6
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public async Task <ViewModel.NewsItem> CreateItem(ViewModel.NewsItem result, SyndicationItem item)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            result.URIToSource = item.Id;
            return(result);
        }
Example #7
0
        public async Task <ViewModel.NewsItem> CreateItem(ViewModel.NewsItem result, SyndicationItem item)
        {
            result.URIToSource = item.Id;

            if (!string.IsNullOrEmpty(result.ContentRaw))
            {
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(result.ContentRaw);
                foreach (HtmlNode link in doc.DocumentNode.Descendants("img"))
                {
                    result.Image = link.Attributes["src"].Value; break;
                }

                result.Content    = HtmlEntity.DeEntitize(doc.DocumentNode.InnerText);
                result.ContentRaw = null;
            }

            HtmlDocument site = null;
            HtmlWeb      web  = new HtmlWeb();

            try { site = await web.LoadFromWebAsync(result.URIToSource); }
            catch { }

            if (site != null)
            {
                var contentNode = site.DocumentNode.Descendants("div").Where(A => A.Attributes["class"]?.Value?.Contains("formatted") ?? false).FirstOrDefault();

                HtmlNode imagecontainer;
                while ((imagecontainer = site.DocumentNode.Descendants("a").Where(A => A.Attributes["class"]?.Value?.Contains("golem-gallery2-nojs") ?? false).FirstOrDefault()) != null)
                {
                    string imageHtml           = "<div>\n";
                    var    imagesFromContainer = imagecontainer.Descendants("img");
                    foreach (var img in imagesFromContainer)
                    {
                        img.Attributes["src"].Value = img.Attributes["data-src-full"]?.Value ?? img.Attributes["data-src"]?.Value;
                        img.Attributes.Append("style", "max-width:600px");
                        imageHtml += img.OuterHtml + "<br/>\n";
                    }
                    imageHtml += "</div>\n";
                    var imagesNode = HtmlNode.CreateNode(imageHtml);
                    contentNode.ReplaceChild(imagesNode, imagecontainer);
                }


                if (contentNode != null && !string.IsNullOrEmpty(contentNode.InnerText) && !string.IsNullOrWhiteSpace(HtmlEntity.DeEntitize(contentNode.InnerText)))
                {
                    var nodes = contentNode.Elements("div").ToList();
                    foreach (var delNode in nodes)
                    {
                        contentNode.ChildNodes.Remove(delNode);
                    }

                    result.ContentRaw = string.Format(GenericHtml.HTML, contentNode.InnerHtml);
                }

                Func <string, string> getMetaContentByName = (string name) => site.DocumentNode.Descendants("meta")
                                                             ?.Where(A => A.Attributes["name"]?.Value?.ToLower() == name && !string.IsNullOrEmpty(A.Attributes["content"]?.Value))
                                                             ?.FirstOrDefault()?.Attributes["content"]?.Value;

                var twitter_image_src = getMetaContentByName("twitter:image:src");

                if (!string.IsNullOrEmpty(twitter_image_src))
                {
                    result.Image = twitter_image_src;
                }
            }

            return(result);
        }