Skip to content

Instantly share code, notes, and snippets.

@redavis22
Forked from ranman/twitch_plays_aws_console.py
Created February 5, 2017 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redavis22/2d0901366755b282b43503ac6cc24e31 to your computer and use it in GitHub Desktop.
Save redavis22/2d0901366755b282b43503ac6cc24e31 to your computer and use it in GitHub Desktop.
from collections import defaultdict
import time
import irc.client
import pyaudio
from pymouse import PyMouse
from pykeyboard import PyKeyboard
import win32ui
import boto3
polly = boto3.client('polly', region_name='us-east-1')
p = pyaudio.PyAudio()
win = win32ui.FindWindow(None, "AWS Management Console - Google Chrome")
win.SetForegroundWindow()
win.SetFocus()
m = PyMouse()
k = PyKeyboard()
x_dim, y_dim = m.screen_size()
def mouse_move(x, y):
print "fired mouse move"
if 0 < x < x_dim and 60 < y < y_dim - 80:
m.move(x, y)
else:
print "Mouse Move: out of bounds"
def mouse_scroll(lines):
if -10 <= lines <= 10 and lines != 0:
m.scroll(vertical=lines)
else:
print "Mouse Scroll: out of bounds"
def mouse_click():
print "fired mouse click"
pos = m.position()
m.click(pos[0], pos[1], 1, 1)
def type_string(char_string):
print "fired type string"
if len(char_string) < 256:
k.type_string(char_string)
else:
print "Keyboard type_string: string too long"
def press_key(key):
k.tap_key(key)
valid_commands = {
'move': mouse_move,
'scroll': mouse_scroll,
'click': mouse_click,
'type': type_string,
'key': press_key
}
def validate_command(command):
for valid_command in valid_commands.keys():
if command.startswith(valid_command):
return True
return False
def speak_winner(winner):
text = "The winning command was {} with {} votes".format(*winner)
resp = polly.synthesize_speech(Text=text, TextType="text", OutputFormat="pcm", VoiceId='Brian')
stream = p.open(format=p.get_format_from_width(width=2), channels=1, rate=16000, output=True)
stream.write(resp['AudioStream'].read())
stream.stop_stream()
stream.close()
def execute_command(command_str):
if command_str.lower().startswith('move'):
#command, args = command_str.split(' ')
command = 'move'
args = command_str[4:] #zomg hacks
print args
args = [int(x) for x in args.split(',')]
valid_commands[command](*args)
elif command_str.lower().startswith('type'):
arg = command_str[4:]
valid_commands['type'](arg)
elif command_str.lower() == 'click':
valid_commands['click']()
elif command_str.lower().startswith('key'):
arg = command_str[3:]
valid_commands['key'](arg)
def chat_handler(connection, event):
global restart
global voted
global votes
if event.source not in voted and validate_command(event.arguments[0].lower()):
votes[event.arguments[0].lower()] += 1
voted.append(event.source)
if time.time() > restart:
winner = ("click", 0)
for command, votes in votes.items():
if votes > winner[1]:
winner = (command, votes)
execute_command("key 17")
execute_command(command)
restart = time.time() + 10
votes = defaultdict(int)
voted = []
speak_winner(winner)
voted = []
votes = defaultdict(int)
if __name__ == '__main__':
restart = time.time() + 10
client = irc.client.Reactor()
client.add_global_handler('pubmsg', chat_handler)
server = client.server()
server.connect("irc.chat.twitch.tv", 6667, "justinfan1234")
server.join('#aws')
print server.is_connected()
client.process_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment