Beispiel #1
0
        public override void RunAsync()
        {
            if (BackgroundWorker.IsBusy)
            {
                MessageBox.Show("Program is busy", "Download in progress", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            OnCoversDownloadStarted(new CoversDownloadStartedEventArgs(SearchResult /*pageIndices, galleryId*/));

            DownloadCoversRunArg runArg = new DownloadCoversRunArg(HttpClient, PathFormatter, SearchResult, MetadataKeywordLists, Filters /*galleryId, pageIndices, SearchResultCache, GetCachedPageIndices*/);

            BackgroundWorker.RunWorkerAsync(runArg);
        }
Beispiel #2
0
        private static void DownloadCoverBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker     backgroundWorker = sender as BackgroundWorker;
            DownloadCoversRunArg runArg           = e.Argument as DownloadCoversRunArg;
            SearchResult         searchResult     = runArg.SearchResult;
            int maxCount  = searchResult.Result.Count;
            int loadCount = 0;

            for (int i = 0; i < maxCount; ++i)
            {
                if (backgroundWorker.CancellationPending)
                {
                    CoverDownloadCompletedArg cancelledArg = new CoverDownloadCompletedArg(/*loadCount, maxCount, cacheCount, metadata*/ searchResult);

                    e.Result = cancelledArg;
                    //e.Cancel = true;
                    return;
                }

                Metadata metadata = searchResult.Result[i];
                string   error    = "";
                string   coverCachedFilePath;
                if (runArg.PathFormatter != null && runArg.PathFormatter.IsEnabled)
                {
                    coverCachedFilePath = runArg.PathFormatter.GetCover(metadata);
                }
                else
                {
                    coverCachedFilePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}",
                                                        runArg.PathFormatter.GetCacheDirectory(), metadata.Id, metadata.Images.Cover.GetFileExtension());
                }

                bool shouldFilter = ShouldFilter(metadata, runArg.MetadataKeywordLists, runArg.Filters);

                if (shouldFilter)
                {
                    error = "SKIP";
                }
                else
                {
                    ++loadCount;

                    if (!File.Exists(coverCachedFilePath))
                    {
                        try
                        {
                            string uri = string.Format(CultureInfo.InvariantCulture, "https://t.nhentai.net/galleries/{0}/{1}{2}", metadata.MediaId, "cover", metadata.Images.Cover.GetFileExtension());

                            using (HttpResponseMessage response = Task.Run(() => runArg.HttpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead)).GetAwaiter().GetResult())
                            {
                                if (!response.IsSuccessStatusCode)
                                {
                                    coverCachedFilePath = "";
                                    response.EnsureSuccessStatusCode();
                                    //error = string.Format(CultureInfo.InvariantCulture, "{0} ({1})", response.ReasonPhrase, response.StatusCode);
                                    //continue;
                                }
                                else
                                {
                                    try
                                    {
                                        byte[] imageData = Task.Run(() => response.Content.ReadAsByteArrayAsync()).GetAwaiter().GetResult();

                                        Directory.CreateDirectory(Path.GetDirectoryName(coverCachedFilePath));
                                        File.WriteAllBytes(coverCachedFilePath, imageData);
                                    }
                                    catch (Exception ex)
                                    {
                                        coverCachedFilePath = "";
                                        error = ex.Message;
                                        //continue;
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            coverCachedFilePath = "";
                            error = ex.Message;
                            //continue;
                        }
                    }
                }

                CoverDownloadProgressArg progressArg = new CoverDownloadProgressArg(searchResult, loadCount, metadata, coverCachedFilePath, error);

                backgroundWorker.ReportProgress((int)(loadCount / (float)maxCount * 100), progressArg);
            }

            CoverDownloadCompletedArg completedArg = new CoverDownloadCompletedArg(/*loadCount, maxCount, cacheCount, */ searchResult);

            e.Result = completedArg;
        }
Beispiel #3
0
        private static void DownloadCoverBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker     backgroundWorker = sender as BackgroundWorker;
            DownloadCoversRunArg runArg           = e.Argument as DownloadCoversRunArg;
            SearchResult         searchResult     = runArg.SearchResult;
            int maxCount  = searchResult.Result.Count;
            int loadCount = 0;

            for (int i = 0; i < maxCount; ++i)
            {
                if (backgroundWorker.CancellationPending)
                {
                    CoverDownloadCompletedArg cancelledArg = new CoverDownloadCompletedArg(/*loadCount, maxCount, cacheCount, metadata*/ searchResult);

                    e.Result = cancelledArg;
                    //e.Cancel = true;
                    return;
                }

                Metadata metadata = searchResult.Result[i];
                string   error    = "";
                string   coverCachedFilePath;
                if (runArg.PathFormatter != null && runArg.PathFormatter.IsEnabled)
                {
                    coverCachedFilePath = runArg.PathFormatter.GetCover(metadata);
                }
                else
                {
                    coverCachedFilePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}",
                                                        runArg.PathFormatter.GetCacheDirectory(), metadata.Id, metadata.Images.Cover.GetFileExtension());
                }

                bool shouldFilter = ShouldFilter(metadata, runArg.MetadataKeywordLists, runArg.Filters);

                if (shouldFilter)
                {
                    error = "SKIP";
                }
                else
                {
                    ++loadCount;

                    if (!File.Exists(coverCachedFilePath))
                    {
                        try
                        {
                            var mediaServerId = -1;

                            {
                                var url = $"https://nhentai.net/g/{metadata.Id}/";

                                Program.Logger.LogLine($"Downloading gallery webpage {url}...");

                                using (var response = Task.Run(() => runArg.HttpClient?.GetAsync(url, HttpCompletionOption.ResponseHeadersRead)).GetAwaiter().GetResult())
                                {
                                    if (!response.IsSuccessStatusCode)
                                    {
                                        Program.Logger.ErrorLine($"{response.ReasonPhrase} ({response.StatusCode})");
                                        response.EnsureSuccessStatusCode();
                                    }
                                    else
                                    {
                                        var html = Task.Run(() => response.Content.ReadAsStringAsync()).GetAwaiter().GetResult();
                                        var mediaServerIdRegExp = new Regex(@"media_server:\s*(\d+),");
                                        var mediaServerIdMatch  = mediaServerIdRegExp.Match(html);

                                        if (!mediaServerIdMatch.Success)
                                        {
                                            throw new Exception("Could not find media server id.");
                                        }
                                        else
                                        {
                                            mediaServerId = int.Parse(mediaServerIdMatch.Groups[1].Value);
                                        }
                                    }
                                }
                            }

                            if (mediaServerId != -1)
                            {
                                string uri = string.Format(CultureInfo.InvariantCulture, "https://t{3}.nhentai.net/galleries/{0}/{1}{2}", metadata.MediaId, "cover", metadata.Images.Cover.GetFileExtension(), mediaServerId);

                                Program.Logger.LogLine($"Downloading cover image {uri}...");

                                using (HttpResponseMessage response = Task.Run(() => runArg.HttpClient?.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead)).GetAwaiter().GetResult())
                                {
                                    if (!response.IsSuccessStatusCode)
                                    {
                                        Program.Logger.ErrorLine($"{response.ReasonPhrase} ({response.StatusCode})");
                                        response.EnsureSuccessStatusCode();
                                    }
                                    else
                                    {
                                        byte[] imageData = Task.Run(() => response.Content.ReadAsByteArrayAsync()).GetAwaiter().GetResult();

                                        Directory.CreateDirectory(Path.GetDirectoryName(coverCachedFilePath));
                                        File.WriteAllBytes(coverCachedFilePath, imageData);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            //coverCachedFilePath = "";
                            error = ex.Message;

                            Program.Logger.ErrorLine(ex.ToString());
                        }
                    }
                }

                CoverDownloadProgressArg progressArg = new CoverDownloadProgressArg(searchResult, loadCount, metadata, coverCachedFilePath, error);

                backgroundWorker.ReportProgress((int)(loadCount / (float)maxCount * 100), progressArg);
            }

            CoverDownloadCompletedArg completedArg = new CoverDownloadCompletedArg(/*loadCount, maxCount, cacheCount, */ searchResult);

            e.Result = completedArg;
        }