private async Task<Models.FeedData> GetFeedAsync(string feedUriString)
		{
			var client = new SyndicationClient();
			Uri feedUri;
			if (!Uri.TryCreate(feedUriString, UriKind.RelativeOrAbsolute, out feedUri))
			{
				return null;
			}

			try
			{
				var feed = await client.RetrieveFeedAsync(feedUri);
				var feedData = new FeedData();
				if (feed.Title != null && feed.Title.Text != null)
				{
					feedData.Title = feed.Title.Text;
				}
				if (feed.Subtitle != null && feed.Subtitle.Text != null)
				{
					feedData.Description = feed.Subtitle.Text;
				}
				if (feed.Items != null && feed.Items.Count > 0)
				{
					feedData.PublishedDate = feed.Items[0].PublishedDate.DateTime;

					foreach (var item in feed.Items)
					{
						var feedItem = new FeedItem();
						if (item.Title != null && item.Title.Text != null)
						{
							feedItem.Title = item.Title.Text;
						}
						if (item.PublishedDate != null)
						{
							feedItem.PublishedDate = item.PublishedDate.DateTime;
						}
						if (item.Authors != null && item.Authors.Count > 0)
						{
							feedItem.PublishedDate = item.PublishedDate.DateTime;
						}
						if (feed.SourceFormat == SyndicationFormat.Atom10)
						{
							if (item.Content != null && item.Content.Text != null)
							{
								feedItem.Content = item.Content.Text;
							}
							if (item.Id != null)
							{
								feedItem.Link = new Uri(item.Id);
							}
						}
						else if (feed.SourceFormat == SyndicationFormat.Rss20)
						{
							if (item.Summary != null && item.Summary.Text != null)
							{
								feedItem.Content = item.Summary.Text;
							}
							if (item.Links != null && item.Links.Count > 0)
							{
								feedItem.Link = item.Links[0].Uri;
							}
						}

						feedItem.Id = Regex.Replace(feedItem.Link.ToString(), @"[^0-9a-zA-Z\.]+", string.Empty).GetHashCode();

						feedData.Items.Add(feedItem);
					}
				}

				return feedData;
			}
			catch (Exception ex)
			{
				//System.Diagnostics.Debug.WriteLine(ex.Message);
				//return null;
				SyndicationErrorStatus status = SyndicationError.GetStatus(ex.HResult);
				if (status == SyndicationErrorStatus.InvalidXml)
				{
					System.Diagnostics.Debug.WriteLine("An invalid XML exception was thrown. Please make sure to use a URI that points to a RSS or Atom feed.");
				}

				if (status == SyndicationErrorStatus.Unknown)
				{
					Windows.Web.WebErrorStatus webError = Windows.Web.WebError.GetStatus(ex.HResult);

					if (webError == Windows.Web.WebErrorStatus.Unknown)
					{
						// Neither a syndication nor a web error. Rethrow.
						throw;
					}
				}

				return null;
			}
		}
Exemple #2
0
		protected override async Task<Models.FeedData> GetFeedAsync(string feedUriString)
		{
			var client = new SyndicationClient();
			Uri feedUri;
			if (!Uri.TryCreate(feedUriString, UriKind.RelativeOrAbsolute, out feedUri))
			{
				return null;
			}

			try
			{
				var feed = await client.RetrieveFeedAsync(feedUri);
				var feedData = new FeedData();
				if (feed.Title != null && feed.Title.Text != null)
				{
					feedData.Title = feed.Title.Text;
				}
				if (feed.Subtitle != null && feed.Subtitle.Text != null)
				{
					feedData.Description = feed.Subtitle.Text;
				}
				if (feed.Items != null && feed.Items.Count > 0)
				{
					feedData.PublishedDate = feed.Items[0].PublishedDate.DateTime;

					foreach (var item in feed.Items)
					{
						var feedItem = new FeedItem();
						if (item.Title != null && item.Title.Text != null)
						{
							feedItem.Title = item.Title.Text;
						}
						if (item.PublishedDate != null)
						{
							feedItem.PublishedDate = item.PublishedDate.DateTime;
						}
						if (item.Authors != null && item.Authors.Count > 0)
						{
							feedItem.PublishedDate = item.PublishedDate.DateTime;
						}
						if (feed.SourceFormat == SyndicationFormat.Atom10)
						{
							if (item.Content != null && item.Content.Text != null)
							{
								feedItem.Content = item.Content.Text;
							}
							if (item.Id != null)
							{
								feedItem.Link = new Uri(item.Id);
							}
						}
						else if (feed.SourceFormat == SyndicationFormat.Rss20)
						{
							if (item.Summary != null && item.Summary.Text != null)
							{
								feedItem.Content = item.Summary.Text;
							}
							if (item.Links != null && item.Links.Count > 0)
							{
								feedItem.Link = item.Links[0].Uri;
							}
						}

						feedItem.Id = Regex.Replace(feedItem.Link.ToString(), @"[^0-9a-zA-Z\.]+", string.Empty).GetHashCode();

						feedData.Items.Add(feedItem);
					}
				}

				return feedData;
			}
			catch (Exception ex)
			{
				System.Diagnostics.Debug.WriteLine(ex.Message);
				return null;
			}
		}
Exemple #3
0
        protected override Task<Models.FeedData> GetFeedAsync(string feedUriString)
        {
            var task = new TaskCompletionSource<Models.FeedData>();

            var webClient = new WebClient();
            webClient.DownloadStringCompleted += (sender, args) =>
            {
                if (args.Error == null)
                {
                    var stringReader = new StringReader(args.Result);
                    var feedData = new FeedData();
                    using (var xmlReader = XmlReader.Create(stringReader))
                    {
                        var feed = SyndicationFeed.Load(xmlReader);

                        if (feed.Title != null && feed.Title.Text != null)
                        {
                            feedData.Title = feed.Title.Text;
                        }
                        if (feed.Description != null && feed.Description.Text != null)
                        {
                            feedData.Description = feed.Description.Text;
                        }
                        if (feed.Items != null && feed.Items.Any())
                        {
                            feedData.PublishedDate = feed.Items.First().PublishDate.DateTime;

                            foreach (var item in feed.Items)
                            {
                                var feedItem = new FeedItem();
                                if (item.Title != null && item.Title.Text != null)
                                {
                                    feedItem.Title = item.Title.Text;
                                }
                                if (item.PublishDate != null)
                                {
                                    feedItem.PublishedDate = item.PublishDate.DateTime;
                                }
                                if (item.Authors != null && item.Authors.Count > 0)
                                {
                                    feedItem.PublishedDate = item.PublishDate.DateTime;
                                }
                                if (item.Content != null && ((TextSyndicationContent)item.Content).Text != null)
                                {
                                    feedItem.Content = ((TextSyndicationContent)item.Content).Text;
                                }
                                else if (item.Summary != null && item.Summary.Text != null)
                                {
                                    feedItem.Content = item.Summary.Text;
                                }
                                if (item.Links != null && item.Links.Count > 0)
                                {
                                    feedItem.Link = item.Links[0].Uri;
                                }

                                feedItem.Id = Regex.Replace(feedItem.Link.ToString(), @"[^0-9a-zA-Z\.]+", string.Empty).GetHashCode();

                                feedData.Items.Add(feedItem);
                            }
                        }
                    }
                    task.TrySetResult(feedData);
                }
                else
                {
                    task.TrySetException(args.Error);
                }
            };
            webClient.DownloadStringAsync(new Uri(feedUriString));

            return task.Task;
        }