BlueMoonBot commited on
Commit
f47299a
·
verified ·
1 Parent(s): 1596fbf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -23
app.py CHANGED
@@ -1,28 +1,60 @@
1
- import asyncio
2
- from fastapi import FastAPI
3
- import uvicorn
 
4
 
5
- app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- @app.get("/")
8
- def home():
9
- return {"status": "Lichess bot is running!"}
10
 
11
- async def run_lichess_bot():
12
- print("Starting Lichess bot...")
13
- # --- YOUR COURSERA / LICHESS BOT CODE GOES HERE ---
14
- # Example:
15
- # while True:
16
- # await check_lichess_games()
17
- # await asyncio.sleep(1)
18
- pass
 
19
 
20
- @app.on_event("startup")
21
- async def startup_event():
22
- # This fires off your chess bot in the background
23
- # so it doesn't block the web server from starting
24
- asyncio.create_task(run_lichess_bot())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- if __name__ == "__main__":
27
- # Hugging Face strictly requires port 7860
28
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ import threading
2
+ import time
3
+ import requests
4
+ import yaml
5
 
6
+ # --- BACKGROUND MAIA TARGETER FOR HUGGING FACE ---
7
+ def maia_targeter_loop():
8
+ print("[Background Matchmaker] Waiting 20 seconds for the main bot to initialize...")
9
+ time.sleep(20)
10
+
11
+ # 1. Try to read the token from config.yml
12
+ try:
13
+ with open("config.yml", "r") as f:
14
+ config = yaml.safe_load(f)
15
+ # Handle Hugging Face environment variables if referenced via $
16
+ token = config.get("token", "")
17
+ if token.startswith("$"):
18
+ import os
19
+ token = os.environ.get(token.replace("$", ""), "")
20
+ except Exception:
21
+ token = ""
22
 
23
+ if not token:
24
+ print("[Background Matchmaker] Error: Could not resolve Lichess Token. Loop stopped.")
25
+ return
26
 
27
+ headers = {"Authorization": f"Bearer {token}"}
28
+ target_bots = ["maia1", "maia5", "maia9"]
29
+ challenge_data = {
30
+ "rated": "false",
31
+ "clock.limit": 180,
32
+ "clock.increment": 0,
33
+ "variant": "standard",
34
+ "keepAliveStream": "false"
35
+ }
36
 
37
+ print("[Background Matchmaker] Loop successfully activated!")
38
+ while True:
39
+ try:
40
+ # Check if our bot is busy playing a game
41
+ status_resp = requests.get("https://lichess.org/api/account", headers=headers)
42
+ if status_resp.status_code == 200 and not status_resp.json().get("playing", False):
43
+ # Bot is free! Try challenging the Maias
44
+ for bot in target_bots:
45
+ url = f"https://challenge.lichess.org/api/challenge/{bot}"
46
+ resp = requests.post(url, headers=headers, data=challenge_data)
47
+ if resp.status_code == 200:
48
+ print(f"[Background Matchmaker] Successfully challenged {bot}!")
49
+ time.sleep(90) # Hold while match is accepted
50
+ break
51
+ time.sleep(4) # Slight delay between target checks
52
+
53
+ time.sleep(45) # Sleep before checking status again
54
+ except Exception as e:
55
+ print(f"[Background Matchmaker] Loop encountered an error: {e}")
56
+ time.sleep(30)
57
 
58
+ # Start the background matchmaking thread immediately upon script startup
59
+ threading.Thread(target=maia_targeter_loop, daemon=True).start()
60
+ # --------------------------------------------------