Inheritance: DbContext
Beispiel #1
0
        public static void MyClassInitialize(TestContext testContext)
        {
            UpdateHtmlForm updateForm = new UpdateHtmlForm();
            updateForm.ShowDialog();

            using (var ctx = new NijieContext())
            {
                ctx.Database.Delete();
                ctx.SaveChanges();
            }
        }
        public MainWindow()
        {
            Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            checkUpgrade();
            InitializeComponent();

            ConfigureBot();

            Log.Info(AppName + " started.");

            using (var ctx = new NijieContext())
            {
                var count = ctx.Images.Count();
                Log.Info(string.Format("Tracking {0} image(s)", count));
            }

            // prevent sleep config
            NijieDownloader.UI.Settings.Download.SetPreventSleep();

            Application.Current.MainWindow = this;
        }
Beispiel #3
0
 private void SaveImageToDB(JobDownloadViewModel job, NijieImage image)
 {
     try
     {
         lock (_dbLock)
         {
             using (var dao = new NijieContext())
             {
                 if (Properties.Settings.Default.TraceDB)
                 {
                     dao.Database.Log = MainWindow.Log.Debug;
                 }
                 image.SaveToDb(dao);
             }
         }
     }
     catch (Exception ex)
     {
         MainWindow.Log.Error("Failed to save to DB: " + image.ImageId, ex);
         job.Message += ex.Message;
     }
 }
Beispiel #4
0
        /// <summary>
        /// Parse the image page
        /// - Illustration
        /// - Manga
        /// </summary>
        /// <param name="job"></param>
        /// <param name="memberPage"></param>
        /// <param name="imageTemp"></param>
        private void processImage(JobDownloadViewModel job, NijieMember memberPage, NijieImage imageTemp)
        {
            if (isJobCancelled(job))
                return;

            MainWindow.Log.Debug("Processing Image:" + imageTemp.ImageId);

            // skip if exists in DB
            if (Properties.Settings.Default.SkipIfExistsInDB && !NijieDownloader.Library.Properties.Settings.Default.Overwrite)
            {
                using (var dao = new NijieContext())
                {
                    var result = NijieImage.IsDownloadedInDB(imageTemp.ImageId);
                    if (result)
                    {
                        job.Message = String.Format("Image {0} already downloaded in DB", imageTemp.ImageId);
                        job.SkipCount++;
                        return;
                    }
                }
            }

            var image = MainWindow.Bot.ParseImage(imageTemp, memberPage);

            if (image.IsFriendOnly)
            {
                // sample: 74587
                job.Message = "Image locked!";
                return;
            }
            if (image.IsGoldenMember)
            {
                job.Message = "Image only for Gold Membership";
                return;
            }

            if (image.IsManga)
            {
                processManga(job, image);
            }
            else
            {
                processIllustration(job, image);
            }
        }
Beispiel #5
0
        public void SaveToDb(NijieContext dao, bool suppressSave = false)
        {
            var member = (from x in dao.Members
                          where x.MemberId == this.Member.MemberId
                          select x).FirstOrDefault();
            if (member != null)
            {
                this.Member = member;
            }

            var temp = new List<NijieTag>();
            for (int i = 0; i < this.Tags.Count; ++i)
            {
                var t = this.Tags.ElementAt(i);
                var x = (from a in dao.Tags
                         where a.Name == t.Name
                         select a).FirstOrDefault();
                if (x != null)
                {
                    temp.Add(x);
                }
                else
                {
                    temp.Add(t);
                }
            }
            this.Tags = temp;

            dao.Images.AddOrUpdate(this);

            if (!suppressSave)
            {
                dao.SaveChanges();
            }

            Debug.Assert(this.WorkDate != DateTime.MinValue, "Works Date cannot be set to DateTime.MinValue");
        }
Beispiel #6
0
 public static bool IsDownloadedInDB(int imageId)
 {
     using (var dao = new NijieContext())
     {
         var image = (from i in dao.Images
                      where i.ImageId == imageId
                      select i).FirstOrDefault();
         if (image != null)
         {
             if (!string.IsNullOrWhiteSpace(image.SavedFilename))
                 return true;
         }
     }
     return false;
 }
Beispiel #7
0
        public void TestDBMethod()
        {
            int MEMBER_COUNT = 10;
            int IMAGE_COUNT = 10;
            int TAG_COUNT = 10;
            object _lock = new object();

            using (var ctx = new NijieContext())
            {
                Assert.IsTrue(ctx.Images.Count() == 0);
            }

            LimitedConcurrencyLevelTaskScheduler scheduler = new LimitedConcurrencyLevelTaskScheduler(3, 3);
            TaskFactory jobFactory = new TaskFactory(scheduler);
            List<Task> tasks = new List<Task>();
            for (int m = 0; m < MEMBER_COUNT; m++)
            {
                int tempM = m;
                var task = jobFactory.StartNew(() =>
                {
                    Debug.WriteLine(String.Format("Task {0} running...", tempM));
                    using (var ctx = new NijieContext())
                    {
                        var mbr = ctx.Members.Create();
                        mbr.MemberId = tempM;
                        mbr.UserName = "******";

                        for (int i = 0; i < IMAGE_COUNT; i++)
                        {
                            var img = ctx.Images.Create();
                            img.ImageId = i;
                            img.Title = "Dummy Image";
                            img.WorkDate = DateTime.Now;
                            img.SavedFilename = @"C:\haha.jpg";
                            img.Member = mbr;

                            img.Tags = new List<NijieTag>();
                            for (int t = 0; t < TAG_COUNT; ++t)
                            {
                                img.Tags.Add(new NijieTag() { Name = "Tag-" + t });
                            }
                            lock (_lock)
                            {
                                using (var dao = new NijieContext())
                                {
                                    img.SaveToDb(dao);
                                }
                            }
                        }
                        Debug.WriteLine(String.Format("Task {0} completed...", tempM));
                    }
                });
                tasks.Add(task);
            }

            Task.WaitAll(tasks.ToArray());
            using (var ctx = new NijieContext())
            {
                var images = from x in ctx.Images.Include("Tags")
                             select x;

                foreach (var item in images)
                {
                    Debug.WriteLine(String.Format("Image {0}: {1} ==> {2}", item.ImageId, item.ViewUrl, item.SavedFilename));
                    Debug.WriteLine(String.Format("DateTime: {0}", item.WorkDate));
                    foreach (var tag in item.Tags)
                    {
                        Debug.WriteLine(String.Format("\t - {0}", tag.Name));
                    }

                    Assert.IsTrue(item.WorkDate != DateTime.MinValue);
                }
            }
        }
        public void GetImage()
        {
            MainWindow.Log.Debug("Loading Image: " + this.ImageId);

            NijieImage temp = null;

            using (var ctx = new NijieContext())
            {
                temp = (from x in ctx.Images.Include("Member").Include("Tags")
                        where x.ImageId == this.ImageId && x.IsManga == false
                        select x).FirstOrDefault();
                if (temp != null && !String.IsNullOrWhiteSpace(temp.SavedFilename))
                {
                    temp.IsDownloaded = true;
                }
            }
            if (temp == null)
            {
                try
                {
                    var result = MainWindow.Bot.ParseImage(this.ImageId);
                    temp = result;
                    this.Message = "Image parsed, loading image...";
                }
                catch (NijieException ne)
                {
                    this.Message = "Error: " + ne.Message;
                    this.ImageStatus = ImageLoader.IMAGE_ERROR;
                    this.BigImage = ViewModelHelper.Error;
                    MainWindow.Log.Error(ne.Message, ne.InnerException);
                }
            }

            this._image = temp;
            this._mangaImageStatus = new List<string>();
        }
        /// <summary>
        /// Parse image list from member.
        /// </summary>
        /// <param name="html"></param>
        /// <param name="referer"></param>
        /// <returns></returns>
        private List<NijieImage> ParseImageList(string html, string referer)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            var images = doc.DocumentNode.SelectNodes("//div[@class='nijie']");

            // parse images
            var list = new List<NijieImage>();
            if (images != null)
            {
                foreach (var imageDiv in images)
                {
                    var imageId = imageDiv.SelectSingleNode("//div[@class='nijie']//a").Attributes["href"].Value;
                    var res = re_image.Match(imageId);
                    if (res.Success)
                    {
                        NijieImage image = new NijieImage(Int32.Parse(res.Groups[1].Value));
                        image.Referer = referer;

                        var div = new HtmlDocument();
                        div.LoadHtml(imageDiv.SelectSingleNode("//div[@class='nijie']").InnerHtml);

                        var link = div.DocumentNode.SelectSingleNode("//a");
                        image.Title = link.Attributes["title"].Value;

                        var thumb = div.DocumentNode.SelectSingleNode("//a/img");
                        image.ThumbImageUrl = thumb.Attributes["src"].Value;

                        // check if image is friend only
                        // img src="//img.nijie.info/pic/common_icon/illust/friends.png"
                        image.IsFriendOnly = false;
                        if (image.ThumbImageUrl.EndsWith("friends.png"))
                        {
                            image.IsFriendOnly = true;
                        }

                        //"//img.nijie.info/pic/common_icon/illust/golden.png"
                        image.IsGoldenMember = false;
                        if (image.ThumbImageUrl.EndsWith("golden.png"))
                        {
                            image.IsGoldenMember = true;
                        }

                        // check manga icon
                        image.IsManga = false;
                        var icon = div.DocumentNode.SelectSingleNode("//div[@class='thumbnail-icon']/img");
                        if (icon != null)
                        {
                            if (icon.Attributes["src"].Value.EndsWith("thumbnail_comic.png"))
                                image.IsManga = true;
                        }

                        // check animation icon
                        image.IsAnimated = false;
                        var animeIcon = div.DocumentNode.SelectSingleNode("//div[@class='thumbnail-icon']/img");
                        if (animeIcon != null)
                        {
                            if (animeIcon.Attributes["src"].Value.EndsWith("thumbnail_anime.png"))
                                image.IsAnimated = true;
                        }

                        list.Add(image);
                    }
                    imageDiv.Remove();
                }
            }

            using (var ctx = new NijieContext())
            {
                foreach (var item in list)
                {
                    var r = (from x in ctx.Images
                             where x.ImageId == item.ImageId
                             select x).FirstOrDefault();
                    if (r != null && !String.IsNullOrWhiteSpace(r.SavedFilename)) item.IsDownloaded = true;
                    else item.IsDownloaded = false;
                }
            }
            return list;
        }