Skip to content

Instantly share code, notes, and snippets.

@drbh
Created November 29, 2024 20:06
Show Gist options
  • Save drbh/81f71f3d4a60776ca64284c46549ffae to your computer and use it in GitHub Desktop.
Save drbh/81f71f3d4a60776ca64284c46549ffae to your computer and use it in GitHub Desktop.
start and stop your tv every 3 seconds in JS and Python
// Roku device IP address
const rokuIp = "192.168.1.252";
// Base URL for ECP commands
const baseUrl = `http://${rokuIp}:8060/keypress`;
// Delay between commands in milliseconds
const delay = 3000;
async function sendKeypress(command) {
try {
const response = await fetch(`${baseUrl}/${command}`, { method: 'POST' });
if (response.ok) {
console.log(`Sent command: ${command}`);
} else {
console.log(`Failed to send command: ${command}, Status code: ${response.status}`);
}
} catch (error) {
console.log(`Error sending command: ${error.message}`);
}
}
async function alternatePlayPause() {
while (true) {
await sendKeypress("Play");
await new Promise(resolve => setTimeout(resolve, delay));
await sendKeypress("Pause");
await new Promise(resolve => setTimeout(resolve, delay));
}
}
// Start the script
alternatePlayPause();
import requests
import time
# Roku device IP address
roku_ip = "192.168.1.252"
# Base URL for ECP commands
base_url = f"http://{roku_ip}:8060/keypress"
# Delay between commands in seconds
delay = 3
def send_keypress(command):
"""Send a keypress command to the Roku device."""
response = requests.post(f"{base_url}/{command}")
if response.status_code == 200:
print(f"Sent command: {command}")
else:
print(f"Failed to send command: {command}, Status code: {response.status_code}")
def alternate_play_pause():
"""Alternate between Play and Pause commands every 3 seconds."""
while True:
send_keypress("Play")
time.sleep(delay)
send_keypress("Pause")
time.sleep(delay)
if __name__ == "__main__":
alternate_play_pause()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment