Ejemplo n.º 1
0
        /// <summary>
        /// <see cref="IHandler{T,K}.FetchContent(x,string,x,ICollection{string})"/>
        /// </summary>
        public override async Task FetchContent(RedditListing parsedSource, string targetFolder, RedditFilter filter, ICollection <string> outputLog)
        {
            await Task.Run(async() =>
            {
                if (parsedSource.GetCollections() != null)
                {
                    outputLog.Add($"Starting download of {parsedSource.GetImages().Count()} images.");

                    foreach (var redditPost in parsedSource.GetCollections().Where(image => image != null).AsParallel())
                    {
                        if (redditPost.GetImages() == null)
                        {
                            continue;
                        }

                        var sync = new object();

                        foreach (var image in redditPost.GetImages().Where(image => image != null).AsParallel())
                        {
                            var imageName = Filenamer.Clean($"{redditPost.ShortTitle} - {await image.GetImageName()}");

                            try
                            {
                                if (filter(await image.GetHeight(), await image.GetWidth(), redditPost.Over_18, redditPost.Album != null, await image.GetAspectRatio()))
                                {
                                    var path         = Filenamer.DetermineUniqueFilename(Path.Combine(targetFolder, imageName));
                                    var fileContents = await image.GetImage();

                                    File.WriteAllBytes(path, fileContents);

                                    lock (sync)
                                    {
                                        outputLog.Add($"Image saved: {imageName}");
                                        image.Dispose();
                                    }
                                }
                                else
                                {
                                    lock (sync)
                                    {
                                        outputLog.Add($"Image skipped: {imageName}");
                                        image.Dispose();
                                    }
                                }
                            }
                            catch (WebException)
                            {
                                lock (sync)
                                {
                                    outputLog.Add($"Unable to download image: {imageName}");
                                    image.Dispose();
                                }
                            }
                            catch (IOException)
                            {
                                lock (sync)
                                {
                                    outputLog.Add($"IO Failure - Error occured while saving image: {imageName}");
                                    image.Dispose();
                                }
                            }
                        }
                    }
                }

                outputLog.Add("Download finished.");
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// <see cref="IHandler{T,K}.FetchContent(x,string,x,ICollection{string})"/>
        /// </summary>
        public override async Task FetchContent(RedditListing parsedSource, string targetFolder, RedditFilter filter, ICollection <string> outputLog, bool saveNestedCollectionsInNestedFolders = false)
        {
            await Task.Run(() =>
            {
                if (parsedSource.GetCollections() != null)
                {
                    outputLog.Add($"Starting download of {parsedSource.GetImages().Count()} images.");


                    foreach (var redditPost in parsedSource.GetCollections().Where(image => image != null))
                    {
                        if (redditPost.GetImages() == null)
                        {
                            continue;
                        }
                        var sync = new object();
                        var localTargetFolder = targetFolder;

                        //If the images will be saved in their own album, then there is no reason to prefix them with the reddit-title, since the folder has that info already.
                        string imageNamePrefix;

                        if (redditPost.IsAlbum && saveNestedCollectionsInNestedFolders)
                        {
                            localTargetFolder = Path.Combine(localTargetFolder, Filenamer.Clean(redditPost.ShortTitle));
                            imageNamePrefix   = string.Empty;
                        }
                        else
                        {
                            imageNamePrefix = Filenamer.Clean(redditPost.ShortTitle) + " - ";
                        }

                        //Limit degree of parallelism to avoid sending too many http-requests at the same time.
                        //  8 seems like a reasonable amount of requests to have in-flight at a time.
                        var parallelOptions = new ParallelOptions {
                            MaxDegreeOfParallelism = 8
                        };

                        Parallel.ForEach(redditPost.GetImages().Where(image => image != null), parallelOptions, image =>
                        {
                            using (image)
                            {
                                var imageName = imageNamePrefix + Filenamer.Clean(image.GetImageName().Result);

                                try
                                {
                                    if (filter(image.GetHeight().Result, image.GetWidth().Result, redditPost.Over_18,
                                               redditPost.Album != null, image.GetAspectRatio().Result))
                                    {
                                        //Dont create the folder unless we actually have something to save in it.
                                        if (!Directory.Exists(localTargetFolder))
                                        {
                                            Directory.CreateDirectory(localTargetFolder);
                                        }

                                        var path         = Filenamer.DetermineUniqueFilename(Path.Combine(localTargetFolder, imageName));
                                        var fileContents = image.GetImage().Result;

                                        File.WriteAllBytes(path, fileContents);
                                        WriteToLog(sync, outputLog, $"Image saved: {imageName}");
                                    }
                                    else
                                    {
                                        WriteToLog(sync, outputLog, $"Image skipped: {imageName}");
                                    }
                                }
                                catch (WebException)
                                {
                                    WriteToLog(sync, outputLog, $"Unable to download image: {imageName}");
                                }
                                catch (IOException)
                                {
                                    WriteToLog(sync, outputLog,
                                               $"IO Failure - Error occured while saving image: {imageName}");
                                }
                                catch (AggregateException ex)
                                {
                                    if (ex.InnerException is WebException)
                                    {
                                        WriteToLog(sync, outputLog, $"Unable to download image: {imageName}");
                                    }
                                    else
                                    {
                                        WriteToLog(sync, outputLog, $"Unknown error occured for {imageName}. Error message is: " + ex.InnerException.Message);
                                    }
                                }
                            }
                        });
                    }
                }

                outputLog.Add("Download finished.");
            });
        }