Пример #1
0
        RssChannelDom DownloadChannelDom(string url)
        {
            // look for disk cache first
            RssChannelDom dom = TryLoadFromDisk(url);

            if (dom != null)
            {
                return(dom);
            }

            // download the feed
            byte[] feed = new WebClient().DownloadData(url);

            // parse it as XML
            XmlDocument doc = new XmlDocument();

            doc.Load(new MemoryStream(feed));

            // parse into DOM
            dom = RssXmlHelper.ParseChannelXml(doc);

            // set expiry
            string ttlString = null;

            dom.Channel.TryGetValue("ttl", out ttlString);
            int      ttlMinutes = GetTtlFromString(ttlString, _defaultTtlMinutes);
            DateTime utcExpiry  = DateTime.UtcNow.AddMinutes(ttlMinutes);

            dom.SetExpiry(utcExpiry);

            // save to disk
            TrySaveToDisk(doc, url, utcExpiry);

            return(dom);
        }
Пример #2
0
        protected void LoadFromXml(XmlDocument doc)
        {
            // parse XML
            RssChannelDom dom = RssXmlHelper.ParseChannelXml(doc);

            // create the channel
            LoadFromDom(dom);
        }
Пример #3
0
        internal XmlDocument SaveAsXml()
        {
            XmlDocument doc         = RssXmlHelper.CreateEmptyRssXml();
            XmlNode     channelNode = RssXmlHelper.SaveRssElementAsXml(doc.DocumentElement, this, "channel");

            if (_image != null)
            {
                RssXmlHelper.SaveRssElementAsXml(channelNode, _image, "image");
            }

            foreach (RssItemType item in _items)
            {
                RssXmlHelper.SaveRssElementAsXml(channelNode, item, "item");
            }

            return(doc);
        }
Пример #4
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Simple RSS Compiler v2.0");
            Console.WriteLine();

            // Process command line
            if (args.Length != 2)
            {
                Console.WriteLine("usage: rssdl.exe url-or-file outputcode.cs");
                return;
            }

            string url             = args[0];
            string codeFilename    = args[1];
            string classNamePrefix = Path.GetFileNameWithoutExtension(codeFilename);

            // Load the channel data from supplied url
            string codeString;

            try
            {
                using (Stream feedStream = DownloadManager.GetFeed(url))
                {
                    using (XmlTextReader reader = new XmlTextReader(feedStream))
                    {
                        codeString = RssXmlHelper.ConvertToRssXml(reader);

                        try
                        {
                            // Open the output code file
                            using (TextWriter codeWriter = new StreamWriter(codeFilename, false))
                            {
                                // Get the language from file extension
                                string lang = Path.GetExtension(codeFilename);

                                if (lang != null && lang.Length > 1 && lang.StartsWith("."))
                                {
                                    lang = lang.Substring(1).ToUpperInvariant();
                                }
                                else
                                {
                                    lang = "CS";
                                }

                                // Generate source
                                try
                                {
                                    RssCodeGenerator.GenerateCode(codeString, url, lang, string.Empty, classNamePrefix, codeWriter, true);
                                    Console.WriteLine("Done -- generated '{0}'.", codeFilename);
                                    return;
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine("*** Error generating '{0}' *** {1}: {2}", codeFilename, e.GetType().Name, e.Message);
                                }
                            }

                            File.Delete(codeFilename);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("*** Failed to open '{0}' for writing *** {1}: {2}", codeFilename, e.GetType().Name, e.Message);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("*** Failed to load '{0}' *** {1}: {2}", url, e.GetType().Name, e.Message);
            }
        }
Пример #5
0
        RssChannelDom TryLoadFromDisk(string url)
        {
            if (_directoryOnDisk == null)
            {
                return(null); // no place to cache
            }

            // look for all files matching the prefix
            // looking for the one matching url that is not expired
            // removing expired (or invalid) ones
            string pattern = GetTempFileNamePrefixFromUrl(url) + "_*.rss";

            string[] files = Directory.GetFiles(_directoryOnDisk, pattern, SearchOption.TopDirectoryOnly);

            foreach (string rssFilename in files)
            {
                XmlDocument rssDoc               = null;
                bool        isRssFileValid       = false;
                DateTime    utcExpiryFromRssFile = DateTime.MinValue;
                string      urlFromRssFile       = null;

                try {
                    rssDoc = new XmlDocument();
                    rssDoc.Load(rssFilename);

                    // look for special XML comment (before the root tag)'
                    // containing expiration and url
                    XmlComment comment = rssDoc.DocumentElement.PreviousSibling as XmlComment;

                    if (comment != null)
                    {
                        string c = comment.Value;
                        int    i = c.IndexOf('@');
                        long   expiry;

                        if (long.TryParse(c.Substring(0, i), out expiry))
                        {
                            utcExpiryFromRssFile = DateTime.FromBinary(expiry);
                            urlFromRssFile       = c.Substring(i + 1);
                            isRssFileValid       = true;
                        }
                    }
                }
                catch {
                    // error processing one file shouldn't stop processing other files
                }

                // remove invalid or expired file
                if (!isRssFileValid || utcExpiryFromRssFile < DateTime.UtcNow)
                {
                    try {
                        File.Delete(rssFilename);
                    }
                    catch {
                    }

                    // try next file
                    continue;
                }

                // match url
                if (urlFromRssFile == url)
                {
                    // found a good one - create DOM and set expiry (as found on disk)
                    RssChannelDom dom = RssXmlHelper.ParseChannelXml(rssDoc);
                    dom.SetExpiry(utcExpiryFromRssFile);
                    return(dom);
                }
            }

            // not found
            return(null);
        }