public RssFeed(string feedUrl) { this.FeedUrl = new Uri(feedUrl); this.syndicationClient = new SyndicationClient(); this.feed = null; var feedPromise = GetFeed(); }
public static void ParseSyndicationFeed(this RssFeed feed, SyndicationFeed syndicFeed) { feed.Title = syndicFeed.Title.Text; if (syndicFeed.Subtitle != null && syndicFeed.Subtitle.Text != null) { feed.Description = syndicFeed.Subtitle.Text; } feed.PubDate = new DateTime(); feed.Items = new ObservableCollection<RssItem>(); RssItem item; bool dateSet = false; foreach (SyndicationItem syndicItem in syndicFeed.Items) { item = new RssItem(); item.ParseSyndicationItem(syndicFeed, syndicItem); if (dateSet == false) { feed.PubDate = item.PubDate; dateSet = true; } feed.Items.Add(item); } }
public static void ParseSyndicationItem(this RssItem item, SyndicationFeed syndicFeed, SyndicationItem syndicItem) { item.Title = syndicItem.Title.Text; item.PubDate = syndicItem.PublishedDate.DateTime; item.Author = syndicItem.Authors.Count > 0 ? syndicItem.Authors[0].Name.ToString() : ""; if (syndicItem.Id.StartsWith("http")) { item.Link = syndicItem.Id; } else if (syndicItem.Links.Count > 0) { item.Link = syndicItem.Links[0].Uri.OriginalString; } item.ParseImage(syndicItem.NodeValue); if (syndicFeed.SourceFormat == SyndicationFormat.Atom10) { item.Content = HtmlUtilities.ConvertToText(syndicItem.Content.Text); } else if (syndicFeed.SourceFormat == SyndicationFormat.Rss20) { item.Content = HtmlUtilities.ConvertToText(syndicItem.Summary.Text); } }
async Task <SyndicationFeed> ReadFromWebWithAlternative() { // including user agent, otherwise FB rejects the request var _Client = new HttpClient(); var _UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; _Client.DefaultRequestHeaders.Add("user-agent", _UserAgent); // fetch as string to avoid error var _Uri = new Uri(this.SourceUrl); var _Response = await _Client.GetAsync(_Uri); var _String = await _Response.Content.ReadAsStringAsync(); // convert to xml (will validate, too) var _XmlDocument = new Windows.Data.Xml.Dom.XmlDocument(); _XmlDocument.LoadXml(_String); // manually fill feed from xml var _Feed = new Windows.Web.Syndication.SyndicationFeed(); _Feed.LoadFromXml(_XmlDocument); return(_Feed); }
// This method can be call simultaneously. Make sure only one thread is touching it. public static AddQuestionsResult AddQuestions(string websiteUrl, SyndicationFeed feed, bool skipLatestPubDate) { AddQuestionsResult globalResult = new AddQuestionsResult(); DateTimeOffset latestPubDate = SettingsManager.GetLastestPubDate(websiteUrl); DateTimeOffset newLatestPubDate = DateTimeOffset.MinValue; Debug.WriteLine("{0} Current LastestPubDate is {1}.", websiteUrl, latestPubDate); // Wait until the event is set by another thread. addEvent.WaitOne(); try { CheckSettingsAreLoaded(); foreach (SyndicationItem item in feed.Items) { AddQuestionResult result = AddQuestionResult.None; if (skipLatestPubDate || DateTimeOffset.Compare(item.PublishedDate.DateTime, latestPubDate) > 0) { result = AddQuestion(websiteUrl, item); // Chances are we need to update the LatestPubDate. if (result == AddQuestionResult.Added && item.PublishedDate > newLatestPubDate) { newLatestPubDate = item.PublishedDate; Debug.WriteLine("{0} New LastestPubDate is {1}.", websiteUrl, newLatestPubDate); } } else { result = UpdateQuestion(websiteUrl, item); } switch (result) { case AddQuestionResult.Added: globalResult.AddedQuestions++; break; case AddQuestionResult.Updated: globalResult.UpdatedQuestions++; break; } } // If the quesiton list did not change, there should not be a new LatestPubDate. if (globalResult.AddedQuestions > 0) { SettingsManager.SetLastestPubDate(websiteUrl, newLatestPubDate); } return globalResult; } finally { // Set the event, so other threads waiting on it can do their job. addEvent.Set(); } }
public IList<SyndicationItem> Thing(string text) { SyndicationFeed feed = new SyndicationFeed(); feed.Load(text); return feed.Items; }
private async void GetFeed_Click(object sender, RoutedEventArgs e) { OutputField.Text = ""; // By default 'FeedUri' is disabled and URI validation is not required. When enabling the text box // validating the URI is required since it was received from an untrusted source (user input). // The URI is validated by calling Uri.TryCreate() that will return 'false' for strings that are not valid URIs. // Note that when enabling the text box users may provide URIs to machines on the intrAnet that require // the "Home or Work Networking" capability. Uri uri; if (!Uri.TryCreate(FeedUri.Text.Trim(), UriKind.Absolute, out uri)) { rootPage.NotifyUser("Error: Invalid URI.", NotifyType.ErrorMessage); return; } SyndicationClient client = new SyndicationClient(); client.BypassCacheOnRetrieve = true; // Although most HTTP servers do not require User-Agent header, others will reject the request or return // a different response if this header is missing. Use SetRequestHeader() to add custom headers. client.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); rootPage.NotifyUser("Downloading feed...", NotifyType.StatusMessage); OutputField.Text = "Downloading feed: " + uri.ToString() + "\r\n"; try { currentFeed = await client.RetrieveFeedAsync(uri); rootPage.NotifyUser("Feed download complete.", NotifyType.StatusMessage); DisplayFeed(); } catch (Exception ex) { SyndicationErrorStatus status = SyndicationError.GetStatus(ex.HResult); if (status == SyndicationErrorStatus.InvalidXml) { OutputField.Text += "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) { WebErrorStatus webError = WebError.GetStatus(ex.HResult); if (webError == WebErrorStatus.Unknown) { // Neither a syndication nor a web error. Rethrow. throw; } } rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage); } }
public static SyndicationFeed Parse(string xml) { //DtdProcessing = DtdProcessing.Ignore is needed for some feeds (e.g. http://www.dotnetrocks.com/feed.aspx) var feed = new SyndicationFeed(); var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); feed.Load(xmlDoc.GetXml()); //feed.Load(reader.ReadContentAsString()); return feed; }
public async Task<SyndicationFeed> GetFeed() { if(this.feed == null) { try { this.feed = await syndicationClient.RetrieveFeedAsync(this.FeedUrl); } catch (Exception ex) { throw new DataSourceException("Couldn't connect to the server with url " + this.FeedUrl, ex); } } return this.feed; }
public PodcastItemsLoader(SyndicationFeed feed) { _feed = feed; _observable = Observable.Defer(() => feed.Items.Where(item => item.IsPodcast()) .ToObservable() //.Do(item => item.SourceFeed = feed) .Select(item => new PodcastItemViewModel(item))) .Publish(); }
private void DisplayFeed(SyndicationFeed currentFeed) { List<TrafficInfoItem> results = new List<TrafficInfoItem>(); foreach (var item in currentFeed.Items) { TrafficInfoItem newItem = new TrafficInfoItem(); newItem.Title = item.Title.Text; newItem.Discription = item.Summary.Text; newItem.Date = item.PublishedDate.ToString(); results.Add(newItem); } grid.ItemsSource = results; }
private IEnumerable<NewsItem> TransformRssFeedToNewsItems(SyndicationFeed syndicationFeed) { var result = new List<NewsItem>(); foreach (var syndicationItem in syndicationFeed.Items) { var title = syndicationItem.Title.Text; var text = CutHtmlMetaFromDescription(syndicationItem); var publicationDate = syndicationItem.PublishedDate; Uri imageSource; TryGetImageSource(syndicationItem, out imageSource); var newsItem = new NewsItem(title, text, publicationDate, imageSource); result.Add(newsItem); } return result; }
private static async void RefreshModelRssFeed(ProgressBarHelper progressBarHelper) { App.MainViewModel.RssItems.Clear(); string url = PreferenceHelper.GetPreference("RSS_FollowerPath"); if (string.IsNullOrEmpty(url)) { progressBarHelper.PopTask(); return; } RestClient client = new RestClient(); RestRequest request = new RestRequest(); client.Authority = url; request.Method = Method.Get; RestResponse response = await client.BeginRequest(request); if(response.Error == RestError.ERROR_SUCCESS && response.Content != null) { try { SyndicationFeed feed = new SyndicationFeed(); feed.Load(response.Content); foreach (SyndicationItem item in feed.Items) { ItemViewModel model = RSSFeedConverter.ConvertFeedToCommon(item); if (model != null) { App.MainViewModel.RssItems.Add(model); } } } catch (System.Exception ex) { DialogHelper.ShowToastDialog("RSS获取中发生错误,可能是网络问题,也可能是对应站点地址变更"); } finally { progressBarHelper.PopTask(); } } else { DialogHelper.ShowToastDialog("RSS获取中发生错误,可能是网络问题,也可能是对应站点地址变更"); progressBarHelper.PopTask(); } }
public async void Refresh(string url) { string errorMessage = null; Uri uri; if (!Uri.TryCreate(url, UriKind.Absolute, out uri)) return; SyndicationClient client = new SyndicationClient(); client.BypassCacheOnRetrieve = true; // Although most HTTP servers do not require User-Agent header, others will reject the request or return // a different response if this header is missing. Use SetRequestHeader() to add custom headers. client.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); try { currentFeed = await client.RetrieveFeedAsync(uri); DisplayFeed(); } catch (Exception ex) { //SyndicationErrorStatus status = SyndicationError.GetStatus(ex.HResult); //if (status == SyndicationErrorStatus.InvalidXml) // OutputField.Text += "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) //{ // WebErrorStatus webError = WebError.GetStatus(ex.HResult); // if (webError == WebErrorStatus.Unknown) // { // // Neither a syndication nor a web error. Rethrow. // throw; // } //} errorMessage = ex.Message; } if (!string.IsNullOrEmpty(errorMessage)) await Utils.MessageBox(errorMessage); }
private async System.Threading.Tasks.Task GenerateInfo() { SyndicationClient client = new SyndicationClient(); client.BypassCacheOnRetrieve = true; Uri uri = new Uri("http://services.sapo.pt/Traffic/GeoRSS"); try { currentFeed = await client.RetrieveFeedAsync(uri); } catch (Exception) { var dialog = new Windows.UI.Popups.MessageDialog("Tem de estar conetado à internet para correr esta app"); dialog.Commands.Add(new UICommand("Close", new UICommandInvokedHandler(this.CloseApp))); dialog.DefaultCommandIndex = 0; dialog.CancelCommandIndex = 1; dialog.ShowAsync(); } if (searchBox.Text == "") { DisplayFeed(currentFeed); } else { DisplayFeed(currentFeed, searchBox.Text); } }
private static void UpdateTile(SyndicationFeed feed) { // Create a tile update manager for the specified syndication feed. var updater = TileUpdateManager.CreateTileUpdaterForApplication(); updater.EnableNotificationQueue(true); updater.Clear(); // Keep track of the number feed items that get tile notifications. int itemCount = 0; // Create a tile notification for each feed item. foreach (var item in feed.Items) { var tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText03); tileXml.GetElementsByTagName(textElementName)[0].InnerText = item.Title.Text; // Create a new tile notification. updater.Update(new TileNotification(tileXml)); // Don't create more than 5 notifications. if (itemCount++ > 5) break; } }
private FeedData DeserializeFeedData(SyndicationFeed feed) { try { FeedData 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) { // Use the date of the latest post as the last updated date. feedData.PublishDate = feed.Items[0].PublishedDate.DateTime; foreach (SyndicationItem item in feed.Items) { FeedItem feedItem = new FeedItem(); if (item.Title != null && item.Title.Text != null) { feedItem.Title = item.Title.Text; } if (item.PublishedDate != null) { feedItem.PublishDate = item.PublishedDate.DateTime; } if (item.Authors != null && item.Authors.Count > 0) { feedItem.Author = item.Authors[0].Name.ToString(); } // Handle the differences between RSS and Atom feeds. 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 || feed.SourceFormat == SyndicationFormat.Rss10) { 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; } } feedData.Items.Add(feedItem); } } return feedData; } catch { return null; } }
private async void RssSetPathCallback(String path) { RestClient client = new RestClient(); RestRequest request = new RestRequest(); client.Authority = path; request.Method = Method.Get; RestResponse response = await client.BeginRequest(request); if (response.Error == RestError.ERROR_SUCCESS && response.Content != null) { try { SyndicationFeed feed = new SyndicationFeed(); feed.Load(response.Content); FollowerSitePath = path; FollowerSiteName = feed.Title.Text; PreferenceHelper.SetPreference("RSS_FollowerPath", FollowerSitePath); PreferenceHelper.SetPreference("RSS_FollowerSite", FollowerSiteName); App.MainViewModel.IsChanged = true; } catch { FollowerSiteName = "未关注"; FollowerSitePath = "未设置"; PreferenceHelper.RemovePreference("RSS_FollowerPath"); PreferenceHelper.RemovePreference("RSS_FollowerSite"); DialogHelper.ShowMessageDialog("刚刚输入的为无效Rss地址", "悲剧鸟"); } } else { DialogHelper.ShowMessageDialog("刚刚输入的rss地址无效,请检查拼写正确,并确保网络畅通", "悲剧鸟"); } }
/// <summary> /// The timer callback for periodic updates /// </summary> /// <param name="state">A reference to the IoC <see cref="Container"/></param> private async void Callback(object state) { var container = state as Container; if (container == null) { return; } var viewModel = container.ResolveNamed<NewsViewModel>(NewsViewModel.Name); if (viewModel == null) { return; } var url = string.Empty; lock (m_Lock) { url = string.Format("http://www.reddit.com/r/{0}/new/.rss", m_Subreddit); } var feed = new SyndicationFeed(); var client = new HttpClient(); var rssString = await client.GetStringAsync(url); var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(rssString); feed.LoadFromXml(xmlDocument); DispatcherHelper.RunOnUIThread(() => { viewModel.FeedItems = feed.Items; }); }
///获取Rss目录列表 public static void GetRssItems(string rssFeed, Action<IEnumerable<RssItem>> onGetRssItemsCompleted = null, Action<string> onError = null, Action onFinally = null) { var request = HttpWebRequest.Create(rssFeed); request.Method = "GET"; request.BeginGetResponse((result) => { try { HttpWebRequest httpWebResquest = (HttpWebRequest)result.AsyncState; WebResponse webResponse = httpWebResquest.EndGetResponse(result); using (Stream stream = webResponse.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { string content = reader.ReadToEnd(); //将网络获取的信息转化成RSS实体类 List<RssItem> rssItems = new List<RssItem>(); SyndicationFeed feeds = new SyndicationFeed(); feeds.Load(content); foreach (SyndicationItem f in feeds.Items) { RssItem rssItem = new RssItem(f.Title.Text, f.Summary.Text, f.PublishedDate.ToString(), f.Links[0].Uri.AbsoluteUri); rssItems.Add(rssItem); } if (onGetRssItemsCompleted != null) { onGetRssItemsCompleted(rssItems); } } } } catch (WebException webEx) { string exceptionInfo = ""; switch (webEx.Status) { case WebExceptionStatus.ConnectFailure: exceptionInfo = "ConnectFaliure:远程服务器连接失败"; break; case WebExceptionStatus.MessageLengthLimitExceeded: exceptionInfo = "MessageLengthLimitExceeded:网络请求的信息长度受到限制"; break; case WebExceptionStatus.Pending: exceptionInfo = "Pending:内部异步请求挂起"; break; case WebExceptionStatus.RequestCanceled: exceptionInfo = "RequestCaneled:该请求将被取消"; break; case WebExceptionStatus.SendFailure: exceptionInfo = "SendFailure:发送失败,未能将完整请求发送到"; break; case WebExceptionStatus.UnknownError: exceptionInfo = "UnknownError:未知错误"; break; case WebExceptionStatus.Success: exceptionInfo = "Success :请求成功"; break; default: exceptionInfo = "未知网络异常"; break; } if (onError != null) onError(exceptionInfo); } catch (Exception e) { if (onError != null) onError("异常" + e.ToString()); } finally { if (onFinally != null) onFinally(); } }, request); }
public NewsFeed Parse(string rss) { var syndicationFeed = new SyndicationFeed(); syndicationFeed.Load(rss); return new NewsFeed(TransformRssFeedToNewsItems(syndicationFeed)); }
private static async Task<FeedData> RetrieveFeedAsync(Uri feedLink) { var feed = new SyndicationFeed(); var client = new SyndicationClient(); var feedData = new FeedData(); client.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); client.BypassCacheOnRetrieve = true; try { feed = await client.RetrieveFeedAsync(feedLink); } catch (Exception e) { SyndicationErrorStatus syndicationError = SyndicationError.GetStatus(e.HResult); if (syndicationError == SyndicationErrorStatus.Unknown) { WebErrorStatus webError = WebError.GetStatus(e.HResult); if (webError == WebErrorStatus.Unknown) { throw; } } return null; } if (feed.Title != null && !string.IsNullOrEmpty(feed.Title.Text)) { feedData.Title = feed.Title.Text; } if (feed.Subtitle != null && !string.IsNullOrEmpty(feed.Subtitle.Text)) { feedData.Description = feed.Subtitle.Text; } feedData.Link = feedLink; if (feed.Items != null && feed.Items.Any()) { feedData.PubDate = feed.Items.First().PublishedDate.DateTime; foreach (SyndicationItem item in feed.Items) { feedData.Items.Add(item.ToFeedItem(feed.SourceFormat)); } } return feedData; }
public async void Run(IBackgroundTaskInstance taskInstance) { Logger.Log("Starting task"); //it is an async work that could take long, we need to get a deferral... var deferral = taskInstance.GetDeferral(); try { CachedFileController fileController = new CachedFileController(); fileController.NotifyMessage += (o,message) => { ShowNotificationBadge(message); }; var asyncDownload= fileController.DownloadFileAsync(); asyncDownload.Progress = (o, p) => { taskInstance.Progress = (uint)(p.BytesReceived * 100 / (p.TotalBytesToReceive ?? (50 * 1024))); }; await asyncDownload; //now count feed posts var file = await fileController.GetFileAsync(); XmlDocument doc = new XmlDocument(); doc.LoadXml(file); SyndicationFeed feed = new SyndicationFeed(); feed.LoadFromXml(doc); ShowNotificationBadge(feed.Items.Count); } catch (Exception ex) { Logger.Log("Download File Exception: {0}", ex.Message); ShowNotificationBadge("error"); ShowNotificationBadge(0); } finally { Logger.Log("End Task"); deferral.Complete(); } }
public SyndicationItemIterator() { this.feed = null; this.index = 0; }
public void AttachFeed(SyndicationFeed feed) { this.feed = feed; this.index = 0; }
private Uri Search_ImageUri(SyndicationFeed feed) { foreach (var ele in feed.ElementExtensions) { if (ele.NodeName.ToLower() == "image") { try { return new Uri(ele.AttributeExtensions[0].Value); } catch (Exception) { return null; } } } return null; }
/// <summary> /// Extracts queues from the given HTTP response. /// </summary> /// <param name="response">HTTP response.</param> /// <returns>Collection of queues.</returns> private IEnumerable<QueueInfo> GetQueues(HttpResponseMessage response) { Debug.Assert(response.IsSuccessStatusCode); SyndicationFeed feed = new SyndicationFeed(); feed.Load(response.Content.ReadAsStringAsync().Result); return SerializationHelper.DeserializeCollection<QueueInfo>(feed, (item, queue) => queue.Initialize(item)); }