|
| 1 | +package com.kinancity.core.captcha.twocaptchabasic; |
| 2 | + |
| 3 | +import com.kinancity.api.errors.TechnicalException; |
| 4 | +import com.kinancity.core.captcha.CaptchaException; |
| 5 | +import com.kinancity.core.captcha.CaptchaProvider; |
| 6 | +import com.kinancity.core.captcha.CaptchaQueue; |
| 7 | +import com.kinancity.core.captcha.impl.BaseCaptchaProvider; |
| 8 | +import com.kinancity.core.captcha.impl.BaseConfigurationException; |
| 9 | +import com.kinancity.core.captcha.ptc.PtcCaptchaData; |
| 10 | +import com.twocaptcha.TwoCaptcha; |
| 11 | +import com.twocaptcha.captcha.Captcha; |
| 12 | +import com.twocaptcha.captcha.ReCaptcha; |
| 13 | +import com.twocaptcha.exceptions.ApiException; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +@Slf4j |
| 21 | +public class TwoCaptchaBasicProvider extends BaseCaptchaProvider<TwoCaptchaTask> { |
| 22 | + |
| 23 | + public static final String OPTION_CUSTOM_HOST = "CUSTOM_HOST"; |
| 24 | + |
| 25 | + /** |
| 26 | + * 2 captcha Soft Id; |
| 27 | + */ |
| 28 | + private final int KINAN_SOFT_ID = 1816; |
| 29 | + |
| 30 | + private TwoCaptcha solver; |
| 31 | + |
| 32 | + public static CaptchaProvider getInstance(CaptchaQueue queue, String apiKey) throws CaptchaException { |
| 33 | + return getInstance(queue, apiKey, Optional.empty()); |
| 34 | + } |
| 35 | + |
| 36 | + public static CaptchaProvider getInstance(CaptchaQueue queue, String apiKey, Optional<String> altUrl) throws CaptchaException { |
| 37 | + return new TwoCaptchaBasicProvider(queue, apiKey, altUrl); |
| 38 | + } |
| 39 | + |
| 40 | + public TwoCaptchaBasicProvider(CaptchaQueue queue, String apiKey, Optional<String> altUrl) throws BaseConfigurationException { |
| 41 | + super(queue, apiKey); |
| 42 | + |
| 43 | + log.debug("Start using TwoCaptchaBasicProvider with api key starting by {}", apiKey.substring(0, 6)); |
| 44 | + |
| 45 | + solver = new TwoCaptcha(apiKey); |
| 46 | + solver.setSoftId(KINAN_SOFT_ID); |
| 47 | + |
| 48 | + // Custom Host for 2captcha alt |
| 49 | + altUrl.ifPresent(solver::setHost); |
| 50 | + altUrl.ifPresent(url -> log.info("TwoCaptchaBasicProvider with custom host : {}", url)); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public double getBalance() throws TechnicalException { |
| 56 | + try { |
| 57 | + return solver.balance(); |
| 58 | + } catch (Exception e) { |
| 59 | + throw new TechnicalException("Error getting balance", e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void checkTaskStatusAndProcess(TwoCaptchaTask task) { |
| 65 | + |
| 66 | + try { |
| 67 | + // Update Captcha Object |
| 68 | + String result = solver.getResult(task.getTaskId()); |
| 69 | + |
| 70 | + if (result != null) { |
| 71 | + this.onTaskSuccess(task); |
| 72 | + this.sendSolutionToQueue(result); |
| 73 | + } |
| 74 | + // Otherwise, we just wait |
| 75 | + |
| 76 | + } catch (Exception e) { |
| 77 | + log.error("Error getting Captcha Info", e); |
| 78 | + task.setCanBeRetried(false); |
| 79 | + this.onTaskFailed(task); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void createNewCaptchaTasks(int nbToRequest) { |
| 85 | + for (int i = 0; i < nbToRequest; i++) { |
| 86 | + if(isHasBalanceLeft()) { |
| 87 | + try { |
| 88 | + String captchaId = solver.send(getNewCaptchaTask()); |
| 89 | + this.onTaskCreationSuccess(new TwoCaptchaTask(captchaId)); |
| 90 | + } catch (Exception e) { |
| 91 | + if (e instanceof ApiException && e.getMessage().contains("ERROR_ZERO_BALANCE")) { |
| 92 | + this.onZeroBalanceError(); |
| 93 | + } else { |
| 94 | + log.error("Failed to create Captcha Task", e); |
| 95 | + } |
| 96 | + } |
| 97 | + } else { |
| 98 | + log.warn("Zero Balance reached, skip remaining captcha tasks creations"); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private Captcha getNewCaptchaTask() { |
| 104 | + ReCaptcha captcha = new ReCaptcha(); |
| 105 | + captcha.setSoftId(KINAN_SOFT_ID); |
| 106 | + captcha.setSiteKey(PtcCaptchaData.GOOGLE_SITE_KEY); |
| 107 | + captcha.setUrl(PtcCaptchaData.PAGE_URL); |
| 108 | + return captcha; |
| 109 | + } |
| 110 | +} |
0 commit comments