/// <summary>
        /// Возвращает изображение для новостной категории
        /// </summary>
        /// <param name="rss">Рсс-лента, для которой нужно получить изображение категории</param>
        /// <returns></returns>
        private static async Task <string> GetCategoryImgAsync(Rss rss)
        {
            var    itemNumber = 0;
            string imageLink  = null;

            do
            {
                var uri = rss.Channel.Items[itemNumber].Link;

                imageLink = HttpServices.ParseImage(await HttpServices.GetHttpResponseAsync(uri));
            } while ((imageLink == null) && (itemNumber < 50));

            return(imageLink);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// К уже полученной коллекции категорий и новостей, добавляем недостающие поля (изображения, основной текст новости и "связанные" новости"
        /// </summary>
        public static async Task <ObservableCollection <NewsCategory> > GetNewsItemsAsync(ObservableCollection <NewsCategory> categories)
        {
            // Восстанавливаем предыдущее состояние.
            var          oldNewsCollection = HttpServices.ReadXmlAsync();
            NewsCategory existedCategory   = null;

            NewsCategory.NewsItem existedItem = null;


            foreach (var category in categories)
            {
                // Пытаемся восстановить новости из предыдущего состояния, если такая новость уже была в нашем списке,
                // чтобы уменьшить количество запросов к внешнему ресурсу.
                if (oldNewsCollection != null)
                {
                    existedCategory = oldNewsCollection.FirstOrDefault(x => x.CategoryName == category.CategoryName);
                }
                foreach (var newsItem in category.Items)
                {
                    if (existedCategory != null)
                    {
                        existedItem = existedCategory.Items.FirstOrDefault(x => x.NewsTitle == newsItem.NewsTitle);
                    }
                    if (existedItem != null)
                    {
                        newsItem.NewsImagePath    = existedItem.NewsImagePath;
                        newsItem.NewsArticle      = existedItem.NewsArticle;
                        newsItem.RelatedNewsItems = existedItem.RelatedNewsItems;
                    }
                    // Если новость "новая"
                    else
                    {
                        var response = await HttpServices.GetHttpResponseAsync(newsItem.NewsLink);

                        newsItem.NewsImagePath    = HttpServices.ParseImage(response);
                        newsItem.NewsArticle      = HttpServices.ParseNewsBody(response);
                        newsItem.RelatedNewsItems = await GetRelatedNews(response);
                    }
                }
            }
            return(categories);
        }