capturing capcha tokens
This commit is contained in:
@ -438,6 +438,16 @@ class MEXCRequestInterceptor:
|
||||
if self.session_cookies:
|
||||
print(f" 🍪 Cookies: {self.cookies_file}")
|
||||
|
||||
# Extract and save CAPTCHA tokens from captured requests
|
||||
captcha_tokens = self.extract_captcha_tokens()
|
||||
if captcha_tokens:
|
||||
captcha_file = f"mexc_captcha_tokens_{self.timestamp}.json"
|
||||
with open(captcha_file, 'w') as f:
|
||||
json.dump(captcha_tokens, f, indent=2)
|
||||
logger.info(f"Saved CAPTCHA tokens to {captcha_file}")
|
||||
else:
|
||||
logger.warning("No CAPTCHA tokens found in captured requests")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error saving data: {e}")
|
||||
|
||||
@ -487,6 +497,28 @@ class MEXCRequestInterceptor:
|
||||
if self.save_to_file and (self.captured_requests or self.captured_responses):
|
||||
self._save_all_data()
|
||||
logger.info("Final data save complete")
|
||||
|
||||
def extract_captcha_tokens(self):
|
||||
"""Extract CAPTCHA tokens from captured requests"""
|
||||
captcha_tokens = []
|
||||
for request in self.captured_requests:
|
||||
if 'captcha-token' in request.get('headers', {}):
|
||||
token = request['headers']['captcha-token']
|
||||
captcha_tokens.append({
|
||||
'token': token,
|
||||
'url': request.get('url', ''),
|
||||
'timestamp': request.get('timestamp', '')
|
||||
})
|
||||
elif 'captcha' in request.get('url', '').lower():
|
||||
response = request.get('response', {})
|
||||
if response and 'captcha-token' in response.get('headers', {}):
|
||||
token = response['headers']['captcha-token']
|
||||
captcha_tokens.append({
|
||||
'token': token,
|
||||
'url': request.get('url', ''),
|
||||
'timestamp': request.get('timestamp', '')
|
||||
})
|
||||
return captcha_tokens
|
||||
|
||||
def main():
|
||||
"""Main function to run the interceptor"""
|
||||
|
@ -19,6 +19,8 @@ from typing import Dict, List, Optional, Any
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
from urllib.parse import urlencode
|
||||
import glob
|
||||
import os
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -191,16 +193,30 @@ class MEXCFuturesWebClient:
|
||||
def _extract_captcha_token_from_browser(self) -> str:
|
||||
"""
|
||||
Extract captcha token from browser session using stored cookies or requests.
|
||||
This is a placeholder for actual implementation which would interact with browser data.
|
||||
This method looks for the most recent mexc_captcha_tokens JSON file to retrieve a token.
|
||||
"""
|
||||
try:
|
||||
# Placeholder for extracting token from browser session or stored data
|
||||
# In a real scenario, this would parse the mexc_requests JSON file or interact with Selenium
|
||||
logger.info("MEXC: Attempting to extract captcha token from browser data")
|
||||
# For now, return empty string as placeholder
|
||||
return ""
|
||||
# Look for the most recent mexc_captcha_tokens file
|
||||
captcha_files = glob.glob("mexc_captcha_tokens_*.json")
|
||||
if not captcha_files:
|
||||
logger.error("MEXC: No CAPTCHA token files found")
|
||||
return ""
|
||||
|
||||
# Sort files by timestamp (most recent first)
|
||||
latest_file = max(captcha_files, key=os.path.getctime)
|
||||
logger.info(f"MEXC: Using CAPTCHA token file {latest_file}")
|
||||
|
||||
with open(latest_file, 'r') as f:
|
||||
captcha_data = json.load(f)
|
||||
|
||||
if captcha_data and isinstance(captcha_data, list) and len(captcha_data) > 0:
|
||||
# Return the most recent token
|
||||
return captcha_data[0].get('token', '')
|
||||
else:
|
||||
logger.error("MEXC: No valid CAPTCHA tokens found in file")
|
||||
return ""
|
||||
except Exception as e:
|
||||
logger.error(f"MEXC: Error extracting captcha token from browser: {str(e)}")
|
||||
logger.error(f"MEXC: Error extracting captcha token from browser data: {str(e)}")
|
||||
return ""
|
||||
|
||||
def generate_signature(self, method: str, path: str, params: Dict[str, Any],
|
||||
|
Reference in New Issue
Block a user