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

View File

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

View File

@ -9,13 +9,6 @@ from .config import (
app_key, app_key,
get_result_url, get_result_url,
create_task_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 .errors import ReadError, IdGetError, ParamError
from .get_answer import get_sync_result, get_async_result 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" create_task_url = "https://api.anti-captcha.com/createTask"
# Адрес для получения ответа # Адрес для получения ответа
get_result_url = "https://api.anti-captcha.com/getTaskResult" 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" app_key = "867"

View File

@ -74,7 +74,7 @@ class TestControl(MainAntiCaptcha):
assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl) assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)
with requests_mock.Mocker() as req_mock: 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() control.get_balance()
history = req_mock.request_history history = req_mock.request_history
@ -94,7 +94,7 @@ class TestControl(MainAntiCaptcha):
mode = random.choice(AntiCaptchaControl.mods) mode = random.choice(AntiCaptchaControl.mods)
with requests_mock.Mocker() as req_mock: 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) control.get_app_stats(softId=config.app_key, mode=mode)
history = req_mock.request_history history = req_mock.request_history
@ -116,7 +116,7 @@ class TestControl(MainAntiCaptcha):
task_id = 123456 task_id = 123456
with requests_mock.Mocker() as req_mock: 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( control.complaint_on_result(
reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[0] reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[0]
) )
@ -139,7 +139,7 @@ class TestControl(MainAntiCaptcha):
task_id = 123456 task_id = 123456
with requests_mock.Mocker() as req_mock: 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( control.complaint_on_result(
reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[1] reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[1]
) )
@ -158,7 +158,7 @@ class TestControl(MainAntiCaptcha):
def test_queue_payload(self): def test_queue_payload(self):
queue_id = random.choice(AntiCaptchaControl.queue_ids) queue_id = random.choice(AntiCaptchaControl.queue_ids)
with requests_mock.Mocker() as req_mock: 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) AntiCaptchaControl.AntiCaptchaControl.get_queue_status(queue_id)
history = req_mock.request_history history = req_mock.request_history