Example #1
0
        private static async Task Start(int imageCount, BioFilter filter)
        {
            try
            {
                string dirPath = Path.GetFullPath($"fake_photos_{DateTime.Now:dd-MM-yyyy_hh-mm-ss}");
                Directory.CreateDirectory(dirPath);

                try
                {
                    for (int i = 0; i < imageCount;)
                    {
                        if (await TryOneImage(dirPath, i, filter))
                        {
                            i++;
                        }
                    }
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine(e);
                }
            }
            catch (WebDriverException e)
            {
                Console.WriteLine(e);
            }
        }
Example #2
0
        private static (int, BioFilter) ParseArgs(string[] args)
        {
            int imageCount = 5;

            var bioFilter = new BioFilter(
                needMale: true,
                needFemale: true,
                minAge: 16,
                maxAge: 50
                );

            foreach (string arg in args)
            {
                string[] splitted = arg.Split(new[] { ':' });
                if (splitted.Length != 2)
                {
                    Console.WriteLine("unknown arg: " + arg);
                    throw new ArgumentException();
                }

                string argName  = splitted[0];
                string argValue = splitted[1];

                try
                {
                    switch (argName)
                    {
                    case "count":
                        imageCount = int.Parse(argValue);
                        break;

                    case "male":
                        bioFilter.NeedMale = bool.Parse(argValue);
                        break;

                    case "female":
                        bioFilter.NeedFemale = bool.Parse(argValue);
                        break;

                    case "min_age":
                        bioFilter.MinAge = int.Parse(argValue);
                        break;

                    case "max_age":
                        bioFilter.MaxAge = int.Parse(argValue);
                        break;

                    default:
                        throw new ArgumentException();
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("unknown arg: " + arg);
                    throw new ArgumentException();
                }
            }

            return(imageCount, bioFilter);
        }
Example #3
0
        private static async Task <bool> TryOneImage(string dirPath, int imageNum, BioFilter filter)
        {
            var imageBytes = await NewImage();

            string imagePath = Path.Combine(dirPath, $"{imageNum}.jpg");

            File.WriteAllBytes(imagePath, imageBytes);

            Bio bio;

            try
            {
                bio = BioOfImage(imagePath);
            }
            catch (Exception e)
            {
                File.Delete(imagePath);
                throw;
            }

            bool isFit = filter.IsFit(bio);

            if (isFit)
            {
                string genderStr   = bio.IsFemale ? "f" : "m";
                string newFileName = $"{imageNum}_{genderStr}_{bio.Age}";
                File.Move(imagePath, Path.Combine(dirPath, $"{newFileName}.jpg"));
            }
            else
            {
                File.Delete(imagePath);
            }

            return(isFit);
        }