changes
This commit is contained in:
parent
4fe3efa045
commit
d7b15b3708
@ -1,7 +1,9 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
import hashlib
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
from typing import Any, Dict, List, Mapping, Union
|
from typing import Any, Dict, List, Mapping, Union
|
||||||
|
from requests_toolbelt.utils import dump
|
||||||
|
|
||||||
from requests import Response, Session
|
from requests import Response, Session
|
||||||
|
|
||||||
@ -45,6 +47,7 @@ class SlowRequests(Session):
|
|||||||
self.request_log_name = utils.get_file(utils.now().strftime("debug/requests_%Y-%m-%d.log"))
|
self.request_log_name = utils.get_file(utils.now().strftime("debug/requests_%Y-%m-%d.log"))
|
||||||
self.last_time = utils.now()
|
self.last_time = utils.now()
|
||||||
self.headers.update({'User-Agent': user_agent})
|
self.headers.update({'User-Agent': user_agent})
|
||||||
|
self.hooks["response"] = [self._log_response]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def as_dict(self):
|
def as_dict(self):
|
||||||
@ -55,7 +58,7 @@ class SlowRequests(Session):
|
|||||||
self._slow_down_requests()
|
self._slow_down_requests()
|
||||||
self._log_request(url, method, **kwargs)
|
self._log_request(url, method, **kwargs)
|
||||||
resp = super().request(method, url, *args, **kwargs)
|
resp = super().request(method, url, *args, **kwargs)
|
||||||
self._log_response(url, resp)
|
self._log_response(resp)
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
def _slow_down_requests(self):
|
def _slow_down_requests(self):
|
||||||
@ -85,12 +88,14 @@ class SlowRequests(Session):
|
|||||||
with open(self.request_log_name, 'ab') as file:
|
with open(self.request_log_name, 'ab') as file:
|
||||||
file.write(body.encode("UTF-8"))
|
file.write(body.encode("UTF-8"))
|
||||||
|
|
||||||
def _log_response(self, url, resp, redirect: bool = False):
|
def _log_response(self, response: Response, *args, **kwargs):
|
||||||
|
redirect = kwargs.get('redirect')
|
||||||
|
url = response.request.url
|
||||||
if self.debug:
|
if self.debug:
|
||||||
if resp.history and not redirect:
|
if response.history and not redirect:
|
||||||
for hist_resp in resp.history:
|
for hist_resp in response.history:
|
||||||
self._log_request(hist_resp.request.url, 'REDIRECT')
|
self._log_request(hist_resp.request.url, 'REDIRECT')
|
||||||
self._log_response(hist_resp.request.url, hist_resp, redirect=True)
|
self._log_response(hist_resp, redirect=True)
|
||||||
|
|
||||||
fd_path = 'debug/requests'
|
fd_path = 'debug/requests'
|
||||||
fd_time = self.last_time.strftime('%Y/%m/%d/%H-%M-%S')
|
fd_time = self.last_time.strftime('%Y/%m/%d/%H-%M-%S')
|
||||||
@ -98,13 +103,17 @@ class SlowRequests(Session):
|
|||||||
fd_extra = '_REDIRECT' if redirect else ""
|
fd_extra = '_REDIRECT' if redirect else ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
utils.json.loads(resp.text)
|
utils.json.loads(response.text)
|
||||||
fd_ext = 'json'
|
fd_ext = 'json'
|
||||||
except utils.json.JSONDecodeError:
|
except utils.json.JSONDecodeError:
|
||||||
fd_ext = 'html'
|
fd_ext = 'html'
|
||||||
|
|
||||||
filename = f'{fd_path}/{fd_time}_{fd_name}{fd_extra}.{fd_ext}'
|
filename = f'{fd_path}/{fd_time}_{fd_name}{fd_extra}.{fd_ext}'
|
||||||
utils.write_file(filename, resp.text)
|
utils.write_file(filename, response.text)
|
||||||
|
|
||||||
|
if not redirect:
|
||||||
|
data = dump.dump_all(response)
|
||||||
|
utils.write_file(f'debug/dumps/{fd_time}_{fd_name}{fd_extra}.{fd_ext}', data.decode('utf8'))
|
||||||
|
|
||||||
|
|
||||||
class CitizenBaseAPI:
|
class CitizenBaseAPI:
|
||||||
@ -152,12 +161,18 @@ class CitizenBaseAPI:
|
|||||||
return self.post(f'{self.url}/main/sessionGetChallenge', data=data)
|
return self.post(f'{self.url}/main/sessionGetChallenge', data=data)
|
||||||
|
|
||||||
def _post_main_session_unlock(
|
def _post_main_session_unlock(
|
||||||
self, captcha: int, image: str, challenge: str, coords: List[Dict[str, int]], src: str
|
self, captcha_id: int, image_id: str, challenge_id: str, coords: List[Dict[str, int]], src: str
|
||||||
) -> Response:
|
) -> Response:
|
||||||
env = dict(l=['tets', ], s=[], c=["l_chathwe", "l_chatroom"], m=0)
|
env = dict(l=['tets', ], s=[], c=[c for c in self._req.cookies.keys() if not c.startswith('erpk')], m=0)
|
||||||
data = dict(_token=self.token, captchaId=captcha, imageId=image, challengeId=challenge,
|
cookies = dict(sh=hashlib.sha256(','.join(env['l']).encode('utf8')).hexdigest(),
|
||||||
clickMatrix=utils.json_dumps(coords).replace(' ', ''), isMobile=0, env=utils.b64json(env), src=src)
|
ch=hashlib.sha256(','.join(env['c']).encode('utf8')).hexdigest())
|
||||||
return self.post(f'{self.url}/main/sessionUnlock', data=data)
|
self._req.cookies.update(cookies)
|
||||||
|
if not env['c']:
|
||||||
|
env['c'] = ['']
|
||||||
|
b64_env = utils.b64json(env)
|
||||||
|
data = dict(_token=self.token, captchaId=captcha_id, imageId=image_id, challengeId=challenge_id,
|
||||||
|
clickMatrix=utils.json_dumps(coords).replace(' ', ''), isMobile=0, env=b64_env, src=src)
|
||||||
|
return self.post(f'{self.url}/main/sessionUnlock', data=data, headers={'X-Requested-With': 'XMLHttpRequest'})
|
||||||
|
|
||||||
|
|
||||||
class ErepublikAnniversaryAPI(CitizenBaseAPI):
|
class ErepublikAnniversaryAPI(CitizenBaseAPI):
|
||||||
|
@ -11,9 +11,9 @@ from typing import Any, Dict, List, NoReturn, Optional, Set, Tuple, Union
|
|||||||
|
|
||||||
from requests import RequestException, Response
|
from requests import RequestException, Response
|
||||||
|
|
||||||
from . import access_points, classes, constants, types, utils
|
from . import access_points, classes, constants, _types as types, utils
|
||||||
from .classes import OfferItem
|
from .classes import OfferItem
|
||||||
from .logging import ErepublikErrorHTTTPHandler, ErepublikFileHandler, ErepublikFormatter, ErepublikLogConsoleHandler
|
from ._logging import ErepublikErrorHTTTPHandler, ErepublikFileHandler, ErepublikFormatter, ErepublikLogConsoleHandler
|
||||||
|
|
||||||
|
|
||||||
class BaseCitizen(access_points.CitizenAPI):
|
class BaseCitizen(access_points.CitizenAPI):
|
||||||
@ -256,21 +256,21 @@ class BaseCitizen(access_points.CitizenAPI):
|
|||||||
captcha_id = data.get('sessionValidation', {}).get("captchaId")
|
captcha_id = data.get('sessionValidation', {}).get("captchaId")
|
||||||
captcha_data = self._post_main_session_get_challenge(captcha_id).json()
|
captcha_data = self._post_main_session_get_challenge(captcha_id).json()
|
||||||
coordinates = self.solve_captcha(captcha_data['src'])
|
coordinates = self.solve_captcha(captcha_data['src'])
|
||||||
|
while True:
|
||||||
|
r = self._post_main_session_unlock(
|
||||||
|
captcha_id, captcha_data['imageId'], captcha_data['challengeId'], coordinates, captcha_data['src']
|
||||||
|
)
|
||||||
|
rj = r.json()
|
||||||
|
if not rj.get('error') and rj.get('verified'):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
raise classes.ErepublikException('Captcha failed!')
|
||||||
|
except classes.ErepublikException:
|
||||||
|
self.report_error('Captcha failed!')
|
||||||
|
captcha_data = self._post_main_session_get_challenge(captcha_id, captcha_data['imageId']).json()
|
||||||
|
coordinates = self.solve_captcha(captcha_data['src'])
|
||||||
|
|
||||||
for x in range(5):
|
|
||||||
captcha_data = self._post_main_session_get_challenge(captcha_id, captcha_data['imageId']).json()
|
|
||||||
coordinates = self.solve_captcha(captcha_data['src'])
|
|
||||||
|
|
||||||
r = self._post_main_session_unlock(
|
|
||||||
captcha_id, captcha_data['imageId'], captcha_data['challengeId'], coordinates, captcha_data['src']
|
|
||||||
)
|
|
||||||
rj = r.json()
|
|
||||||
if not rj.get('error') and rj.get('verified'):
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
self.report_error('Captcha failed!')
|
|
||||||
if retry < 6:
|
|
||||||
return self.do_captcha_challenge(retry + 1)
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def refresh_captcha_image(self, captcha_id: int, image_id: str):
|
def refresh_captcha_image(self, captcha_id: int, image_id: str):
|
||||||
|
@ -7,7 +7,7 @@ from typing import Any, Dict, Generator, Iterable, List, NamedTuple, NoReturn, U
|
|||||||
|
|
||||||
from requests import Response, Session, post
|
from requests import Response, Session, post
|
||||||
|
|
||||||
from . import constants, types, utils
|
from . import constants, _types as types, utils
|
||||||
|
|
||||||
__all__ = ['Battle', 'BattleDivision', 'BattleSide', 'Company', 'Config', 'Details', 'Energy', 'ErepublikException',
|
__all__ = ['Battle', 'BattleDivision', 'BattleSide', 'Company', 'Config', 'Details', 'Energy', 'ErepublikException',
|
||||||
'ErepublikNetworkException', 'EnergyToFight', 'Holding', 'Inventory', 'MyCompanies', 'OfferItem', 'Politics',
|
'ErepublikNetworkException', 'EnergyToFight', 'Holding', 'Inventory', 'MyCompanies', 'OfferItem', 'Politics',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user