//-----------------console-------------------------- #region feeds public static RssItemCollection GetRssItemCollection(this SyndicationFeed atomfeed) { int i = 0; List <RssItem> retlist = new List <RssItem>(); RssItemCollection colection = new RssItemCollection(); if (atomfeed.Items.Any()) { foreach (SyndicationItem item in atomfeed.Items) { i++; break; } } if (i > 0) { retlist = atomfeed.Items.Select(x => new RssItem { Title = x.Title.Text, Description = x.Summary.Text, Link = x.Links[0].Uri, PubDate = x.PublishDate.DateTime }).ToList(); } foreach (var item in retlist.OrderBy(x => x.PubDate)) { colection.Add(item); } return(colection); }
/// <summary> /// Gets the collection as a collection of RSS items. /// </summary> /// <returns></returns> public RssItemCollection ToRssItemCollection() { RssItemCollection items = new RssItemCollection(); foreach (object entity in this.InnerList) { items.Add(((IRssItem)entity).ToRssItem()); } // return... return(items); }
/// <summary>Does read the target URL.</summary> /// <param name="state"></param> public void DoRead(Object state) { _mutex.WaitOne(); if (!_activated) { _mutex.ReleaseMutex(); return; } Log.AddInfo(@"RssWatcher.DoRead() : " + this._targetInfo.Title); string url = _targetInfo.Url; #if ZEPTAIR if (_targetInfo.IsZeptDist) { Zeptair.Lib.Common.ConfParam zeptConf = TaskMain.Instance.ZeptConf(false); if (zeptConf.SpecifyAdminNames && zeptConf.AdminNames != null && zeptConf.AdminNames.Length > 0) { if (url.IndexOf('?') >= 0) { url += @"&"; } else { url += @"?"; } url += @"admins=" + zeptConf.AdminNames; } } #endif RssFeed feed = null; try { ServicePointManager.ServerCertificateValidationCallback = ThetisCore.Lib.EasyTrustPolicy.CheckValidationResult; HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url); if (_targetInfo.UserName != null && _targetInfo.UserName.Length > 0) { webReq.Credentials = new NetworkCredential(_targetInfo.UserName, _targetInfo.Password); } if (_lastFeed == null) { feed = RssFeed.Read(webReq); } else { feed = RssFeed.Read(webReq, _lastFeed); } _lastFeed = feed; } catch (WebException we) { Log.AddError(" " + we.Message + "\n" + we.StackTrace); } if (feed != null && !feed.Cached) { bool modified = false; ArrayList postArray = new ArrayList(); if (_feedHistory == null) { _feedHistory = feed; foreach (RssChannel channel in feed.Channels) { foreach (RssItem item in channel.Items) { postArray.Add(InfoItem.Create(item, channel.Title, _targetInfo)); } } modified = true; } else { foreach (RssChannel channel in feed.Channels) { int historyIdx = RssHelper.IndexOf(_feedHistory.Channels, channel); if (historyIdx < 0) { _feedHistory.Channels.Add(channel); foreach (RssItem item in channel.Items) { postArray.Add(InfoItem.Create(item, channel.Title, _targetInfo)); } modified = true; } else { RssItemCollection historyItems = _feedHistory.Channels[historyIdx].Items; foreach (RssItem item in channel.Items) { if (!RssHelper.Contains(historyItems, item)) { historyItems.Add(item); postArray.Add(InfoItem.Create(item, channel.Title, _targetInfo)); modified = true; } } } } } if (modified) { _feedHistory.LastModified = feed.LastModified; _targetInfo.SaveFeedHistory(_feedHistory); _manager.PostItems(postArray, _targetInfo); postArray.Clear(); } postArray = null; } _mutex.ReleaseMutex(); }
protected void Page_Load(object sender, EventArgs e) { string decodedURL = HttpUtility.HtmlDecode(Request.QueryString["url"]); string decodedFilterString = HttpUtility.HtmlDecode(Request.QueryString["q"]); string[] filterStrings = decodedFilterString.Split(new char[] { ' ', '�@' }, StringSplitOptions.RemoveEmptyEntries); bool checkDescription = Request.QueryString["Description"] != null; bool checkTitle = true; if (checkDescription) { checkTitle = Request.QueryString["title"] != null; } RssFeed feed; try { feed = RssFeed.Read(decodedURL); } catch (Exception ex) { Response.ContentType = "text/plain"; Response.Write(ex.Message); Response.End(); return; } if (feed.Channels.Count == 0) { Response.ContentType = "text/plain"; Response.Write("This feed has no channel."); Response.End(); return; } //Feed��o�^ lock (Application["feedsDT"]) { FeedsDS.FeedsDTDataTable feedsDT = Application["feedsDT"] as FeedsDS.FeedsDTDataTable; FeedsDS.FeedsDTRow row = feedsDT.FindByFilterStringFeedURL(decodedFilterString, decodedURL); if (null == row) { // insert feedsDT.AddFeedsDTRow(decodedURL, decodedFilterString, checkTitle, checkDescription, Request.Url.AbsoluteUri, DateTime.Now); } else { //update row.TargetTitle = checkTitle; row.TargetDescription = checkDescription; row.ResistDate = DateTime.Now; } WriteFeedXml(feedsDT); } //main from here foreach (RssChannel channel in feed.Channels) { RssItemCollection newItems = new RssItemCollection(); foreach (RssItem item in channel.Items) { StringBuilder targetString = new StringBuilder(); if (checkTitle) { targetString.Append(item.Title); } if (checkDescription) { targetString.Append(item.Description); } if (!IsFilteredEntry(targetString.ToString(), filterStrings)) { newItems.Add(item); } } if (newItems.Count == 0) { Response.ContentType = "text/plain"; Response.Write("Channel must contain at least one item."); Response.End(); return; } channel.Items.Clear(); foreach (RssItem item in newItems) { channel.Items.Add(item); } } feed.Encoding = feed.Encoding ?? Encoding.UTF8; Response.ContentType = "text/xml"; feed.Write(Response.OutputStream); Response.End(); } bool IsFilteredEntry(string targetString, string[] filterStrings) { string targetStringLow = targetString.ToLower(); foreach (string filterString in filterStrings) { string filterStringLow = filterString.ToLower(); if (filterStringLow.StartsWith("-")) { if (targetStringLow.Contains(filterStringLow.Substring(1))) { return(true); } } else { if (!targetStringLow.Contains(filterStringLow)) { return(true); } } } return(false); } void WriteFeedXml(FeedsDS.FeedsDTDataTable feedsDT) { string feedsFileName = Path.Combine(Server.MapPath("./"), ConfigurationManager.AppSettings["FeedsFilePath"]); if (!File.Exists(feedsFileName)) { Directory.CreateDirectory(Path.GetDirectoryName(feedsFileName)); } feedsDT.WriteXml(feedsFileName); } } // end of class