private void writeItem(RssItem item, int channelHashCode) { if (this.Writer == null) { throw new InvalidOperationException("RssWriter has been closed, and can not be written to."); } if (item == null) { throw new ArgumentNullException("Item must be instanciated with data to be written."); } if (!this.ChannelBegun) { throw new InvalidOperationException("Channel must be written first, before writing an item."); } this.BeginDocument(); this.Writer.WriteStartElement("item"); if ((item.Title == RssDefault.String) && (item.Description == RssDefault.String)) { throw new ArgumentException("item title and description cannot be null"); } this.WriteElement("title", item.Title, false); this.WriteElement("description", item.Description, false); this.WriteElement("link", item.Link, false); if (item.Source != null) { this.Writer.WriteStartElement("source"); this.WriteAttribute("url", item.Source.Url, true); this.Writer.WriteString(item.Source.Name); this.Writer.WriteEndElement(); } if (item.Enclosure != null) { this.Writer.WriteStartElement("enclosure"); this.WriteAttribute("url", item.Enclosure.Url, true); this.WriteAttribute("length", item.Enclosure.Length, true); this.WriteAttribute("type", item.Enclosure.Type, true); this.Writer.WriteEndElement(); } foreach (RssCategory category in item.Categories) { if (category.Name != RssDefault.String) { this.Writer.WriteStartElement("category"); this.WriteAttribute("domain", category.Domain, false); this.Writer.WriteString(category.Name); this.Writer.WriteEndElement(); } } this.WriteElement("author", item.Author, false); this.WriteElement("comments", item.Comments, false); if ((item.Guid != null) && (item.Guid.Name != RssDefault.String)) { this.Writer.WriteStartElement("guid"); if (!item.Guid.PermaLink.IsNull) { this.WriteAttribute("isPermaLink", (bool)item.Guid.PermaLink, false); } this.Writer.WriteString(item.Guid.Name); this.Writer.WriteEndElement(); } this.WriteElement("pubDate", item.PubDate, false); foreach (RssModule rssModule in this._rssModules) { if (rssModule.IsBoundTo(channelHashCode)) { foreach (RssModuleItemCollection rssModuleItemCollection in rssModule.ItemExtensions) { if (rssModuleItemCollection.IsBoundTo(item.GetHashCode())) { this.writeSubElements(rssModuleItemCollection, rssModule.NamespacePrefix); } } } } this.Writer.WriteEndElement(); this.Writer.Flush(); }
private void writeItem(RssItem.RssItem item, int channelHashCode) { if (writer == null) throw new InvalidOperationException("RssWriter has been closed, and can not be written to."); if (item == null) throw new ArgumentNullException("Item must be instanciated with data to be written."); if (!wroteChannel) throw new InvalidOperationException("Channel must be written first, before writing an item."); BeginDocument(); writer.WriteStartElement("item"); switch (rssVersion) { case RssVersion.RSS090: case RssVersion.RSS10: case RssVersion.RSS091: WriteElement("title", item.Title, true); WriteElement("description", item.Description, false); WriteElement("link", item.Link, true); break; case RssVersion.RSS20: if ((item.Title == RssDefault.String) && (item.Description == RssDefault.String)) throw new ArgumentException("item title and description cannot be null"); goto case RssVersion.RSS092; case RssVersion.RSS092: WriteElement("title", item.Title, false); WriteElement("description", item.Description, false); WriteElement("link", item.Link, false); if (item.Source != null) { writer.WriteStartElement("source"); WriteAttribute("url", item.Source.Url, true); writer.WriteString(item.Source.Name); writer.WriteEndElement(); } if (item.Enclosure != null) { writer.WriteStartElement("enclosure"); WriteAttribute("url", item.Enclosure.Url, true); WriteAttribute("length", item.Enclosure.Length, true); WriteAttribute("type", item.Enclosure.Type, true); writer.WriteEndElement(); } foreach (RssCategory category in item.Categories) if (category.Name != RssDefault.String) { writer.WriteStartElement("category"); WriteAttribute("domain", category.Domain, false); writer.WriteString(category.Name); writer.WriteEndElement(); } break; } if (rssVersion == RssVersion.RSS20) { WriteElement("author", item.Author, false); WriteElement("comments", item.Comments, false); if ((item.Guid != null) && (item.Guid.Name != RssDefault.String)) { writer.WriteStartElement("guid"); try { WriteAttribute("isPermaLink", (bool) item.Guid.PermaLink, false); } catch { } writer.WriteString(item.Guid.Name); writer.WriteEndElement(); } WriteElement("pubDate", item.PubDate, false); foreach (RssModule rssModule in _rssModules) { if (rssModule.IsBoundTo(channelHashCode)) { foreach (RssModuleItemCollection rssModuleItemCollection in rssModule.ItemExtensions) { if (rssModuleItemCollection.IsBoundTo(item.GetHashCode())) writeSubElements(rssModuleItemCollection, rssModule.NamespacePrefix); } } } } writer.WriteEndElement(); writer.Flush(); }
static void Main(string[] args) { RssFeed r = new RssFeed(); r.Version = RssVersion.RSS20; RssItem ri1a = new RssItem(); ri1a.Author = "Test Author 1a"; ri1a.Title = "Test Title 1a"; ri1a.Description = "Test Description 1a"; ri1a.Link = new Uri("http://www.yourserver.com/"); ri1a.PubDate = DateTime.Now; RssItem ri1b = new RssItem(); ri1b.Author = "Test Author 1b"; ri1b.Title = "Test Title 1b"; ri1b.Description = "Test Description 1b"; ri1b.Link = new Uri("http://www.yourserver.com/"); ri1b.PubDate = DateTime.Now; RssChannel rc1 = new RssChannel(); rc1.Items.Add(ri1a); rc1.Items.Add(ri1b); rc1.Title = "Test Channel Title 1"; rc1.Description = "Test Channel Description 1"; rc1.Link = new Uri("http://www.yourserver.com/channel.html"); rc1.PubDate = DateTime.Now; r.Channels.Add(rc1); RssPhotoAlbumCategoryPhotoPeople pacpp = new RssPhotoAlbumCategoryPhotoPeople("John Doe"); RssPhotoAlbumCategoryPhoto pacp1 = new RssPhotoAlbumCategoryPhoto(DateTime.Now.Subtract(new TimeSpan(2, 12, 0, 0)), "Test Photo Description 1", new Uri("http://www.yourserver.com/PhotoAlbumWeb/GetPhoto.aspx?PhotoID=123"), pacpp); RssPhotoAlbumCategoryPhoto pacp2 = new RssPhotoAlbumCategoryPhoto(DateTime.Now.Subtract(new TimeSpan(2, 10, 0, 0)), "Test Photo Description 2", new Uri("http://www.yourserver.com/PhotoAlbumWeb/GetPhoto.aspx?PhotoID=124")); RssPhotoAlbumCategoryPhoto pacp3 = new RssPhotoAlbumCategoryPhoto(DateTime.Now.Subtract(new TimeSpan(2, 10, 0, 0)), "Test Photo Description 2", new Uri("http://www.yourserver.com/PhotoAlbumWeb/GetPhoto.aspx?PhotoID=125")); RssPhotoAlbumCategoryPhotos pacps = new RssPhotoAlbumCategoryPhotos(); pacps.Add(pacp1); pacps.Add(pacp2); RssPhotoAlbumCategory pac1 = new RssPhotoAlbumCategory("Test Photo Album Category 1", "Test Photo Album Category Description 1", DateTime.Now.Subtract(new TimeSpan(5, 10, 0, 0)), DateTime.Now, pacps); RssPhotoAlbumCategory pac2 = new RssPhotoAlbumCategory("Test Photo Album Category 2", "Test Photo Album Category Description 2", DateTime.Now.Subtract(new TimeSpan(9, 10, 0, 0)), DateTime.Now, pacp3); RssPhotoAlbumCategories pacs = new RssPhotoAlbumCategories(); pac1.BindTo(ri1a.GetHashCode()); pac2.BindTo(ri1b.GetHashCode()); pacs.Add(pac1); pacs.Add(pac2); RssPhotoAlbum pa = new RssPhotoAlbum(new Uri("http://your.web.server/PhotoAlbumWeb"), pacs); pa.BindTo(rc1.GetHashCode()); r.Modules.Add(pa); RssItem ri2 = new RssItem(); ri2.Author = "Test Author 2"; ri2.Title = "Test Title 2"; ri2.Description = "Test Description 2"; ri2.Link = new Uri("http://www.yourotherserver.com/"); ri2.PubDate = DateTime.Now; RssChannel rc2 = new RssChannel(); rc2.Items.Add(ri2); rc2.Title = "Test Channel Title 2"; rc2.Description = "Test Channel Description 2"; rc2.Link = new Uri("http://www.yourotherserver.com/channel.html"); rc2.PubDate = DateTime.Now; r.Channels.Add(rc2); r.Write("out.xml"); RssBlogChannel rbc = new RssBlogChannel(new Uri("http://www.google.com"), new Uri("http://www.google.com"), new Uri("http://www.google.com"), new Uri("http://www.google.com")); }
private DownloadPackage GetDownloadPackage(Rss.RssItem rssItem, FeedItem feedItem, string strDownloadlocation) { bool boolAcceptableFile = false; bool byteRanging = true; string strFilenameWithoutExt = null; string strFilenameExt = null; string strPath = null; DirectoryInfo dirInfo = null; FileInfo[] fileInfo = null; long longReturn = 0; //bool boolGuid = false; string strFilename = null; // new WebResponse response = null; // first try a HEAD call (less intrusive) if (feedItem.Authenticate == true) { string strPassword = EncDec.Decrypt(feedItem.Password, feedItem.Username); response = GetWebResponseObjectHEAD(rssItem.Enclosure.Url, feedItem.Username, strPassword); } else { response = GetWebResponseObjectHEAD(rssItem.Enclosure.Url, "", ""); } // the HEAD call didn't seem to work. We'll use a GET now if (response == null) { if (feedItem.Authenticate == true) { string strPassword = EncDec.Decrypt(feedItem.Password, feedItem.Username); response = GetWebResponseObjectGET(rssItem.Enclosure.Url, feedItem.Username, strPassword); } else { response = GetWebResponseObjectGET(rssItem.Enclosure.Url, "", ""); } } if (response != null) { long longDownloadsize = response.ContentLength; if (longDownloadsize == -1) { longDownloadsize = 1; } WebHeaderCollection responseHeaders = response.Headers; string[] strAcceptRange = responseHeaders.GetValues("Accept-Ranges"); if (strAcceptRange != null && strAcceptRange[0] == "none") { // no byte ranging byteRanging = false; } // check if there is content-disposition header string[] strContDisp = responseHeaders.GetValues("content-disposition"); string strAttachment = ""; string strHeader = ""; if (strContDisp != null && strContDisp.Length > 0) { for (int l = 0; l < strContDisp.Length; l++) { strHeader = strContDisp[l]; if (strHeader.ToLower().StartsWith("attachment")) { // attachment header strAttachment = strHeader.Substring(strHeader.ToLower().IndexOf("filename=") + 9); } } } if (strAttachment != "") { if (feedItem.UseTitleForFiles) { FileInfo f = new FileInfo(strAttachment); strPath = Utils.GetValidFileName(rssItem.Title + f.Extension); } else { strPath = strAttachment; strPath = Utils.GetValidFileName(strPath); } strFilename = strDownloadlocation + "\\" + strPath; if (strFilename.IndexOf(".") > 0) { FileInfo f = new FileInfo(strFilename); strFilenameWithoutExt = f.FullName.Substring(0, f.FullName.Length - f.Extension.Length); //strFilenameWithoutExt = strFilename.Substring(0, strFilename.LastIndexOf(".")); strFilenameExt = f.Extension.Substring(1); //strFilenameExt = strFilename.Substring(strFilename.LastIndexOf(".") + 1); } else { strFilenameWithoutExt = strFilename; strFilenameExt = ""; } } else { try { Uri uriEnclosure = new Uri(rssItem.Enclosure.Url); string strEnclosureUrl = uriEnclosure.GetLeftPart(UriPartial.Path); if (feedItem.UseTitleForFiles) { strPath = strEnclosureUrl.Substring(strEnclosureUrl.LastIndexOf("/") + 1); FileInfo f = new FileInfo(strPath); strPath = Utils.GetValidFileName(rssItem.Title + f.Extension); } else { strPath = strEnclosureUrl.Substring(strEnclosureUrl.LastIndexOf("/") + 1); } if (strPath == "") { // check the query strEnclosureUrl = uriEnclosure.GetLeftPart(UriPartial.Query); if (feedItem.UseTitleForFiles) { strPath = strEnclosureUrl.Substring(strEnclosureUrl.LastIndexOf("/") + 1); FileInfo f = new FileInfo(strPath); strPath = Utils.GetValidFileName(rssItem.Title + f.Extension); } else { strPath = strEnclosureUrl.Substring(strEnclosureUrl.LastIndexOf("/") + 1); } } // empty path? not good. Generate one //strPath = System.Web.HttpUtility.UrlDecode(strPath); strFilename = strDownloadlocation + "\\" + strPath; FileInfo fileInfo1 = new FileInfo(strFilename); strFilenameWithoutExt = fileInfo1.FullName.Substring(0, fileInfo1.FullName.Length - fileInfo1.Extension.Length); //strFilenameWithoutExt = strFilename.Substring(0, strFilename.LastIndexOf(".")); strFilenameExt = fileInfo1.Extension.Substring(1); //strFilenameExt = strFilename.Substring(strFilename.LastIndexOf(".") + 1); //strFilenameWithoutExt = strFilename.Substring(0, strFilename.LastIndexOf(".")); //strFilenameExt = strFilename.Substring(strFilename.LastIndexOf(".") + 1); } catch { strFilename = ""; strFilenameExt = ""; strFilenameWithoutExt = ""; boolAcceptableFile = false; } } if (Settings.Default.DefaultMediaAction == 0) { boolAcceptableFile = true; } else { foreach (string extension in Utils.AudioExtensions) { if (extension.ToUpper() == strFilenameExt.ToUpper()) { boolAcceptableFile = true; break; } } if (!boolAcceptableFile) { // check if it is a video file foreach (string extension in Utils.VideoExtensions) { if (extension.ToUpper() == strFilenameExt.ToUpper()) { boolAcceptableFile = true; break; } } } } if (boolAcceptableFile) { try { dirInfo = new System.IO.DirectoryInfo(strDownloadlocation); fileInfo = dirInfo.GetFiles(strPath); } catch (Exception fex) { if (Settings.Default.LogLevel > 0) { log.Error("Retriever", fex); } strFilenameExt = ""; } // check if the file is available on the filesystem bool boolContinue = true; if (fileInfo.Length > 0) { if ((fileInfo.Length > 0 && fileInfo[0].Length != longDownloadsize)) { // there is a file with the same name switch (Settings.Default.DuplicateAction) { case 1: strFilename = strFilenameWithoutExt + DateTime.Now.ToString("yyyyMMddTHHMMss") + "." + strFilenameExt; boolContinue = true; break; default: boolContinue = false; break; } } else if (fileInfo.Length > 0) { // add the item to the history HistoryItem historyItem = new HistoryItem(); historyItem.FeedGUID = feedItem.GUID; historyItem.FeedUrl = feedItem.Url; historyItem.Title = rssItem.Title; historyItem.Hashcode = rssItem.GetHashCode(); //historyItem.LocalFilename = fileInfo[0].FullName; historyItem.ItemDate = fileInfo[0].CreationTime.ToString("yyyy-MM-dd HH:MM:ss"); historyItem.FileName = fileInfo[0].Name; //historyItem.URL = enc.EnclosureURL; Settings.Default.History.Add(historyItem); boolContinue = false; } else { boolContinue = false; } } if (boolContinue) { // did the user specify a text filter on the item? if (feedItem.Textfilter != null && feedItem.Textfilter != "") { System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(feedItem.Textfilter); if (regEx.Match(rssItem.Description).Success || regEx.Match(rssItem.Title).Success) { // if(rssItem.Description.ToUpper().IndexOf(feedItem.textFilter.ToUpper()) >= 0 || rssItem.Title.ToUpper().IndexOf(feedItem.textFilter.ToUpper()) >= 0) // { longReturn = longDownloadsize; } else if (rssItem.Description.ToUpper().IndexOf(feedItem.Textfilter.ToUpper()) >= 0 || rssItem.Title.ToUpper().IndexOf(feedItem.Textfilter.ToUpper()) >= 0) { longReturn = longDownloadsize; } } else { longReturn = longDownloadsize; } } } } DownloadPackage downloadPackage = new DownloadPackage(); downloadPackage.ByteRanging = byteRanging; downloadPackage.DownloadSize = longReturn; downloadPackage.strFilename = strFilename; return(downloadPackage); }