Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- frontend/__pycache__/app.cpython-312.pyc +0 -0
- frontend/app.py +10 -1
- rain_collector.py +120 -66
frontend/__pycache__/app.cpython-312.pyc
CHANGED
|
Binary files a/frontend/__pycache__/app.cpython-312.pyc and b/frontend/__pycache__/app.cpython-312.pyc differ
|
|
|
frontend/app.py
CHANGED
|
@@ -4,7 +4,16 @@ import sqlite3
|
|
| 4 |
|
| 5 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 6 |
ROOT_DIR = os.path.dirname(BASE_DIR)
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__, static_folder='static', static_url_path='')
|
| 10 |
|
|
|
|
| 4 |
|
| 5 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 6 |
ROOT_DIR = os.path.dirname(BASE_DIR)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_db_path():
|
| 10 |
+
bucket = os.getenv('BUCKET_PATH')
|
| 11 |
+
if bucket:
|
| 12 |
+
return os.path.join(os.path.abspath(bucket), 'rain_metrics.db')
|
| 13 |
+
return os.path.join(ROOT_DIR, 'rain_metrics.db')
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
DB_PATH = get_db_path()
|
| 17 |
|
| 18 |
app = Flask(__name__, static_folder='static', static_url_path='')
|
| 19 |
|
rain_collector.py
CHANGED
|
@@ -124,14 +124,14 @@ def is_done_text(text):
|
|
| 124 |
|
| 125 |
def get_claim_buttons(page):
|
| 126 |
"""
|
| 127 |
-
Les VRAIS boutons de récupération : éléments .claim-
|
| 128 |
-
(
|
| 129 |
"""
|
| 130 |
buttons = []
|
| 131 |
try:
|
| 132 |
for frame in page.frames:
|
| 133 |
try:
|
| 134 |
-
for el in frame.locator('.claim-
|
| 135 |
try:
|
| 136 |
if el.is_visible():
|
| 137 |
buttons.append((frame, el))
|
|
@@ -206,11 +206,11 @@ def click_rain_locator(locator):
|
|
| 206 |
|
| 207 |
|
| 208 |
def count_claim_buttons(page, visible_only=False):
|
| 209 |
-
"""Nombre de .claim-
|
| 210 |
n = 0
|
| 211 |
try:
|
| 212 |
for frame in page.frames:
|
| 213 |
-
for el in frame.locator('.claim-
|
| 214 |
try:
|
| 215 |
if not visible_only or el.is_visible():
|
| 216 |
n += 1
|
|
@@ -221,10 +221,31 @@ def count_claim_buttons(page, visible_only=False):
|
|
| 221 |
return n
|
| 222 |
|
| 223 |
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
"""
|
| 226 |
-
Re-connexion / rechargement si la session est expirée
|
| 227 |
-
|
|
|
|
|
|
|
| 228 |
"""
|
| 229 |
try:
|
| 230 |
url = page.url or ''
|
|
@@ -233,23 +254,26 @@ def ensure_game_alive(page, phone, password):
|
|
| 233 |
login(page, phone, password)
|
| 234 |
save_session(page.context)
|
| 235 |
open_aviator(page)
|
| 236 |
-
|
| 237 |
-
game_frames = [f for f in page.frames if 'spribegaming' in f.url or 'aviator' in f.url]
|
| 238 |
-
if not game_frames:
|
| 239 |
print('⚠️ Frame du jeu absente, rechargement de la page Aviator...')
|
| 240 |
open_aviator(page)
|
|
|
|
|
|
|
|
|
|
| 241 |
except Exception as exc:
|
| 242 |
print(f'⚠️ Échec du contrôle de santé : {exc}')
|
|
|
|
| 243 |
|
| 244 |
|
| 245 |
def click_claim_loop(page, poll_interval=0.25, health_interval=120.0,
|
| 246 |
-
phone=None, password=None):
|
| 247 |
"""
|
| 248 |
-
Clique UNE fois sur CHAQUE bouton .claim-
|
| 249 |
-
Surveille
|
| 250 |
-
|
| 251 |
"""
|
| 252 |
claims = 0
|
|
|
|
| 253 |
last_dump = 0.0
|
| 254 |
last_candidates = time.time()
|
| 255 |
last_status = time.time()
|
|
@@ -260,8 +284,17 @@ def click_claim_loop(page, poll_interval=0.25, health_interval=120.0,
|
|
| 260 |
try:
|
| 261 |
while True:
|
| 262 |
try:
|
| 263 |
-
if time.time() - last_health > health_interval and (phone
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
last_health = time.time()
|
| 266 |
|
| 267 |
buttons = get_claim_buttons(page)
|
|
@@ -298,6 +331,8 @@ def click_claim_loop(page, poll_interval=0.25, health_interval=120.0,
|
|
| 298 |
print(f'⏳ Aucun bouton actif | URL: {page.url[:60]} | frames jeu: {len(game_frames)} '
|
| 299 |
f'| claim dans le DOM: {total} (visibles: {visible}) | login: {on_login}')
|
| 300 |
last_candidates = time.time()
|
|
|
|
|
|
|
| 301 |
except Exception:
|
| 302 |
pass
|
| 303 |
time.sleep(poll_interval)
|
|
@@ -337,6 +372,63 @@ def increment_rain_count(db_path=None):
|
|
| 337 |
return False
|
| 338 |
|
| 339 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
def main():
|
| 341 |
phone, password = load_environment()
|
| 342 |
ensure_session_dir()
|
|
@@ -347,56 +439,18 @@ def main():
|
|
| 347 |
except Exception:
|
| 348 |
claim_poll = 0.25
|
| 349 |
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
launch_args = {
|
| 353 |
-
'headless': False,
|
| 354 |
-
'args': [
|
| 355 |
-
"--window-size=1920,950",
|
| 356 |
-
"--no-sandbox",
|
| 357 |
-
"--disable-dev-shm-usage",
|
| 358 |
-
"--disable-gpu"
|
| 359 |
-
]
|
| 360 |
-
}
|
| 361 |
-
|
| 362 |
-
browser = playwright.chromium.launch(**launch_args)
|
| 363 |
-
context = make_context(browser)
|
| 364 |
-
page = context.new_page()
|
| 365 |
-
|
| 366 |
try:
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
print('⏳ Attente de 60 secondes (chargement du jeu) avant la capture de contrôle...')
|
| 378 |
-
time.sleep(60)
|
| 379 |
-
|
| 380 |
-
screenshot_path = os.path.join(get_bucket_base(), 'screenshot_verification.png')
|
| 381 |
-
page.screenshot(path=screenshot_path)
|
| 382 |
-
print(f'📸 Capture de contrôle enregistrée : {screenshot_path}')
|
| 383 |
-
|
| 384 |
-
click_claim_loop(page, poll_interval=claim_poll, phone=phone, password=password)
|
| 385 |
-
|
| 386 |
-
except KeyboardInterrupt:
|
| 387 |
-
print('\n🛑 Interruption par l\'utilisateur.')
|
| 388 |
-
except Exception as exc:
|
| 389 |
-
print(f'❌ Erreur : {exc}')
|
| 390 |
-
finally:
|
| 391 |
-
try:
|
| 392 |
-
context.close()
|
| 393 |
-
except Exception:
|
| 394 |
-
pass
|
| 395 |
-
try:
|
| 396 |
-
browser.close()
|
| 397 |
-
except Exception:
|
| 398 |
-
pass
|
| 399 |
-
print('🚪 Navigateur fermé (tentative effectuée).')
|
| 400 |
|
| 401 |
|
| 402 |
if __name__ == '__main__':
|
|
|
|
| 124 |
|
| 125 |
def get_claim_buttons(page):
|
| 126 |
"""
|
| 127 |
+
Les VRAIS boutons de récupération : éléments .btn.claim-btn visibles
|
| 128 |
+
(classes confirmées par inspection : « btn claim-btn ng-star-inserted »).
|
| 129 |
"""
|
| 130 |
buttons = []
|
| 131 |
try:
|
| 132 |
for frame in page.frames:
|
| 133 |
try:
|
| 134 |
+
for el in frame.locator('.btn.claim-btn').all():
|
| 135 |
try:
|
| 136 |
if el.is_visible():
|
| 137 |
buttons.append((frame, el))
|
|
|
|
| 206 |
|
| 207 |
|
| 208 |
def count_claim_buttons(page, visible_only=False):
|
| 209 |
+
"""Nombre de .btn.claim-btn dans le DOM (visibles ou non)."""
|
| 210 |
n = 0
|
| 211 |
try:
|
| 212 |
for frame in page.frames:
|
| 213 |
+
for el in frame.locator('.btn.claim-btn').all():
|
| 214 |
try:
|
| 215 |
if not visible_only or el.is_visible():
|
| 216 |
n += 1
|
|
|
|
| 221 |
return n
|
| 222 |
|
| 223 |
|
| 224 |
+
class RestartBrowser(Exception):
|
| 225 |
+
"""Lancé par la boucle pour demander un redémarrage complet du navigateur."""
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def game_frame_has_canvas(page):
|
| 229 |
+
"""Vrai si au moins une frame du jeu contient un canvas (jeu réellement rendu)."""
|
| 230 |
+
try:
|
| 231 |
+
for frame in page.frames:
|
| 232 |
+
if 'spribegaming' in frame.url or 'aviator' in frame.url:
|
| 233 |
+
try:
|
| 234 |
+
if frame.locator('canvas').count() > 0:
|
| 235 |
+
return True
|
| 236 |
+
except Exception:
|
| 237 |
+
continue
|
| 238 |
+
except Exception:
|
| 239 |
+
pass
|
| 240 |
+
return False
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def ensure_game_health(page, phone, password):
|
| 244 |
"""
|
| 245 |
+
Re-connexion / rechargement si la session est expirée, si la frame Spribe a disparu,
|
| 246 |
+
ou si aucun canvas n'est rendu (jeu figé / écran vide).
|
| 247 |
+
En cas d'échec (ex: reconnexion impossible), l'exception remonte pour être
|
| 248 |
+
comptée par l'appelant → redémarrage complet après plusieurs échecs.
|
| 249 |
"""
|
| 250 |
try:
|
| 251 |
url = page.url or ''
|
|
|
|
| 254 |
login(page, phone, password)
|
| 255 |
save_session(page.context)
|
| 256 |
open_aviator(page)
|
| 257 |
+
elif not [f for f in page.frames if 'spribegaming' in f.url or 'aviator' in f.url]:
|
|
|
|
|
|
|
| 258 |
print('⚠️ Frame du jeu absente, rechargement de la page Aviator...')
|
| 259 |
open_aviator(page)
|
| 260 |
+
elif not game_frame_has_canvas(page):
|
| 261 |
+
print('⚠️ Frame présente mais canvas de jeu absent (écran vide/figé), rechargement...')
|
| 262 |
+
open_aviator(page)
|
| 263 |
except Exception as exc:
|
| 264 |
print(f'⚠️ Échec du contrôle de santé : {exc}')
|
| 265 |
+
raise
|
| 266 |
|
| 267 |
|
| 268 |
def click_claim_loop(page, poll_interval=0.25, health_interval=120.0,
|
| 269 |
+
max_health_failures=3, phone=None, password=None):
|
| 270 |
"""
|
| 271 |
+
Clique UNE fois sur CHAQUE bouton .btn.claim-btn actif visible, sans cooldown.
|
| 272 |
+
Surveille la « santé » du jeu (reconnexion, reload, canvas) ; après plusieurs
|
| 273 |
+
échecs consécutifs, demande un redémarrage complet du navigateur.
|
| 274 |
"""
|
| 275 |
claims = 0
|
| 276 |
+
health_failures = 0
|
| 277 |
last_dump = 0.0
|
| 278 |
last_candidates = time.time()
|
| 279 |
last_status = time.time()
|
|
|
|
| 284 |
try:
|
| 285 |
while True:
|
| 286 |
try:
|
| 287 |
+
if time.time() - last_health > health_interval and (phone and password):
|
| 288 |
+
try:
|
| 289 |
+
ensure_game_health(page, phone, password)
|
| 290 |
+
health_failures = 0
|
| 291 |
+
except RestartBrowser:
|
| 292 |
+
raise
|
| 293 |
+
except Exception:
|
| 294 |
+
health_failures += 1
|
| 295 |
+
print(f'⚠️ Échec santé #{health_failures}/{max_health_failures}')
|
| 296 |
+
if health_failures >= max_health_failures:
|
| 297 |
+
raise RestartBrowser()
|
| 298 |
last_health = time.time()
|
| 299 |
|
| 300 |
buttons = get_claim_buttons(page)
|
|
|
|
| 331 |
print(f'⏳ Aucun bouton actif | URL: {page.url[:60]} | frames jeu: {len(game_frames)} '
|
| 332 |
f'| claim dans le DOM: {total} (visibles: {visible}) | login: {on_login}')
|
| 333 |
last_candidates = time.time()
|
| 334 |
+
except RestartBrowser:
|
| 335 |
+
raise
|
| 336 |
except Exception:
|
| 337 |
pass
|
| 338 |
time.sleep(poll_interval)
|
|
|
|
| 372 |
return False
|
| 373 |
|
| 374 |
|
| 375 |
+
def run_session(playwright, phone, password, claim_poll):
|
| 376 |
+
"""
|
| 377 |
+
Démarre Chromium, se connecte, ouvre Aviator et lance la boucle de collecte.
|
| 378 |
+
Une RestartBrowser remonte telle quelle pour être gérée par main().
|
| 379 |
+
"""
|
| 380 |
+
# ⚙️ headless=False car on utilise Xvfb !
|
| 381 |
+
launch_args = {
|
| 382 |
+
'headless': False,
|
| 383 |
+
'args': [
|
| 384 |
+
"--window-size=1920,950",
|
| 385 |
+
"--no-sandbox",
|
| 386 |
+
"--disable-dev-shm-usage",
|
| 387 |
+
"--disable-gpu"
|
| 388 |
+
]
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
browser = playwright.chromium.launch(**launch_args)
|
| 392 |
+
context = make_context(browser)
|
| 393 |
+
page = context.new_page()
|
| 394 |
+
|
| 395 |
+
try:
|
| 396 |
+
page.set_viewport_size(DESKTOP_VIEWPORT)
|
| 397 |
+
|
| 398 |
+
if not check_logged_in(page):
|
| 399 |
+
login(page, phone, password)
|
| 400 |
+
else:
|
| 401 |
+
print('✅ Session déjà connectée.')
|
| 402 |
+
|
| 403 |
+
save_session(context)
|
| 404 |
+
open_aviator(page)
|
| 405 |
+
|
| 406 |
+
print('⏳ Attente de 60 secondes (chargement du jeu) avant la capture de contrôle...')
|
| 407 |
+
time.sleep(60)
|
| 408 |
+
|
| 409 |
+
screenshot_path = os.path.join(get_bucket_base(), 'screenshot_verification.png')
|
| 410 |
+
page.screenshot(path=screenshot_path)
|
| 411 |
+
print(f'📸 Capture de contrôle enregistrée : {screenshot_path}')
|
| 412 |
+
|
| 413 |
+
click_claim_loop(page, poll_interval=claim_poll, phone=phone, password=password)
|
| 414 |
+
except RestartBrowser:
|
| 415 |
+
raise
|
| 416 |
+
except KeyboardInterrupt:
|
| 417 |
+
print('\n🛑 Interruption par l\'utilisateur.')
|
| 418 |
+
except Exception as exc:
|
| 419 |
+
print(f'❌ Erreur : {exc}')
|
| 420 |
+
finally:
|
| 421 |
+
try:
|
| 422 |
+
context.close()
|
| 423 |
+
except Exception:
|
| 424 |
+
pass
|
| 425 |
+
try:
|
| 426 |
+
browser.close()
|
| 427 |
+
except Exception:
|
| 428 |
+
pass
|
| 429 |
+
print('🚪 Navigateur fermé (tentative effectuée).')
|
| 430 |
+
|
| 431 |
+
|
| 432 |
def main():
|
| 433 |
phone, password = load_environment()
|
| 434 |
ensure_session_dir()
|
|
|
|
| 439 |
except Exception:
|
| 440 |
claim_poll = 0.25
|
| 441 |
|
| 442 |
+
restarts = 0
|
| 443 |
+
while True:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
try:
|
| 445 |
+
with sync_playwright() as playwright:
|
| 446 |
+
run_session(playwright, phone, password, claim_poll)
|
| 447 |
+
return
|
| 448 |
+
except RestartBrowser:
|
| 449 |
+
restarts += 1
|
| 450 |
+
print(f'🔄 Redémarrage complet du navigateur ({restarts}/5)...')
|
| 451 |
+
if restarts >= 5:
|
| 452 |
+
print('❌ Trop de redémarrages (5), arrêt du script.')
|
| 453 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
|
| 456 |
if __name__ == '__main__':
|