コード例 #1
0
        public bool SendFeeds(List <FeedContract> feeds)
        {
            List <FeedItem> items = feeds.SelectMany(feed => feed.FeedItems.Select(item => new FeedItem
            {
                Id              = Guid.NewGuid(),
                CreateDate      = DateTime.Now,
                Description     = item.Description,
                Link            = item.Link,
                PubDate         = item.PubDate,
                SiteId          = feed.SiteId,
                FeedId          = feed.Id,
                SiteTitle       = feed.SiteTitle,
                SiteUrl         = feed.SiteUrl,
                Title           = item.Title,
                ShowContentType = feed.ShowContentType
            })).ToList();

            //IRepositorySaver saver = new LuceneSaverRepository();
            //saver.AddItems(items);
            _feedItemBusiness.AddItems(items);

            UpdateFeeds(feeds);

            NumberOfNewItemsToday += items.Count;

            //if (NumberOfNewItemsToday - Last_NumberOfNewItemsToday > 50)
            //{
            //    Optimize();
            //    Last_NumberOfNewItemsToday = NumberOfNewItemsToday;
            //}
            //Optimize();
            return(true);
        }
コード例 #2
0
ファイル: FeedUpdater.cs プロジェクト: navaei/NewsCMS
        public int UpdatingFeed(Feed dbfeed, bool saveItems = true)
        {
            int NumberOfNewItem = 0;
            var items           = new List <FeedItem>();
            var log             = new FeedLog()
            {
                FeedId = dbfeed.Id
            };

            try
            {
                if (dbfeed.FeedType == 0 || !dbfeed.FeedType.HasValue)
                {
                    #region Feed
                    RssFeed feed = RssFeed.Read(dbfeed.Link);
                    //-----------shart check kardane inke feed aslan update shode dar site marja ya kheir------------
                    if (feed == null)
                    {
                        dbfeed.Deleted = Common.Share.DeleteStatus.NotWork;
                    }
                    else if (feed.Channels.Count > 0)
                    {
                        //--------Feed has new items-----------
                        if (feed.Channels.Count > 0)
                        {
                            RssChannel channel = (RssChannel)feed.Channels[0];

                            if (channel.Items.LatestPubDate() != channel.Items[0].PubDate)
                            {
                                items = new FeedItemsOperation(_appConfigBiz).RssItemsToFeedItems(channel.ItemsSorted, dbfeed);
                            }
                            else
                            {
                                items = new FeedItemsOperation(_appConfigBiz).RssItemsToFeedItems(channel.Items, dbfeed);
                            }


                            //----------Visual Items---------
                            //NumberOfNewItem = listReturnBack.Count;
                            //if (NumberOfNewItem > 0)
                            //{
                            //    if (dbfeed.InIndex == 1)
                            //    {
                            //        if (dbfeed.Site.HasSocialTag.HasValue && dbfeed.Site.HasSocialTag.Value)
                            //            Updater.FeedItemImage.setFeedItemsImage(dbfeed, listReturnBack, 5);
                            //        //else
                            //        //    FeedItemsOperation.insertFeedItemsIndex(dbfeed, listReturnBack, 5);
                            //    }
                            //    entiti.SaveChanges();
                            //}
                        }
                    }
                    #endregion
                }
                else if (dbfeed.FeedType.HasValue && dbfeed.FeedType.Value == Common.Share.FeedType.Atom)
                {
                    #region Atom
                    XmlReader       reader   = XmlReader.Create(dbfeed.Link);
                    SyndicationFeed atomfeed = SyndicationFeed.Load(reader);
                    int             i        = 0;
                    if (atomfeed == null)
                    {
                        dbfeed.Deleted = Common.Share.DeleteStatus.NotWork;
                    }
                    else if (atomfeed.Items.Any())
                    {
                        foreach (SyndicationItem item in atomfeed.Items)
                        {
                            i++;
                            break;
                        }
                        if (i > 0)
                        {
                            items = new FeedItemsOperation(_appConfigBiz).AtomItemsToFeedItems(atomfeed.Items, dbfeed);
                        }
                    }
                    #endregion
                }

                if (items.Any())
                {
                    if (dbfeed.Site.HasImage != HasImage.NotSupport)
                    {
                        new FeedItemImage(_appConfigBiz).SetItemsImage(items, dbfeed);
                    }
                    if (saveItems)
                    {
                        NumberOfNewItem = ItemBiz.AddItems(items);
                        if (NumberOfNewItem > 0)
                        {
                            dbfeed.LastUpdatedItemUrl = items.Last().Link.SubstringM(0, 400);
                        }
                    }

                    GeneralLogs.WriteLog("Updating feed " + dbfeed.Id + "  Items:" + NumberOfNewItem + "  New:" + NumberOfNewItem + " " + dbfeed.Link.Replace("http://", ""), TypeOfLog.OK, typeof(FeedUpdater));
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.IndexOf("404") > 0)
                {
                    dbfeed.Deleted = Common.Share.DeleteStatus.NotFound;
                }
                else if (ex.Message.IndexOf("403") > 0)
                {
                    dbfeed.Deleted = Common.Share.DeleteStatus.Forbidden;
                }
                else if (ex.Message.IndexOfX("timed out") > 0)
                {
                    dbfeed.Deleted = Common.Share.DeleteStatus.RequestTimeOut;
                }
                else
                {
                    dbfeed.Deleted = Common.Share.DeleteStatus.Temporary;
                }

                log.HasError = true;
                log.Message  = "Error:" + (ex.InnerException != null ? ex.InnerException.Message.SubstringM(0, 512) : ex.Message.SubstringM(0, 512));
                FeedBiz.CreateFeedLog(log);
                var msg = "FeedId " + dbfeed.Id + " " + dbfeed.Link.SubstringM(0, 40) + " Error:" + (ex.InnerException != null ? ex.InnerException.Message.SubstringM(0, 512) : ex.Message.SubstringM(0, 512));
                GeneralLogs.WriteLog(msg, TypeOfLog.Error, typeof(FeedUpdater));

                FeedBiz.Edit(dbfeed);
                return(NumberOfNewItem);
            }

            dbfeed.LastUpdaterVisit = DateTime.Now;

            if (saveItems)
            {
                FeedBiz.CheckForChangeDuration(dbfeed, NumberOfNewItem > 0 ? true : false);
                if (NumberOfNewItem > 0)
                {
                    dbfeed.LastUpdateDateTime = DateTime.Now;
                }
            }

            var res = FeedBiz.Edit(dbfeed);
            if (saveItems && res.Status)
            {
                log.HasError   = false;
                log.Message    = "Successfull Updat Feed " + dbfeed.Title.SubstringM(0, 16) + " " + dbfeed.Link.Replace("http://", "").SubstringM(0, 24);
                log.ItemsCount = NumberOfNewItem;
                FeedBiz.CreateFeedLog(log);
            }

            return(saveItems ? NumberOfNewItem : items.Count);
        }