Example #1
0
        /// <summary>
        /// Import cached items, flags, etc.
        /// </summary>
        public void DoImportCache()
        {
            int totalFeeds     = Math.Max(_importedFeeds.Count, 1);
            int processedFeeds = 0;

            // Import cache
            foreach (FeedInfo fi in _importedFeeds)
            {
                ImportUtils.UpdateProgress(processedFeeds / totalFeeds, _progressMessageCache);
                processedFeeds += 100;
                if (fi.feed.IsDeleted)
                {
                    continue;
                }

                string path = Path.Combine(_cachePath, fi.cacheFile);
                if (!File.Exists(path))
                {
                    continue;
                }
                XmlDocument feed = new XmlDocument();
                try
                {
                    feed.Load(path);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("SharpReader cache '" + path + "' load failed: '" + ex.Message + "'");
                    continue;
                }


                // Load info from feed
                string s = null;
                ImportUtils.InnerText2Prop(feed.SelectSingleNode("/rss/Description") as XmlElement, fi.feed, Props.Description);
                ImportUtils.InnerText2Prop(feed.SelectSingleNode("/rss/WebPageUrl")  as XmlElement, fi.feed, Props.HomePage);

                ImportUtils.InnerText2Prop(feed.SelectSingleNode("/rss/Image/Title") as XmlElement, fi.feed, Props.ImageTitle);
                ImportUtils.InnerText2Prop(feed.SelectSingleNode("/rss/Image/Url")   as XmlElement, fi.feed, Props.ImageURL);
                ImportUtils.InnerText2Prop(feed.SelectSingleNode("/rss/Image/Link")  as XmlElement, fi.feed, Props.ImageLink);

                // Load items from feed
                FeedAuthorParser authParser = new FeedAuthorParser();
                foreach (XmlElement item in feed.SelectNodes("/rss/Items"))
                {
                    IResource feedItem = Core.ResourceStore.BeginNewResource("RSSItem");

                    ImportUtils.Child2Prop(item, "Title", feedItem, Core.Props.Subject);
                    ImportUtils.Child2Prop(item, "Description", feedItem, Core.Props.LongBody);
                    ImportUtils.Child2Prop(item, "Link", feedItem, Props.Link);
                    ImportUtils.Child2Prop(item, "Guid", feedItem, Props.GUID);
                    feedItem.SetProp(Core.Props.LongBodyIsHTML, true);
                    ImportUtils.Child2Prop(item, "CommentsUrl", feedItem, Props.CommentURL);
                    ImportUtils.Child2Prop(item, "CommentsRss", feedItem, Props.CommentRSS);
                    ImportUtils.Child2Prop(item, "Subject", feedItem, Props.RSSCategory);
                    s = ImportUtils.GetUniqueChildText(item, "CommentCount");
                    if (s != null)
                    {
                        try
                        {
                            feedItem.SetProp(Props.CommentCount, Int32.Parse(s));
                        }
                        catch (FormatException)
                        {
                            Trace.WriteLine("SharpReader cache: invalid comment-count");
                        }
                        catch (OverflowException)
                        {
                            Trace.WriteLine("SharpReader cache: invalid comment-count");
                        }
                    }

                    // Date
                    s = ImportUtils.GetUniqueChildText(item, "PubDate");
                    if (s != null)
                    {
                        DateTime dt = DateTime.Parse(s);
                        feedItem.SetProp(Core.Props.Date, dt);
                    }

                    // Read/unread
                    s = ImportUtils.GetUniqueChildText(item, "IsRead");
                    if (s == null || s != "true")
                    {
                        feedItem.SetProp(Core.Props.IsUnread, true);
                    }

                    // Flag
                    s = ImportUtils.GetUniqueChildText(item, "Flag");
                    if (s != null)
                    {
                        IResource flag = null;
                        if (_flagsMap.ContainsKey(s))
                        {
                            flag = (IResource)_flagsMap[s];
                        }
                        else
                        {
                            flag = _defaultFlag;
                        }
                        feedItem.AddLink("Flag", flag);
                    }
                    // Author
                    s = ImportUtils.GetUniqueChildText(item, "Author");
                    if (s != null)
                    {
                        authParser.ParseAuthorString(feedItem, s);
                    }
                    else
                    {
                        feedItem.AddLink(Core.ContactManager.Props.LinkFrom, fi.feed);
                    }

                    feedItem.EndUpdate();
                    fi.feed.AddLink(Props.RSSItem, feedItem);
                }
            }
            ImportUtils.UpdateProgress(processedFeeds / totalFeeds, _progressMessageCache);
        }
Example #2
0
        /// <summary>
        /// Import subscription
        /// </summary>
        public void DoImport(IResource importRoot, bool addToWorkspace)
        {
            IResource feedRes = null;

            importRoot = _plugin.FindOrCreateGroup("RssBandit subscriptions", importRoot);

            // We will add info about imported feeds here
            _importedFeeds = new ArrayList();

            ImportUtils.UpdateProgress(0, _progressMessage);
            // Start to import feeds structure
            XmlDocument feedlist = new XmlDocument();

            try
            {
                feedlist.Load(_subscriptionPath);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("RssBandit subscrption load failed: '" + ex.Message + "'");
                RemoveFeedsAndGroupsAction.DeleteFeedGroup(importRoot);
                ImportUtils.ReportError("RSS Bandit Subscription Import", "Import of RSS Bandit subscription failed:\n" + ex.Message);
                return;
            }

            ImportUtils.FeedUpdateData defaultUpdatePeriod;
            XmlAttribute period = feedlist.SelectSingleNode("/feeds/@refresh-rate") as XmlAttribute;

            if (period != null)
            {
                defaultUpdatePeriod = ImportUtils.ConvertUpdatePeriod(period.Value, 60000);
            }
            else
            {
                defaultUpdatePeriod = ImportUtils.ConvertUpdatePeriod("", 60000);
            }

            XmlNodeList feeds = feedlist.GetElementsByTagName("feed");

            int totalFeeds     = Math.Max(feeds.Count, 1);
            int processedFeeds = 0;

            ImportUtils.UpdateProgress(processedFeeds / totalFeeds, _progressMessage);
            foreach (XmlElement feed in feeds)
            {
                string s = ImportUtils.GetUniqueChildText(feed, "link");

                if (s == null)
                {
                    continue;
                }
                // May be, we are already subscribed?
                if (Core.ResourceStore.FindUniqueResource("RSSFeed", "URL", s) != null)
                {
                    continue;
                }

                FeedInfo info = new FeedInfo();
                info.url = s;

                IResource group = AddCategory(importRoot, feed.GetAttribute("category"));
                // Ok, now we should create feed
                feedRes = Core.ResourceStore.NewResource("RSSFeed");
                feedRes.BeginUpdate();
                feedRes.SetProp("URL", s);

                s = ImportUtils.GetUniqueChildText(feed, "title");
                ImportUtils.Child2Prop(feed, "title", feedRes, Core.Props.Name, Props.OriginalName);

                ImportUtils.Child2Prop(feed, "etag", feedRes, Props.ETag);

                s = ImportUtils.GetUniqueChildText(feed, "last-retrieved");
                if (s != null)
                {
                    DateTime dt = DateTime.Parse(s);
                    feedRes.SetProp("LastUpdateTime", dt);
                }

                // Peridoically
                ImportUtils.FeedUpdateData upd;
                s = ImportUtils.GetUniqueChildText(feed, "refresh-rate");
                if (s != null)
                {
                    upd = ImportUtils.ConvertUpdatePeriod(s, 60000);
                }
                else
                {
                    upd = defaultUpdatePeriod;
                }
                feedRes.SetProp("UpdatePeriod", upd.period);
                feedRes.SetProp("UpdateFrequency", upd.freq);

                // Cached?
                s = ImportUtils.GetUniqueChildText(feed, "cacheurl");
                if (s != null)
                {
                    info.cacheFile = s;
                }
                else
                {
                    info.cacheFile = null;
                }

                // Login & Password
                ImportUtils.Child2Prop(feed, "auth-user", feedRes, Props.HttpUserName);
                s = ImportUtils.GetUniqueChildText(feed, "auth-password");
                if (s != null)
                {
                    feedRes.SetProp(Props.HttpPassword, DecryptPassword(s));
                }

                // Enclosures
                ImportUtils.Child2Prop(feed, "enclosure-folder", feedRes, Props.EnclosurePath);

                // Try to load "read" list
                XmlElement read = ImportUtils.GetUniqueChild(feed, "stories-recently-viewed");
                if (read != null)
                {
                    ArrayList list = new ArrayList();
                    foreach (XmlElement story in read.GetElementsByTagName("story"))
                    {
                        list.Add(story.InnerText);
                    }
                    if (list.Count > 0)
                    {
                        info.readItems = list;
                    }
                    else
                    {
                        info.readItems = null;
                    }
                }
                // Feed is ready
                feedRes.AddLink(Core.Props.Parent, group);
                feedRes.EndUpdate();
                info.feed = feedRes;
                _importedFeeds.Add(info);
                if (addToWorkspace)
                {
                    Core.WorkspaceManager.AddToActiveWorkspace(feedRes);
                }

                processedFeeds += 100;
                ImportUtils.UpdateProgress(processedFeeds / totalFeeds, _progressMessage);
            }
            return;
        }