Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @TargetApi(Build.VERSION_CODES.KITKAT)
- @SuppressWarnings("BusyWait")
- @Override
- public ReadCaptchaResult onReadCaptcha(ReadCaptchaData data) throws HttpException, InvalidResponseException {
- AtomicReference<String> captcha = new AtomicReference<>("");
- WebView webView = data.webView;
- webView.post(() -> {
- WebSettings webSettings = webView.getSettings();
- webSettings.setJavaScriptEnabled(true);
- webView.setWebViewClient(new WebViewClient() {
- @TargetApi(Build.VERSION_CODES.KITKAT)
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- view.post(() -> view.loadUrl(url));
- return false;
- }
- });
- });
- boolean banned = REQUIREMENT_BANNED.equals(data.requirement);
- if (!banned) {
- String token = data.captchaPass != null ? data.captchaPass[0] : null;
- String pin = data.captchaPass != null ? data.captchaPass[1] : null;
- String captchaPassCookie = null;
- if (token != null || pin != null) {
- if (getCaptchaPassData(token, pin).equals(lastCaptchaPassData)) {
- captchaPassCookie = lastCaptchaPassCookie;
- } else {
- captchaPassCookie = readCaptchaPass(data, token, pin);
- }
- }
- if (captchaPassCookie != null) {
- CaptchaData captchaData = new CaptchaData();
- captchaData.put(CAPTCHA_DATA_KEY_PASS_COOKIE, captchaPassCookie);
- return new ReadCaptchaResult(CaptchaState.PASS, captchaData)
- .setValidity(FourchanChanConfiguration.Captcha.Validity.LONG_LIFETIME);
- }
- }
- String captchaType = banned ? FourchanChanConfiguration.CAPTCHA_TYPE_RECAPTCHA_2 : data.captchaType;
- FourchanChanConfiguration configuration = FourchanChanConfiguration.get(this);
- FourchanChanLocator locator = FourchanChanLocator.get(this);
- ReadCaptchaResult result;
- if (FourchanChanConfiguration.CAPTCHA_TYPE_4CHAN_CAPTCHA.equals(captchaType)) {
- if (data.mayShowLoadButton) {
- return new ReadCaptchaResult(CaptchaState.NEED_LOAD, null);
- }
- String threadNumber = data.requirement == null ? data.threadNumber : "1";
- Uri.Builder builder = locator.createSysUri("captcha").buildUpon()
- .appendQueryParameter("board", data.boardName);
- if (threadNumber != null) {
- builder.appendQueryParameter("thread_id", threadNumber);
- }
- Uri uri = builder.build();
- String challenge;
- Bitmap image;
- Bitmap background;
- int wait = 2000;
- while (true) {
- try {
- webView.post(() -> webView.loadUrl(uri.toString()));
- // waiting for the result above
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- webView.post(() -> webView.evaluateJavascript(
- "(function() { return document.querySelector('pre').textContent; })();",
- captcha::set));
- // wait for the result above
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- JSONObject jsonObject = new JSONObject(captcha.get().substring(1, captcha.get().length() - 1).replace("\\\"", "\""));
- String error = jsonObject.optString("error");
- if ("You have to wait a while before doing this again".equals(error)) {
- try {
- Thread.sleep(wait);
- } catch (InterruptedException e) {
- throw new HttpException(0, null);
- }
- wait += 1000;
- } else {
- challenge = jsonObject.getString("challenge");
- byte[] imageBytes = Base64.decode(jsonObject.getString("img"), 0);
- byte[] backgroundBytes = Base64.decode(jsonObject.optString("bg"), 0);
- image = imageBytes.length == 0 ? null
- : BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
- background = backgroundBytes.length == 0 ? null
- : BitmapFactory.decodeByteArray(backgroundBytes, 0, backgroundBytes.length);
- break;
- }
- } catch (JSONException e) {
- throw new InvalidResponseException(e);
- }
- }
Add Comment
Please, Sign In to add comment