예제 #1
0
        public bool UpdateFeeds(List <FeedContract> feeds)
        {
            //var context = new TazehaContext();
            var ids     = feeds.Select(x => x.Id).ToList();
            var dbFeeds = _feedBusiness.GetList().Where(x => ids.Any(f => f == x.Id)).ToList();

            foreach (var dbfeed in dbFeeds)
            {
                dbfeed.LastUpdatedItemUrl = feeds.FirstOrDefault(x => x.Id == dbfeed.Id).LastFeedItemUrl;
                if (!string.IsNullOrEmpty(dbfeed.LastUpdatedItemUrl) && feeds.SingleOrDefault(f => f.Id == dbfeed.Id).FeedItems.Count > 0)
                {
                    dbfeed.UpdatingCount      = dbfeed.UpdatingCount == null ? 1 : dbfeed.UpdatingCount + 1;
                    dbfeed.LastUpdateDateTime = DateTime.Now;
                    _feedBusiness.CheckForChangeDuration(dbfeed, true);
                }
                else
                {
                    _feedBusiness.CheckForChangeDuration(dbfeed, false);
                }

                dbfeed.LastUpdaterVisit = DateTime.Now;

                _feedBusiness.Edit(dbfeed);
            }
            //context.SaveChanges();
            GeneralLogs.WriteLog("UpdateFeeds[AsService] : " + string.Join("[br /]", dbFeeds.Select(x => x.Link)), TypeOfLog.OK);
            return(true);
        }
예제 #2
0
        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);
        }