Esempio n. 1
0
        public static async Task <Boolean> CheckValidAPIAsync(String apiKey)
        {
            if (!CheckAPI(apiKey))
            {
                return(false);
            }

            DerpiImage search = await JsonAPI.GetDerpiImageAsync(null, 1, apiKey, false).ConfigureAwait(true);

            return(search != null && search.total > 0);
        }
        public async Task InitializeTaskAsync()
        {
            if (IsInvalid)
            {
                Log.Add(new LogMessage(Globals.Localization.InitializedInvalidTaskError, MessageType.CriticalError));
                return;
            }

            if (IsInitialized || IsCompleted || _token.IsCancellationRequested)
            {
                return;
            }

            _firstPage = await GetPageAsync(1).ConfigureAwait(true);

            if (_firstPage == null)
            {
                IsInvalid = true;
                return;
            }

            ImagesPerPage = _firstPage.images.Count;
            MaximumImages = _firstPage.total;
            Pages         = (Int32)Math.Ceiling((Single)MaximumImages / ImagesPerPage);

            if (_countOfPages == 0)
            {
                Log.Add(new LogMessage(Globals.Localization.ZeroDownloadPagesArgument, MessageType.Warning));
                IsCompleted = true;
                return;
            }

            if (_firstPageNumber > Pages)
            {
                Log.Add(new LogMessage(Globals.Localization.NoImagesFound, MessageType.Warning));
                IsCompleted = true;
                return;
            }

            if (_countOfPages > 0)
            {
                Pages = MathUtils.ToRange(Math.Min(Pages, _firstPageNumber + _countOfPages - 1), 0, Pages);
            }

            Int32 lastPageImageCount = MaximumImages <= ImagesPerPage ? MaximumImages : MathUtils.ToRange(MaximumImages % ImagesPerPage, 1, ImagesPerPage, true);

            MaximumImages = MathUtils.ToRange((Pages - 1) * ImagesPerPage + lastPageImageCount, 0, MaximumImages);

            if (MaximumImages <= 0 || Pages <= 0)
            {
                Log.Add(new LogMessage(Globals.Localization.NoImagesFound, MessageType.Warning));
                IsCompleted = true;
                return;
            }

            // !DirectoryUtils.TryCreateDirectory(Regex.Replace(_saveDirectory, @"\{[^\{\}]+\}", "test"))

            if (SaveToDisk && !DirectoryUtils.CheckPermissions(_saveDirectory, FileSystemRights.Read | FileSystemRights.Write))
            {
                Log.Add(new LogMessage(Globals.Localization.CreateDirectoryError, MessageType.CriticalError));
                IsInvalid = true;
                return;
            }

            IsInitialized = true;
        }
        private async Task <DerpiImage> GetPageAsync(Int32 page)
        {
            try
            {
                const Int32 maximumCounts = 10;
                const Int32 waitDelay     = 30000;
                Int32       count         = 0;
                do
                {
                    using CancellationTokenSource source = new CancellationTokenSource();
                    CancellationToken token = source.Token;

                    if (_token.IsCancellationRequested)
                    {
                        source.Cancel();
                    }

                    await WaitAsync(token).ConfigureAwait(true);

                    token.ThrowIfCancellationRequested();

                    Task <DerpiImage> pageTask = JsonAPI.GetDerpiImageAsync(SearchQuery, page, token: token);

                    try
                    {
                        if (await Task.WhenAny(pageTask, Task.Delay(waitDelay, token)).ConfigureAwait(true) == pageTask)
                        {
                            DerpiImage derpiImage = await pageTask.ConfigureAwait(true);

                            try
                            {
                                pageTask.Dispose();
                            }
                            catch (InvalidOperationException)
                            {
                                //ignored
                            }

                            return(derpiImage);
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        //ignored
                    }

                    Log.Add(new LogMessage(Globals.Localization.PageWaitToLongRetry, MessageType.Warning, new[] { page.ToString() }));
                    source.Cancel();

                    try
                    {
                        pageTask.Dispose();
                    }
                    catch (InvalidOperationException)
                    {
                        //ignored
                    }

                    count++;
                } while (count < maximumCounts);

                Log.Add(new LogMessage(Globals.Localization.GetPageError, MessageType.CriticalWarning, new[] { page.ToString() }));
                return(null);
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
        }
        private async Task GenerateTasksAsync()
        {
            Task[]     tasks    = new Task[ImagesPerPage];
            DerpiImage nextPage = new DerpiImage();

            for (Int32 page = _firstPageNumber; page <= Pages; page++)
            {
                if (page == _firstPageNumber)
                {
                    await Task.Delay(2000, _token).ConfigureAwait(true);
                }

                await WaitAsync(_token).ConfigureAwait(true);

                Task <DerpiImage> nextPageTask = null;

                if (_token.IsCancellationRequested)
                {
                    return;
                }

                if (page < Pages)
                {
                    nextPageTask = GetPageAsync(page + 1);
                }

                DerpiImage derpiPage = page == _firstPageNumber ? _firstPage : nextPage;

                if (nextPage == null)
                {
                    if (nextPageTask != null)
                    {
                        nextPage = await nextPageTask.ConfigureAwait(true);
                    }

                    continue;
                }

                for (Int32 index = 0; index < derpiPage.images.Count; index++)
                {
                    if (IsInvalid || _token.IsCancellationRequested)
                    {
                        return;
                    }

                    Image image = derpiPage.images[index];

                    while (image?.duplicate_of != null)
                    {
                        Log.Add(new LogMessage("Image id:{0} duplicate of id:{1}", MessageType.Warning,
                                               new[] { image.id.ToString(), image.duplicate_of.ToString() }));
                        try
                        {
                            image = (await JsonAPI.GetDerpiImageAsync($"id:{image.duplicate_of}", token: _token).ConfigureAwait(true)).images[0];
                        }
                        catch (IndexOutOfRangeException)
                        {
                            Log.Add(new LogMessage(Globals.Localization.CantFoundDuplicateImage, MessageType.Warning));
                        }
                    }

                    if (image == null)
                    {
                        continue;
                    }

                    ImageDataSupplement(image);

                    tasks[index] = DownloadImageAsync(image);
                }

                await Task.WhenAll(tasks).ConfigureAwait(true);

                if (page >= Pages)
                {
                    continue;
                }

                if (nextPageTask != null)
                {
                    nextPage = await nextPageTask.ConfigureAwait(true);
                }
            }

            IsCompleted = true;
        }