-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
296 lines (239 loc) · 9.68 KB
/
main.py
File metadata and controls
296 lines (239 loc) · 9.68 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python3
"""League of Legends API Client - Main Entry Point.
This is the main entry point for the League of Legends API client.
It provides a unified interface for all major functionality including:
- Account lookups
- Ranked information retrieval
- Match history analysis
- OBS overlay generation
- Live client detection
- Streaming session management
Usage:
python main.py lookup <game_name> <tag_line> [region]
python main.py overlay <game_name> <tag_line> [region]
python main.py monitor
python main.py detect
"""
import sys
import logging
from pathlib import Path
# Fix Windows console encoding for Unicode characters
if sys.platform == "win32":
import locale
import codecs
try:
sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
except AttributeError:
# For older Python versions
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
# Add src directory to path for imports
sys.path.insert(0, str(Path(__file__).parent / "src"))
from src.api.config import setup_logging
from src.data.lookup_account import LeagueAccountLookup
from src.overlay.obs_overlay import OBSOverlayExporter
from src.detection.client_detector import LeagueClientDetector
from src.detection.streaming_session_manager import StreamingSessionManager
def detect_account_region(game_name: str, tag_line: str) -> str:
"""Detect the correct region for an account by trying different regions."""
from src.api.riot_api import RiotAPIClient
from src.api.config import Config, DEFAULT_REGIONS_TO_TRY
config = Config()
api_client = RiotAPIClient(config)
# Check if user has configured a specific region first
if config.region:
try:
print(f"🌍 Trying configured region: {config.region.upper()}")
account = api_client.get_account_by_riot_id(game_name, tag_line)
if account:
print(f"🌍 Found account in configured region: {config.region.upper()}")
return config.region
except Exception:
print(f"⚠️ Account not found in configured region {config.region.upper()}, trying other regions")
# Use configurable regions list
regions_to_try = DEFAULT_REGIONS_TO_TRY
for region in regions_to_try:
try:
# Try to get account info from this region
account = api_client.get_account_by_riot_id(game_name, tag_line)
if account:
print(f"🌍 Found account in region: {region.upper()}")
return region
except Exception:
continue
# Default to NA1 if not found
print("⚠️ Could not detect region, defaulting to NA1")
return "na1"
def show_help():
"""Display help information."""
print("""
League of Legends API Client v1.0.0
USAGE:
python main.py <command> [arguments]
COMMANDS:
lookup <game_name> <tag_line> [region]
Look up a player's account, ranked info, and today's matches
Example: python main.py lookup CoachRogue2 Fill euw1
overlay <game_name> <tag_line> [region]
Generate OBS overlay files for a player
Example: python main.py overlay CoachRogue2 Fill euw1
monitor
Start real-time account monitoring for streaming
Automatically detects account switches and updates overlays
detect
Test League client detection and show current active account
help
Show this help message
SETUP:
1. Create a .env file in the project root
2. Add your Riot API key: RIOT_API_KEY=your_key_here
3. Run any command to get started!
EXAMPLES:
# Look up a specific player
python main.py lookup "Faker" "T1" kr
# Generate overlay for current detected account
python main.py overlay auto
# Start monitoring mode for streaming
python main.py monitor
""")
def cmd_lookup(args):
"""Handle lookup command."""
if len(args) < 2:
print("❌ Error: lookup requires game_name and tag_line")
print("Usage: python main.py lookup <game_name> <tag_line> [region]")
return False
game_name = args[0]
tag_line = args[1]
region = args[2] if len(args) > 2 else None
if region:
print(f"🔍 Looking up player: {game_name}#{tag_line} in {region.upper()}")
else:
print(f"🔍 Looking up player: {game_name}#{tag_line} (auto-detecting region...)")
try:
lookup_service = LeagueAccountLookup(region)
result = lookup_service.lookup_account(game_name, tag_line)
return result is not None
except Exception as e:
print(f"❌ Lookup failed: {e}")
return False
def cmd_overlay(args):
"""Handle overlay command."""
if len(args) >= 1 and args[0].lower() == "auto":
# Auto-detect current account
print("🔍 Auto-detecting current League account...")
detector = LeagueClientDetector()
account_info = detector.detect_current_account()
if account_info and account_info.get('tag_line'):
game_name = account_info['game_name']
tag_line = account_info['tag_line']
# Region will be auto-detected in the overlay function
region = None
print(f"✅ Detected account: {account_info['riot_id']} (region will be auto-detected)")
else:
print("❌ Could not detect account - please start a League game first")
return False
else:
if len(args) < 2:
print("❌ Error: overlay requires game_name and tag_line (or 'auto')")
print("Usage: python main.py overlay <game_name> <tag_line> [region]")
print(" python main.py overlay auto")
return False
game_name = args[0]
tag_line = args[1]
region = args[2] if len(args) > 2 else None
if region:
print(f"🎥 Generating OBS overlay for: {game_name}#{tag_line} in {region.upper()}")
else:
print(f"🎥 Generating OBS overlay for: {game_name}#{tag_line} (auto-detecting region...)")
try:
exporter = OBSOverlayExporter()
data = exporter.export_player_data(game_name, tag_line, region)
if "error" in data:
print(f"❌ Overlay generation failed: {data['error']}")
return False
abs_path = Path(exporter.output_dir).resolve()
print(f"✅ OBS overlay files generated successfully!")
print(f"📁 Files saved to: {abs_path}")
print(f"\n🎥 To use in OBS Studio:")
print(f" 1. Add Browser Source")
print(f" 2. Set URL to: file://{abs_path}/04_combined_overlay.html")
print(f" 3. Set Width: 800, Height: 200")
print(f" 4. Enable 'Shutdown source when not visible'")
print(f" 5. Enable 'Refresh browser when scene becomes active'")
return True
except Exception as e:
print(f"❌ Overlay generation failed: {e}")
return False
def cmd_monitor(args):
"""Handle monitor command."""
try:
# Import and call the main monitoring function
from src.detection.streaming_session_manager import main as monitor_main
monitor_main()
return True
except KeyboardInterrupt:
print("\n👋 Monitor stopped by user")
return True
except Exception as e:
print(f"❌ Monitor failed: {e}")
return False
def cmd_detect(args):
"""Handle detect command."""
print("🔍 Testing League client detection...")
try:
detector = LeagueClientDetector()
# Test connection
connection_test = detector.test_live_client_connection()
if connection_test['error']:
print(f"❌ Connection failed: {connection_test['error']}")
print("💡 Make sure League of Legends is running and you're in a game")
return False
if connection_test['live_client_available']:
print("✅ Live Client API is available!")
account_info = detector.detect_current_account()
if account_info:
print(f"🎯 Current account: {account_info['riot_id']}")
if account_info.get('tag_line'):
print(f" Game Name: {account_info['game_name']}")
print(f" Tag Line: {account_info['tag_line']}")
else:
print("⚠️ Could not extract account information")
else:
print("❌ Live Client API not available")
print("💡 Start a League of Legends game to enable detection")
return True
except Exception as e:
print(f"❌ Detection failed: {e}")
return False
def main():
"""Main entry point."""
# Setup logging
setup_logging(level='WARNING') # Reduce noise for CLI usage
# Parse command line arguments
if len(sys.argv) < 2:
show_help()
sys.exit(0)
command = sys.argv[1].lower()
args = sys.argv[2:]
# Route to appropriate command
success = False
if command in ['help', '-h', '--help']:
show_help()
success = True
elif command == 'lookup':
success = cmd_lookup(args)
elif command == 'overlay':
success = cmd_overlay(args)
elif command == 'monitor':
success = cmd_monitor(args)
elif command == 'detect':
success = cmd_detect(args)
else:
print(f"❌ Unknown command: {command}")
print("Use 'python main.py help' for usage information")
success = False
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()