예제 #1
0
        //Check and see if there is new chapter available for download.
        public bool CheckForUpdate()
        {
            Source[] sources = OrderedSources;
            Dictionary <string, Chapter> chapters = ChapterDictionary;

            ChapterSource[] menuItems;
            SetUpdateProgress(0, 0, UpdateStates.Checking);
            try
            {
                foreach (Source source in sources)
                {
                    if (source == null || !source.Valid || source.ID == OriginSource.ID)
                    {
                        continue;
                    }
                    menuItems = source.GetMenuURLs();
                    if (menuItems == null)
                    {
                        continue;
                    }
                    HashSet <string> usedTitle = new HashSet <string>();
                    for (int i = 0; i < menuItems.Length; i++)
                    {
                        //Check for duplicate chapter titles and changes the name of it so it will not conflict.
                        string chapterTitle = menuItems[i].Title;
                        int    dupIdx       = 2;
                        while (usedTitle.Contains(chapterTitle))
                        {
                            chapterTitle = menuItems[i].Title + "-duplicate x " + dupIdx;
                            dupIdx++;
                        }
                        usedTitle.Add(chapterTitle);

                        if (chapters.Keys.Contains(chapterTitle) && !NovelLibrary.libraryData.ChapterUrls.Where(url => url.Hash == menuItems[i].UrlHash).Any())
                        {
                            ChapterUrl newChapterUrl = new ChapterUrl();
                            newChapterUrl.ChapterID = chapters[chapterTitle].ID;
                            newChapterUrl.Url       = menuItems[i].Url;
                            newChapterUrl.Hash      = menuItems[i].UrlHash;
                            newChapterUrl.Vip       = menuItems[i].Vip;
                            newChapterUrl.SourceID  = source.ID;
                            newChapterUrl.Source    = source;
                            newChapterUrl.Chapter   = chapters[chapterTitle];
                            NovelLibrary.libraryData.ChapterUrls.InsertOnSubmit(newChapterUrl);
                            NovelLibrary.libraryData.SubmitChanges();
                        }
                    }
                }
            }catch (Exception e)
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        public bool DownloadChapter(Chapter downloadChapter, Source specificSource = null)
        {
            if (downloadChapter == null || !downloadChapter.Valid)
            {
                SetUpdateProgress(0, 0, UpdateStates.Error);
                return(false);
            }

            if (UpdateState == UpdateStates.Syncing || UpdateState == UpdateStates.Checking)
            {
                return(false);
            }

            var result = downloadChapter.ChapterUrls;

            if (!result.Any())
            {
                SetUpdateProgress(0, 0, UpdateStates.Error);
                return(false);
            }

            if (specificSource != null)
            {
                if (!Sources.Contains(specificSource))
                {
                    return(false);
                }
                var urlResult = (from url in downloadChapter.ChapterUrls
                                 where url.Source.ID == specificSource.ID
                                 select url);
                if (!urlResult.Any())
                {
                    return(false);
                }
                ChapterUrl specificUrl  = urlResult.First <ChapterUrl>();
                string[]   novelContent = specificSource.GetChapterContent(downloadChapter.ChapterTitle, specificUrl.Url);
                if (novelContent == null)
                {
                    return(false);
                }
                System.IO.File.WriteAllLines(downloadChapter.GetTextFileLocation(), novelContent);
                return(true);
            }

            ChapterUrl[] urls = (from url in result
                                 where url.Vip == false
                                 orderby url.Source.Priority ascending
                                 select url).ToArray <ChapterUrl>();

            foreach (ChapterUrl url in urls)
            {
                //Console.WriteLine("Download chapter " + downloadChapter.ChapterTitle + " " + (url.Vip));
                if (url == null || url.Vip)
                {
                    continue;
                }
                Source source = url.Source;

                if (source == null || !source.Valid)
                {
                    continue;
                }

                string[] novelContent = source.GetChapterContent(downloadChapter.ChapterTitle, url.Url);
                if (novelContent == null)
                {
                    continue;
                }
                System.IO.File.WriteAllLines(downloadChapter.GetTextFileLocation(), novelContent);

                if (Configuration.Instance.TextExportLocation != null)
                {
                    string exportFolderLocation = Path.Combine(Configuration.Instance.TextExportLocation, NovelTitle);

                    if (!Directory.Exists(exportFolderLocation))
                    {
                        Directory.CreateDirectory(exportFolderLocation);
                    }

                    if (Configuration.Instance.NovelExport.ContainsKey(NovelTitle))
                    {
                        ExportOption exportOption = Configuration.Instance.NovelExport[NovelTitle];
                        if (exportOption == ExportOption.Both || exportOption == ExportOption.Text)
                        {
                            downloadChapter.ExportText(exportFolderLocation);
                        }
                    }
                }
                return(true);
            }

            return(false);
        }
예제 #3
0
        //Sync the chapter list to the origin source.
        public bool SyncToOrigin()
        {
            SetUpdateProgress(0, 0, UpdateStates.Syncing);
            if (OriginSource == null)
            {
                return(false);
            }
            ChapterSource[] menuItems = OriginSource.GetMenuURLs();
            if (!File.Exists(GetNovelCoverImageLocation()))
            {
                OriginSource.DownloadNovelCoverImage(GetNovelCoverImageLocation());
            }
            if (menuItems == null)
            {
                SetUpdateProgress(0, 0, UpdateStates.Error);
                return(false);
            }
            List <Chapter> chapterListing = new List <Chapter>();
            int            updateCount    = 0;

            for (int i = 0; i < menuItems.Length; i++)
            {
                try
                {
                    //add new chapter if url does not exist in database
                    if (!OriginSource.ChapterUrls.Where(originUrl => originUrl.Hash == menuItems[i].UrlHash).Any())
                    {
                        Chapter newChapter = new Chapter();
                        newChapter.ChapterTitle = menuItems[i].Title;
                        newChapter.NovelTitle   = NovelTitle;
                        newChapter.Read         = false;
                        newChapter.Index        = i;
                        newChapter.Novel        = this;
                        newChapter.Valid        = true;
                        newChapter.HashID       = menuItems[i].UrlHash.ToString("X");
                        NovelLibrary.libraryData.Chapters.InsertOnSubmit(newChapter);
                        NovelLibrary.libraryData.SubmitChanges();
                        //chapterList.Insert(i, newChapter);
                        chapterListing.Add(newChapter);
                        isDirty = true;
                        updateCount++;

                        ChapterUrl newChapterUrl = new ChapterUrl();
                        newChapterUrl.ChapterID = newChapter.ID;
                        newChapterUrl.Url       = menuItems[i].Url;
                        newChapterUrl.Hash      = menuItems[i].UrlHash;
                        newChapterUrl.Vip       = menuItems[i].Vip;
                        newChapterUrl.SourceID  = OriginSource.ID;
                        newChapterUrl.Source    = OriginSource;
                        newChapterUrl.Chapter   = newChapter;
                        NovelLibrary.libraryData.ChapterUrls.InsertOnSubmit(newChapterUrl);
                        NovelLibrary.libraryData.SubmitChanges();
                    }
                    //chapter already exist within database.
                    else
                    {
                        Chapter c = (from chapterUrl in OriginSource.ChapterUrls
                                     where chapterUrl.Url == menuItems[i].Url
                                     select chapterUrl.Chapter).First <Chapter>();
                        if (c.ChapterTitle != menuItems[i].Title)
                        {
                            c.ChapterTitle = menuItems[i].Title;
                        }
                        if (c.Index != i)
                        {
                            c.Index = i;
                        }
                        chapterListing.Add(c);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return(false);
                }
            }
            chapterList.SyncBindingToConstrain();
            //Remove chapter that no longer belong in the list.
            HashSet <int> includedID = new HashSet <int>(chapterListing.Select(c => c.ID));

            Chapter[] allChapters = Chapters.ToArray();
            for (int i = 0; i < allChapters.Count(); i++)
            {
                if (!includedID.Contains(allChapters[i].ID))
                {
                    Console.WriteLine("Deleting " + allChapters[i].ChapterTitle + " " + allChapters[i].ID);
                    DeleteChapter(allChapters[i], false, false);
                }
            }
            RefreshCacheData();
            if (updateCount == 0)
            {
                SetUpdateProgress(0, 0, UpdateStates.UpToDate);
            }
            else
            {
                SetUpdateProgress(updateCount, 0, UpdateStates.UpdateAvailable);
            }
            return(true);
        }
예제 #4
0
 private void detach_ChapterUrls(ChapterUrl entity)
 {
     this.SendPropertyChanging();
     entity.Chapter = null;
 }
예제 #5
0
 private void attach_ChapterUrls(ChapterUrl entity)
 {
     this.SendPropertyChanging();
     entity.Chapter = this;
 }
예제 #6
0
 partial void DeleteChapterUrl(ChapterUrl instance);
예제 #7
0
 partial void UpdateChapterUrl(ChapterUrl instance);
예제 #8
0
 partial void InsertChapterUrl(ChapterUrl instance);