Exemple #1
0
        public IEnumerable <KeyValuePair <int, string> > EnumChapters()
        {
            int ID = ChapterLinks.Count;

            HtmlAgilityPack.HtmlDocument Page = Document;
            string CurrentPage = CurrentUrl;

            bool       Empty;
            List <int> Ids = new List <int>();

            ChapterLangs = new Dictionary <int, string>();
            ChapterLinks = new Dictionary <int, string>();
            ChapterNames = new Dictionary <int, string>();

            do
            {
                bool First = true;
                Empty = true;
                foreach (var Node in Page.SelectNodes("//*[@id=\"content\"]//div[contains(@class, \"chapter-row\")]"))
                {
                    if (First)
                    {
                        First = false;
                        continue;
                    }

                    var ChapterInfo = Node.SelectSingleNode(Node.XPath + "//a[@class=\"text-truncate\"]");
                    var ChapterLang = Node.SelectSingleNode(Node.XPath + "//span[contains(@class, \"flag\")]");

                    var Name = HttpUtility.HtmlDecode(ChapterInfo.InnerText).ToLower();
                    var Link = HttpUtility.HtmlDecode(ChapterInfo.GetAttributeValue("href", ""));
                    var Lang = HttpUtility.HtmlDecode(ChapterLang.GetAttributeValue("title", "")).Trim();

                    Link = Link.EnsureAbsoluteUrl("https://mangadex.org");

                    if (ChapterLinks.Values.Contains(Link))
                    {
                        continue;
                    }

                    Empty = false;

                    if (Name.Contains('-'))
                    {
                        Name = Name.Substring(0, Name.IndexOf("-"));
                    }

                    if (Name.Contains("vol."))
                    {
                        Name = Name.Substring("vol. ");

                        var Parts = Name.Split(' ');
                        if (Parts.Length > 2)
                        {
                            Name = Parts[2];
                        }
                        else
                        {
                            Name = char.ToUpper(Name[0]) + Name.Substring(1);
                        }
                    }
                    else if (Name.Contains("ch. "))
                    {
                        Name = Name.Substring("ch. ");
                    }

                    ChapterNames[ID] = DataTools.GetRawName(Name.Trim());
                    ChapterLinks[ID] = Link;
                    ChapterLangs[ID] = Lang;

                    Ids.Add(ID++);
                }

                if (!Empty)
                {
                    CurrentPage = GetNextPage(CurrentPage);
                    Page        = new HtmlAgilityPack.HtmlDocument();
                    Page.LoadUrl(CurrentPage, Referer: "https://mangadex.org", UserAgent: CFData?.UserAgent ?? null, Cookies: CFData?.Cookies ?? null);
                }
            } while (!Empty);

            string SelectedLang = SelectLanguage((from x in Ids select ChapterLangs[x]).Distinct().ToArray());

            return(from x in Ids
                   where ChapterLangs[x] == SelectedLang
                   select new KeyValuePair <int, string>(x, ChapterNames[x]));
        }