-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
65 lines (56 loc) · 1.98 KB
/
player.py
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
# Librerias:
# Libraries:
import asyncio
import websockets
from pyppeteer import launch
# Correr el reproductor:
# Run the player:
async def run():
browser = await launch({
'headless': False,
'executablePath': 'C:/Program Files/Google/Chrome/Application/chrome.exe',
'args': [
'--autoplay-policy=no-user-gesture-required',
'--disable-web-security',
'--no-sandbox',
'--disable-setuid-sandbox',
'--start-maximized',
],
'defaultViewport': None,
})
pages = await browser.pages()
page = pages[0]
await page.evaluate('''() => {
window.moveTo(0, 0);
window.resizeTo(screen.width, screen.height);
}''')
# Ir a la página del reproductor:
# Go to the player page:
await page.goto('http://localhost:8000/player', {'waitUntil': 'networkidle2'})
# Esperar a que la API de YouTube se cargue:
# Wait for the YouTube API to load:
await page.waitForFunction("typeof YT !== 'undefined'", {'timeout': 60000})
# Esperar a que el reproductor se cargue:
# Wait for the player to load:
await page.waitForFunction("window.player && typeof window.player.loadVideoById === 'function'", {'timeout': 60000})
print("✅ Player Ready")
# Conectar al servidor WebSocket:
# Connect to the server WebSocket:
uri = "ws://localhost:8000/ws"
async with websockets.connect(uri) as websocket:
try:
while True:
message = await websocket.recv()
if message == "server_shutdown":
print("Received shutdown command. Closing player...")
await browser.close()
break
except websockets.exceptions.ConnectionClosed:
print("WebSocket connection closed")
except Exception as e:
print(f"Error: {e}")
finally:
await browser.close()
# Ejecutar el reproductor:
# Run the player:
asyncio.run(run())