示例#1
0
        public async Task TestSaveCaptcha()
        {
            var impl = new ServiceCollection()
                       .AddSingleton <RequestConfiguration>()
                       .AddPr0grammWebApi()
                       .BuildServiceProvider()
                       .GetService <IPr0grammApi>();

            var captchaResult = await impl.GetCaptcha()
                                .Bind(c => CaptchaConverter.ConvertToByteArray(c.Base64CaptchaImage));

            captchaResult.IsSuccess.ShouldBeTrue();
        }
示例#2
0
        public async Task <Result> ExecuteAsync(LoginVerb loginVerb)
        {
            if (string.IsNullOrWhiteSpace(loginVerb.Username))
            {
                loginVerb.Username = ConsoleHelper.PromptTextInput("Username");
            }
            if (string.IsNullOrWhiteSpace(loginVerb.Password))
            {
                loginVerb.Username = ConsoleHelper.PromptSecretInput("Password");
            }

            var captchaResponseResult = await _pr0grammApi.GetCaptcha();

            var captchaDirectoryPath = _envAbstraction.GetWellKnownPath(WellKnownLocations.TempDirectory);
            var captchaFilename      = $"captcha_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}.png";

            var transformationResult = captchaResponseResult
                                       .Bind(c => CaptchaConverter.ConvertToByteArray(c.Base64CaptchaImage))
                                       .Bind(d => _envAbstraction.SaveFile(captchaDirectoryPath, captchaFilename, d))
                                       // ReSharper disable once ImplicitlyCapturedClosure
                                       .Bind(f => _envAbstraction.OpenImageViewer(f)
                                             .OnFailureCompensate(_ => _envAbstraction.OpenBrowser(f))
                                             .OnFailureCompensate(_ => {
                Console.WriteLine($"Captcha saved, open it here '{f}'");
                return(Result.Success());
            }));

            if (transformationResult.IsFailure)
            {
                return(transformationResult);
            }

            var captchaText = ConsoleHelper.PromptTextInput("Captcha");

            return(await _pr0grammApi.LoginAsync(new Authentication {
                Username = loginVerb.Username,
                Password = loginVerb.Password,
                CaptchaText = captchaText,
                CaptchaToken = captchaResponseResult.Value.Token
            })
                   .Bind(maybeSession => maybeSession.ToResult("Username, Password are wrong, or you are banned."))
                   .Bind(session => _sessionPersistor.Store(session)));
        }