Update control logic and tests

This commit is contained in:
Andrei 2020-03-11 16:22:12 +03:00
parent 3ada499065
commit cc0dde2cca
5 changed files with 48 additions and 43 deletions

View File

@ -1,15 +1,20 @@
import aiohttp
import requests
from python3_anticaptcha import (
get_balance_url,
get_app_stats_url,
get_queue_status_url,
incorrect_recaptcha_url,
incorrect_imagecaptcha_url,
get_spend_stats_url,
send_funds_url,
)
# Адрес для получения баланса
get_balance_url = "https://api.anti-captcha.com/getBalance"
# Адрес для отправки жалобы на неверное решение капчи-изображения
incorrect_imagecaptcha_url = "https://api.anti-captcha.com/reportIncorrectImageCaptcha"
# Адрес для отправки жалобы на неверное решение ReCaptcha
incorrect_recaptcha_url = "https://api.anti-captcha.com/reportIncorrectRecaptcha"
# Адрес для получения информации о очереди
get_queue_status_url = "https://api.anti-captcha.com/getQueueStats"
# С помощью этого метода можно получить статистику трат за последние 24 часа.
get_spend_stats_url = "https://api.anti-captcha.com/getSpendingStats"
# Адрес для получения информации о приложении
get_app_stats_url = "https://api.anti-captcha.com/getAppStats"
# С помощью этого метода можно получить статистику трат за последние 24 часа.
send_funds_url = "https://api.anti-captcha.com/sendFunds"
# available app stats mods
mods = ("errors", "views", "downloads", "users", "money")
@ -218,7 +223,10 @@ class aioAntiCaptchaControl:
async with session.post(
get_balance_url, json={"clientKey": self.ANTICAPTCHA_KEY}
) as resp:
return await resp.json()
if await resp.text():
return await resp.json()
else:
return {"errorId": 1}
async def send_funds(
self, accountLogin: str = None, accountEmail: str = None, amount: float = None
@ -241,7 +249,10 @@ class aioAntiCaptchaControl:
# get response
async with aiohttp.ClientSession() as session:
async with session.post(send_funds_url, json=payload) as resp:
return await resp.json()
if await resp.text():
return await resp.json()
else:
return {"errorId": 1}
async def get_spend_stats(
self, date: int = None, queue: str = None, softId: int = None, ip: str = None
@ -270,7 +281,10 @@ class aioAntiCaptchaControl:
# get response
async with aiohttp.ClientSession() as session:
async with session.post(get_spend_stats_url, json=payload) as resp:
return await resp.json()
if await resp.text():
return await resp.json()
else:
return {"errorId": 1}
async def get_app_stats(self, softId: int, mode: str = "errors") -> dict:
"""
@ -284,7 +298,10 @@ class aioAntiCaptchaControl:
payload = {"clientKey": self.ANTICAPTCHA_KEY, "softId": softId, "mode": mode}
async with aiohttp.ClientSession() as session:
async with session.post(get_app_stats_url, json=payload) as resp:
return await resp.json()
if await resp.text():
return await resp.json()
else:
return {"errorId": 1}
async def complaint_on_result(self, reported_id: int, captcha_type: str = "image") -> dict:
f"""
@ -304,12 +321,18 @@ class aioAntiCaptchaControl:
if captcha_type == "image":
async with aiohttp.ClientSession() as session:
async with session.post(incorrect_imagecaptcha_url, json=payload) as resp:
return await resp.json()
if await resp.text():
return await resp.json()
else:
return {"errorId": 1}
# complaint on re-captcha
elif captcha_type == "recaptcha":
async with aiohttp.ClientSession() as session:
async with session.post(incorrect_recaptcha_url, json=payload) as resp:
return await resp.json()
if await resp.text():
return await resp.json()
else:
return {"errorId": 1}
@staticmethod
async def get_queue_status(queue_id: int) -> dict:
@ -347,4 +370,7 @@ class aioAntiCaptchaControl:
async with aiohttp.ClientSession() as session:
async with session.post(get_queue_status_url, json=payload) as resp:
return await resp.json()
if await resp.text():
return await resp.json()
else:
return {"errorId": 1}

View File

@ -1,9 +1,9 @@
import json
import time
import pika
import requests
import pika
from python3_anticaptcha import (
HOST,
PORT,

View File

@ -9,13 +9,6 @@ from .config import (
app_key,
get_result_url,
create_task_url,
get_balance_url,
get_app_stats_url,
get_queue_status_url,
incorrect_recaptcha_url,
incorrect_imagecaptcha_url,
send_funds_url,
get_spend_stats_url,
)
from .errors import ReadError, IdGetError, ParamError
from .get_answer import get_sync_result, get_async_result

View File

@ -6,20 +6,6 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
create_task_url = "https://api.anti-captcha.com/createTask"
# Адрес для получения ответа
get_result_url = "https://api.anti-captcha.com/getTaskResult"
# Адрес для получения баланса
get_balance_url = "https://api.anti-captcha.com/getBalance"
# Адрес для отправки жалобы на неверное решение капчи-изображения
incorrect_imagecaptcha_url = "https://api.anti-captcha.com/reportIncorrectImageCaptcha"
# Адрес для отправки жалобы на неверное решение ReCaptcha
incorrect_recaptcha_url = "https://api.anti-captcha.com/reportIncorrectRecaptcha"
# Адрес для получения информации о очереди
get_queue_status_url = "https://api.anti-captcha.com/getQueueStats"
# С помощью этого метода можно получить статистику трат за последние 24 часа.
get_spend_stats_url = "https://api.anti-captcha.com/getSpendingStats"
# Адрес для получения информации о приложении
get_app_stats_url = "https://api.anti-captcha.com/getAppStats"
# С помощью этого метода можно получить статистику трат за последние 24 часа.
send_funds_url = "https://api.anti-captcha.com/sendFunds"
# ключ приложения
app_key = "867"

View File

@ -74,7 +74,7 @@ class TestControl(MainAntiCaptcha):
assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)
with requests_mock.Mocker() as req_mock:
req_mock.post(config.get_balance_url, json=self.ERROR_RESPONSE_JSON)
req_mock.post(AntiCaptchaControl.get_balance_url, json=self.ERROR_RESPONSE_JSON)
control.get_balance()
history = req_mock.request_history
@ -94,7 +94,7 @@ class TestControl(MainAntiCaptcha):
mode = random.choice(AntiCaptchaControl.mods)
with requests_mock.Mocker() as req_mock:
req_mock.post(config.get_app_stats_url, json=self.ERROR_RESPONSE_JSON)
req_mock.post(AntiCaptchaControl.get_app_stats_url, json=self.ERROR_RESPONSE_JSON)
control.get_app_stats(softId=config.app_key, mode=mode)
history = req_mock.request_history
@ -116,7 +116,7 @@ class TestControl(MainAntiCaptcha):
task_id = 123456
with requests_mock.Mocker() as req_mock:
req_mock.post(config.incorrect_imagecaptcha_url, json=self.ERROR_RESPONSE_JSON)
req_mock.post(AntiCaptchaControl.incorrect_imagecaptcha_url, json=self.ERROR_RESPONSE_JSON)
control.complaint_on_result(
reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[0]
)
@ -139,7 +139,7 @@ class TestControl(MainAntiCaptcha):
task_id = 123456
with requests_mock.Mocker() as req_mock:
req_mock.post(config.incorrect_recaptcha_url, json=self.ERROR_RESPONSE_JSON)
req_mock.post(AntiCaptchaControl.incorrect_recaptcha_url, json=self.ERROR_RESPONSE_JSON)
control.complaint_on_result(
reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[1]
)
@ -158,7 +158,7 @@ class TestControl(MainAntiCaptcha):
def test_queue_payload(self):
queue_id = random.choice(AntiCaptchaControl.queue_ids)
with requests_mock.Mocker() as req_mock:
req_mock.post(config.get_queue_status_url, json=self.ERROR_RESPONSE_JSON)
req_mock.post(AntiCaptchaControl.get_queue_status_url, json=self.ERROR_RESPONSE_JSON)
AntiCaptchaControl.AntiCaptchaControl.get_queue_status(queue_id)
history = req_mock.request_history