You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

capMonster.ts 2.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import axios from 'axios'
  2. import { ICaptchaResolver } from '../interfaces/ICaptchaResolver';
  3. export class CapMonster implements ICaptchaResolver {
  4. private readonly CAPTCHA_KEY: string = process.env.CAP_MONSTER_KEY
  5. private readonly CAPTCHA_BALANCE: string =
  6. 'https://api.capmonster.cloud/getBalance'
  7. private readonly CAPTCHA_CREATE: string =
  8. 'https://api.capmonster.cloud/createTask'
  9. private readonly CAPTCHA_RESULT: string =
  10. 'https://api.capmonster.cloud/getTaskResult'
  11. public async temSaldoDisponivel (): Promise<boolean> {
  12. try {
  13. const balanceData = {
  14. clientKey: this.CAPTCHA_KEY
  15. }
  16. const { data } = await axios.post(this.CAPTCHA_BALANCE, balanceData)
  17. if (data.errorId > 0) {
  18. console.log(`[consultar-saldo-captcha] - ${data.errorCode}`)
  19. return false
  20. }
  21. return data.balance > 0
  22. } catch (error) {
  23. console.log(`[consultar-saldo-captcha] - ${error.message}`)
  24. return false
  25. }
  26. }
  27. public async resolver (imageData: string): Promise<string> {
  28. const taskId = await this.criar(imageData)
  29. if (taskId === 0) return null
  30. return await this.resultado(taskId)
  31. }
  32. private async criar (imageData: string): Promise<number> {
  33. try {
  34. const createTask = {
  35. clientKey: this.CAPTCHA_KEY,
  36. task: {
  37. type: 'ImageToTextTask',
  38. body: imageData
  39. }
  40. }
  41. const { data } = await axios.post(this.CAPTCHA_CREATE, createTask)
  42. if (data.errorId > 0) {
  43. console.log(`[criar-captcha] - ${data.errorCode}`)
  44. return 0
  45. }
  46. return data.taskId
  47. } catch (error) {
  48. console.log(`[criar-captcha] - ${error.message}`)
  49. return 0
  50. }
  51. }
  52. private async resultado (taskId: number): Promise<string> {
  53. let tentativas = 0
  54. let error: boolean = false
  55. let captchaData = null
  56. const getResultTask = {
  57. clientKey: this.CAPTCHA_KEY,
  58. taskId: taskId
  59. }
  60. do {
  61. try {
  62. const {
  63. data: { errorId, errorCode, solution, status }
  64. } = await axios.post(this.CAPTCHA_RESULT, getResultTask)
  65. if (errorId > 0) {
  66. console.log(
  67. `[resultado-captcha] - Task: ${taskId} Erro: ${errorCode} Tentativas: ${tentativas}`
  68. )
  69. error = true
  70. break
  71. }
  72. if (status === 'ready') {
  73. captchaData = solution.text
  74. break
  75. }
  76. tentativas += 1
  77. await new Promise(r => setTimeout(r, 10 * 1000))
  78. } catch (error) {
  79. console.log(
  80. `[resultado-captcha] - Task: ${taskId} Erro: ${error.message} Tentativas: ${tentativas}`
  81. )
  82. error = true
  83. break
  84. }
  85. } while (tentativas < 20)
  86. if (error) return null
  87. return captchaData
  88. }
  89. }
  90. export default new CapMonster()