Example #1
0
        private async void DownloadBtn_OnClick(object?sender, RoutedEventArgs e)
        {
            string uuid = this.FindControl <TextBox>("IdBox").Text;

            if (String.IsNullOrEmpty(uuid))
            {
                MessageBox box = new();
                box.Content = Utility.GenerateMessageBox("Error", "You need to enter a UUID", box);
                box.ShowDialog(this);
                return;
            }
            if (String.IsNullOrEmpty(_dlFolder))
            {
                MessageBox box = new();
                box.Content = Utility.GenerateMessageBox("Error", "You need to set a folder to where the manga should be downloaded.", box);
                box.ShowDialog(this);
                return;
            }
            string    lang   = ((ComboBoxItem)this.FindControl <ComboBox>("LangBox").SelectedItem).Tag.ToString();
            TextBlock status = this.FindControl <TextBlock>("Progress");

            status.Text = "Getting chapter info...";
            List <Chapter> chapters = await MangaDex.GetChapterList(uuid, lang);

            if (chapters.Count == 0)
            {
                MessageBox box = new();
                box.Content = Utility.GenerateMessageBox("Error", "No chapters found.", box);
                box.ShowDialog(this);
                return;
            }

            string mangaTitle = await MangaDex.GetMangaTitle(uuid);

            Thread t = new Thread(() =>
            {
                foreach (var chapter in chapters)
                {
                    if (Directory.Exists($"{_dlFolder}/{mangaTitle}/Vol. {chapter.Attributes.ChapterVolume} Ch. {chapter.Attributes.ChapterNumber}"))
                    {
                        continue;                                                                                                                              //! check if folder exists, than it was probably already downloaded - this might not be a good way to do this
                    }
                    int l = 1;
                    foreach (var page in chapter.Attributes.PageUrls)
                    {
                        Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            status.Text = $"Downloading page number {l} of chapter {chapter.Attributes.ChapterNumber}";
                        });
                        var RClient = new RestClient();
                        var req     = new RestRequest(page);

                        var response = RClient.Execute(req);
                        if (response.ErrorException != null || response.StatusCode != HttpStatusCode.OK)
                        {
                            if (response.StatusCode == HttpStatusCode.Forbidden)
                            {
                                string sitekey = response.Headers.ToList().Find(x => x.Name == "X-Captcha-Sitekey").Value.ToString();
                                Console.WriteLine(sitekey);
                            }
                            else
                            {
                                Dispatcher.UIThread.InvokeAsync(() =>
                                {
                                    string message;
                                    switch (response.StatusCode)
                                    {
                                    case HttpStatusCode.NotFound:
                                        message =
                                            "Manga not found. Make sure you are entering the new UUID and not the old number ID.";
                                        break;

                                    case HttpStatusCode.BadGateway:
                                        message =
                                            "There is a problem with the MangaDex servers. Check if you are able to access them or if you have internet access.";
                                        break;

                                    case HttpStatusCode.GatewayTimeout:
                                        message = "The MangaDex servers have timed out.";
                                        break;

                                    case HttpStatusCode.ServiceUnavailable:
                                        message =
                                            "Service is unavailable.";
                                        break;

                                    default:
                                        message = response.StatusDescription;
                                        break;
                                    }

                                    MessageBox box = new();
                                    box.Content    = Utility.GenerateMessageBox("Error", message, box);
                                    if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime
                                        desktop)
                                    {
                                        box.ShowDialog(desktop.MainWindow);
                                    }
                                });
                                return;
                            }
                        }

                        string dlPath;
                        Console.WriteLine(chapter.Attributes.ChapterNumber);
                        if (double.Parse(chapter.Attributes.ChapterNumber, CultureInfo.InvariantCulture) < 10)
                        {
                            dlPath = $"{_dlFolder}/{mangaTitle}/Vol. {chapter.Attributes.ChapterVolume} Ch. {chapter.Attributes.ChapterNumber}/0{l}{Path.GetExtension(page)}";
                        }
                        else
                        {
                            dlPath = $"{_dlFolder}/{mangaTitle}/Vol. {chapter.Attributes.ChapterVolume} Ch. {chapter.Attributes.ChapterNumber}/{l}{Path.GetExtension(page)}";
                        }

                        if (!Directory.Exists($"{_dlFolder}/{mangaTitle}/Vol. {chapter.Attributes.ChapterVolume} Ch. {chapter.Attributes.ChapterNumber}"))
                        {
                            Directory.CreateDirectory($"{_dlFolder}/{mangaTitle}/Vol. {chapter.Attributes.ChapterVolume} Ch. {chapter.Attributes.ChapterNumber}");
                        }
                        File.WriteAllBytes(Regex.Replace(dlPath, "/[/\\?%*:|\"<>]/g", ""), response.RawBytes);
                        l++;

                        Thread.Sleep(2500);
                    }
                }
                Dispatcher.UIThread.InvokeAsync(() =>
                {
                    status.Text = $"Download complete";
                });
            });

            t.Start();
        }
Example #2
0
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            string url  = gfxBrowser.Url.ToString();
            string name = string.Empty;
            string num  = string.Empty;

            switch (cmboSource.SelectedItem.ToString().ToLower())
            {
            case "mangadex":
                name = url.Split('/')[5];
                num  = url.Split('/')[4];
                url  = MangaDexHelper.MANGADEX_URL + "/api/manga/" + num;

                Manga m = new MangaDex(FileHelper.CreateDI(Path.Combine(FileHelper.APP_ROOT.FullName, num)), url);

                FrmEdit      editor = new FrmEdit(m, false);
                DialogResult result = editor.ShowDialog();

                if (result == DialogResult.OK)
                {
                    // cleanup
                    foreach (Chapter ch in m.GetChapters())
                    {
                        try
                        {
                            string s = ch.GetChapterRoot().ToString();
                            Directory.Delete(ch.GetChapterRoot().ToString());
                        } catch (Exception) { }
                    }

                    WFClient.dbm.GetMangaDB().Add(m);

                    String[] dls = m.GetDLChapters();
                    if (dls == null || dls[0].Equals("-1"))
                    {
                        foreach (Chapter c in m.GetSetPrunedChapters(false))
                        {
                            WFClient.dlm.AddToQueue(new MangaDexDownload(c));
                        }
                    }
                    else
                    {       // Only download selected chapters
                        foreach (Chapter c in m.GetSetPrunedChapters(false))
                        {
                            if (dls.Contains(c.GetNum()))
                            {
                                WFClient.dlm.AddToQueue(new MangaDexDownload(c));
                            }
                        }
                    }
                    // Start downloading the first one
                    WFClient.dlm.DownloadNext();
                }
                else
                {
                }
                break;

            case "kissmanga":

                // MessageBox.Show("Sorry, can't do this yet! ;(\n\n(ignore the download started box)");
                // break;
                // TODO

                string kName = KissMangaHelper.GetName(url);
                string kHash = KissMangaHelper.GetHash(url);

                // Manga km = new Manga(FileHelper.CreateDI(Path.Combine(FileHelper.APP_ROOT.FullName, num)), url);

                Manga km = new KissManga(FileHelper.CreateDI(Path.Combine(FileHelper.APP_ROOT.FullName, kHash)), url);

                FrmEdit      editor1 = new FrmEdit(km, false);
                DialogResult result1 = editor1.ShowDialog();

                if (result1 == DialogResult.OK)
                {
                    // cleanup
                    foreach (Chapter ch in km.GetChapters())
                    {
                        try
                        {
                            string s = ch.GetChapterRoot().ToString();
                            Directory.Delete(ch.GetChapterRoot().ToString());
                        }
                        catch (Exception) { }
                    }

                    WFClient.dbm.GetMangaDB().Add(km);

                    String[] dls = km.GetDLChapters();
                    if (dls == null || dls[0].Equals("-1"))
                    {
                        foreach (Chapter c in km.GetSetPrunedChapters(false))
                        {
                            WFClient.dlm.AddToQueue(new KissMangaDownload(c));
                        }
                    }
                    else
                    {       // Only download selected chapters
                        foreach (Chapter c in km.GetSetPrunedChapters(false))
                        {
                            if (dls.Contains(c.GetNum()))
                            {
                                WFClient.dlm.AddToQueue(new KissMangaDownload(c));
                            }
                        }
                    }
                    // Start downloading the first one
                    WFClient.dlm.DownloadNext();
                }
                else
                {
                }
                break;

            case "nhentai":
                num  = url.Split('/')[4];
                name = gfxBrowser.DocumentTitle.Substring(0, gfxBrowser.DocumentTitle.IndexOf("nhentai:") - 3);

                JObject hJson = new JObject(
                    new JProperty("hentai",
                                  new JObject(
                                      new JProperty("title", name),
                                      new JProperty("num", num),
                                      new JProperty("url", url))));

                DirectoryInfo hDir = FileHelper.CreateDI(Path.Combine(FileHelper.APP_ROOT.FullName, "h" + num));

                Hentai h = new Nhentai(hDir, hJson.ToString());

                FrmEdit      edit   = new FrmEdit(h, false);
                DialogResult rezult = edit.ShowDialog();

                if (rezult == DialogResult.OK)
                {
                    WFClient.dbm.GetMangaDB().Add(h);

                    Chapter ch = h.GetChapters()[0];
                    WFClient.dlm.AddToQueue(new NhentaiDownload(ch));
                    // Start downloading the first one
                    WFClient.dlm.DownloadNext();
                }
                break;
            }
            MessageBox.Show("Download started! You may close the browser at any time, but please keep MikuReader open until the download has completed.");
        }