-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_api_key.py
More file actions
135 lines (108 loc) Β· 4.32 KB
/
setup_api_key.py
File metadata and controls
135 lines (108 loc) Β· 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
ALICE Bot - API Key Setup Script
Helps configure and validate BSCScan API key
"""
import json
import requests
from pathlib import Path
def setup_api_key():
"""Setup and validate BSCScan API key"""
print("π ALICE Bot - API Key Setup")
print("=" * 50)
# Check if config file exists
config_file = Path("credentials/bscscan_key.json")
if config_file.exists():
print("π Found existing configuration file")
with open(config_file, 'r') as f:
config = json.load(f)
current_key = config.get('bscscan_api_key', '')
if current_key and current_key != 'YourApiKeyHere':
print(f"π Current API Key: {current_key[:8]}...{current_key[-4:]}")
choice = input("\nTest current API key? (y/n): ").lower().strip()
if choice == 'y':
if test_api_key(current_key):
print("β
Current API key is working!")
return True
else:
print("β Current API key failed test")
else:
print("β οΈ No valid API key found in configuration")
else:
print("π No configuration file found")
# Get new API key
print("\nπ Get your FREE BSCScan API key from:")
print(" https://bscscan.com/apis")
print(" 1. Create account")
print(" 2. Go to API-KEYs")
print(" 3. Click 'Add' to create new API key")
while True:
api_key = input(f"\nπ Enter your BSCScan API key: ").strip()
if not api_key:
print("β API key cannot be empty")
continue
if len(api_key) < 20:
print("β API key seems too short")
continue
print(f"\nπ§ͺ Testing API key: {api_key[:8]}...{api_key[-4:]}")
if test_api_key(api_key):
print("β
API key test successful!")
# Save to config file
config_file.parent.mkdir(exist_ok=True)
config = {
"bscscan_api_key": api_key,
"rate_limit": 5,
"timeout": 30,
"max_retries": 3,
"base_url": "https://api.bscscan.com/api"
}
with open(config_file, 'w') as f:
json.dump(config, f, indent=4)
print(f"πΎ API key saved to: {config_file}")
print("\nπ You can now run ALICE Bot:")
print(" python base.py sc WALLET_ADDRESS p Vv output.txt")
return True
else:
print("β API key test failed")
retry = input("Try another key? (y/n): ").lower().strip()
if retry != 'y':
break
return False
def test_api_key(api_key: str) -> bool:
"""Test API key with a simple request"""
try:
url = "https://api.bscscan.com/api"
params = {
'module': 'stats',
'action': 'bnbsupply',
'apikey': api_key
}
print("π‘ Sending test request...")
response = requests.get(url, params=params, timeout=10)
if response.status_code != 200:
print(f"β HTTP Error: {response.status_code}")
return False
data = response.json()
if data.get('status') == '1':
print("β
API key is valid and working")
return True
else:
error_msg = data.get('message', 'Unknown error')
print(f"β API Error: {error_msg}")
if 'invalid api key' in error_msg.lower():
print("π‘ Hint: Make sure you copied the API key correctly")
elif 'rate limit' in error_msg.lower():
print("π‘ Hint: API key is valid but rate limited")
return True # Key is valid, just rate limited
return False
except requests.RequestException as e:
print(f"β Network Error: {e}")
return False
except Exception as e:
print(f"β Unexpected Error: {e}")
return False
if __name__ == "__main__":
if setup_api_key():
print("\nπ Setup completed successfully!")
else:
print("\nβ Setup failed. Please try again.")