def getMidi(self,midi_device): c = 0 for x in range( 0, pygame.midi.get_count() ): twitch_bot_utils.printer(pygame.midi.get_device_info(x)[1]) if pygame.midi.get_device_info(x)[1] == midi_device: twitch_bot_utils.printer("Found midi: %s" % c) return c c = c + 1
def __init__(self,devicename,serial_device): #Midi initialization pygame.midi.init() midi_device = self.getMidi("MIDISPORT 1x1 In") twitch_bot_utils.printer("Got midi: %s" % midi_device) self.ser = serial_device self.midi = pygame.midi.Input(midi_device) #setup thread object self.t = threading.Thread(target=self.midiDrumsThread) self.t.daemon = True
def user_stack_consumer(): global user_stack while True: if len(user_stack)>0 and animating==0: twitch_bot_utils.printer("user stack consumer DEBUG!!!!!!!!!!!: %s %s" % (len(user_stack),animating)) user,data = user_stack.pop(0) twitch_bot_utils.printer("Checking a buffered string: %s" % data) user_commands(user,data) time.sleep(1)
def get_game(): try: url = "https://api.twitch.tv/kraken/streams/%s" % auth.get_streamer() twitch_bot_utils.printer("Checking game...") data = requests.get(url=url) binary = data.content output = json.loads(binary) game = output['stream']['game'] return game except: return False
def set_top(hwnd): win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,0,0,width,height,win32con.SWP_NOACTIVATE) twitch_bot_utils.printer("SetWindowPos to HWND_TOPMOST and SWP_NOACTIVATE") style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X before" % style) style = style | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_TOPMOST twitch_bot_utils.printer("Setting style %X" % style) win32api.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, style) twitch_bot_utils.printer("Set style %X" % style) style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X after" % style)
def set_top(hwnd): win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,x,y,w,h,win32con.SWP_NOACTIVATE) twitch_bot_utils.printer("SetWindowPos to HWND_TOPMOST and SWP_NOACTIVATE") style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X before" % style) style = style | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_TOPMOST twitch_bot_utils.printer("Setting style %X" % style) win32api.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, style) twitch_bot_utils.printer("Set style %X" % style) style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X after" % style)
def admin_commands(user,data): global stayAlive #if user.lower() == auth.get_streamer(): if auth.is_admin(user): #split irc messages into parts by white space parts = data.lower().split() twitch_bot_utils.printer("User is admin, checking for commands") command = parts[0][1:] #get the first "word" and remove the first character which is ":" if command == "!restart" or command == "!reload": stayAlive = 0 if command == "!raffle": raffle() return True
def midiDrumsThread(self): twitch_bot_utils.printer("Started Midi Thread!") while self.midi.poll(): self.midi.read(1000) drums = { 38 : "ffffff", #snare 40 : "ffffff", #snare rim 26 : "ffff00", #highhat edge 46 : "ffff00", #highhat crown 55 : "00ff00", #crash edge 49 : "00ff00", #crash crown 48 : "0000ff", #left tom 45 : "ff00ff", #right tom 59 : "00ffff", #ride edge 51 : "00ffff", #ride crown 41 : "ff8000", #floor tom 36 : "ff0000" } #bass # run the event loop # Todo: ability to start/stop, or enable/disable the thread while self.midiActive: if self.midi.poll(): #read 1000 bytes from midi device events = self.midi.read(1000) for e in events: if e[0][2] != 64: #ignore note off packets #twitch_bot_utils.printer("%s:%s" % (e[0][1],e[0][2])) #debug, comment this out while playing, slows down the thread intensity = abs(e[0][2]) * 2 if intensity > 255: intensity = 255 if e[0][1] in drums: #twitch_bot_utils.printer(drums[e[0][1]]) #debug, comment this out while playing, slows down the thread value = drums[e[0][1]] rgb = twitch_bot_utils.hex2chr(value) #before = get_pixels() self.ser.write("#%c%c%c\xff!" % (rgb[0],rgb[1],rgb[2])) pygame.time.wait(50) #too slow, just write to black '''for x in range(0,30): writing_serial("#%c%c%c%c" % (before["%s"%x][0],before["%s"%x][1],before["%s"%x][2],x)) writing_serial("!")''' self.ser.write("#\x00\x00\x00\xff!") # wait 10ms - this is arbitrary, but wait(0) still resulted # in 100% cpu utilization pygame.time.wait(10) twitch_bot_utils.printer("Stopped Midi Thread!")
def __init__(self,u,p,h,d): try: self.cnx = mysql.connector.connect(user=u,password=p,host=h,database=d) twitch_bot_utils.printer("Connected to DB!") except mysql.connector.Error as err: twitch_bot_utils.printer(err)
def set_animating(status): global animating twitch_bot_utils.printer("Setting animating to: %s" % status) animating = status
def __init__(self,port,speed): #todo: add arduino detection here self.ser = serial.Serial(port, speed) self.writing = False twitch_bot_utils.printer("Opened Serial Port: %s Speed: %s" % (port,speed))
def user_commands(user,data): global user_stack parts = data.split() command = parts[0][1:] twitch_bot_utils.printer("Checking %s for user commands..." % data) #start commands if data.find ( 'test' ) != -1: irc.msg("test to you too!") return True if command == "!game" or data.find ( 'what game' ) != -1: irc.msg("The current game is: %s" % get_game()) return True if command == "!colors": irc.msg("The available colors are listed here: http://www.w3schools.com/html/html_colornames.asp" ) return True if animating == 1: twitch_bot_utils.printer("Busy, adding to stack: animating: %s" % (animating)) user_stack.append([user,data]) del user_stack[10:] temp = [] for stack in user_stack: temp.append(stack[1]) twitch_bot_utils.printer(string.join(temp," - ")) return else: twitch_bot_utils.printer("No animation currently, checking for animations") if data.find ( 'randomcolor' ) != -1: rgb = twitch_bot_utils.convertcolor("random",1) set_animating(1) irc.msg("RANDOMCOLOR!!!!!!!") color = converter.rgbToCIE1931(rgb[0], rgb[1], rgb[2]) twitch_bot_utils.printer("Colors: %d %d %d" % (rgb[0], rgb[1], rgb[2])) twitch_bot_utils.printer(color) sendToAll("{ \"bri\": 254, \"xy\":%s }" % color) user_wait(light_length) set_animating(0) modedefault() return True if data.find ( "fire" ) != -1: set_animating(1) irc.msg("FIRE!!!") color = converter.rgbToCIE1931(255,255,0) hueCommand("lights/1/state","{ \"bri\": 254, \"xy\":%s }" % color ,"PUT") color = converter.rgbToCIE1931(255,0,0) hueCommand("lights/2/state","{ \"bri\": 254, \"xy\":%s }" % color ,"PUT") color = converter.rgbToCIE1931(255,255,0) hueCommand("lights/3/state","{ \"bri\": 254, \"xy\":%s }" % color ,"PUT") sendToAll("{ \"alert\": \"lselect\" }") user_wait(light_length) set_animating(0) modedefault() return True if data.find ( "disco" ) != -1: if data.find ( "strobe" ) != -1: set_animating(1) irc.msg("DISCO SEIZURE PARTY!!!") color = converter.rgbToCIE1931(255,0,0) hueCommand("lights/1/state","{ \"bri\": 254, \"xy\":%s }" % color ,"PUT") color = converter.rgbToCIE1931(0,255,0) hueCommand("lights/2/state","{ \"bri\": 254, \"xy\":%s }" % color ,"PUT") color = converter.rgbToCIE1931(0,0,255) hueCommand("lights/3/state","{ \"bri\": 254, \"xy\":%s }" % color ,"PUT") sendToAll("{ \"alert\": \"lselect\" }") user_wait(light_length) set_animating(0) modedefault() return True set_animating(1) irc.msg("DISCO PARTY!!!") sendToAll("{ \"effect\": \"colorloop\" }") user_wait(light_length) set_animating(0) modedefault() return True if data.find ( "strobe" ) != -1: set_animating(1) irc.msg("SEIZURE PARTY!!!") sendToAll("{ \"alert\": \"lselect\" }") user_wait(light_length) set_animating(0) modedefault() return True if data.find( "spoopy" ) != -1: set_animating(1) irc.msg("2SPOOPY4EVERYONE!!!") sendToAll("{ \"on\": false }") user_wait(light_length) set_animating(0) modedefault() return True if data.find ( "police" ) != -1: set_animating(1) irc.msg("CHEESE IT!!!") color = converter.rgbToCIE1931(0,0,255) hueCommand("lights/1/state","{ \"bri\": 254, \"xy\":%s }" % color ,"PUT") color = converter.rgbToCIE1931(255,0,0) hueCommand("lights/2/state","{ \"bri\": 254, \"xy\":%s }" % color ,"PUT") color = converter.rgbToCIE1931(0,0,255) hueCommand("lights/3/state","{ \"bri\": 254, \"xy\":%s }" % color ,"PUT") sendToAll("{ \"alert\": \"lselect\" }") user_wait(light_length) set_animating(0) modedefault() return True #for key, value in twitch_bot_colors.colors.iteritems(): for k in sorted(twitch_bot_colors.colors, key=len, reverse=True): if data.find ( k.lower() ) != -1: value = twitch_bot_colors.colors[k] set_animating(1) twitch_bot_utils.printer("key: %s value: %s : %s,%s,%s" % (k,value,int("0x"+value[0:2],0),int("0x"+value[2:4],0),int("0x"+value[4:6],0))) irc.msg("%s!!!" % k.upper()) color = converter.rgbToCIE1931(int("0x"+value[0:2],0), int("0x"+value[2:4],0), int("0x"+value[4:6],0)) twitch_bot_utils.printer("Colors: %d %d %d" % (int("0x"+value[0:2],0), int("0x"+value[2:4],0), int("0x"+value[4:6],0))) twitch_bot_utils.printer(color) sendToAll("{ \"bri\": 254, \"xy\":%s }" % color) user_wait(light_length) set_animating(0) modedefault() return True
sendToAll("{ \"bri\": 254, \"xy\":%s }" % color) user_wait(light_length) set_animating(0) modedefault() return True #constants auth = twitch_auth.auth() auth.add_admin("spiffbot") master = auth.get_streamer() user_stack = [] set_animating(0) stayAlive = 1 converter = Converter() light_length = 15 irc = twitch_bot_utils.irc_connection("irc.twitch.tv","6667",auth.get_bot(),auth.get_oauth(), auth.get_streamer(),[admin_commands,user_commands]) twitch_bot_utils.printer("READY!") irc.msg("READY!") user_stack_thread = threading.Thread(target=user_stack_consumer) user_stack_thread.daemon = True user_stack_thread.start() #Main loop while stayAlive: time.sleep(1)
def set_top(hwnd): win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,0,0,width,height,win32con.SWP_NOACTIVATE) twitch_bot_utils.printer("SetWindowPos to HWND_TOPMOST and SWP_NOACTIVATE") style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X before" % style) style = style | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_TOPMOST twitch_bot_utils.printer("Setting style %X" % style) win32api.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, style) twitch_bot_utils.printer("Set style %X" % style) style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X after" % style) pygame.init() twitch_bot_utils.printer("Flicker scare! %s times" % times) pygame.display.set_mode((width, height), pygame.NOFRAME , 32) twitch_bot_utils.printer("Looking for window!") while True: time.sleep(0.001) try: hwnd = win32gui.FindWindow(None,"pygame window") if hwnd: twitch_bot_utils.printer("Found window! hwnd: %s" % hwnd) set_top(hwnd) break except win32gui.error: twitch_bot_utils.printer("Error: window not found") for i in range(0,times): twitch_bot_utils.printer("Flicker off")
win32api.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, style) twitch_bot_utils.printer("Set style %X" % style) style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X after" % style) def user_wait(duration): stop = time.time() + duration while time.time() < stop: time.sleep(0.5) pygame.display.set_mode((width, height), pygame.NOFRAME, 32) return pygame.init() twitch_bot_utils.printer("Dim scare! %s seconds" % times) pygame.display.set_mode((width, height), pygame.NOFRAME, 32) twitch_bot_utils.printer("Looking for window!") while True: time.sleep(0.001) try: hwnd = win32gui.FindWindow(None, "pygame window") if hwnd: twitch_bot_utils.printer("Found window! hwnd: %s" % hwnd) set_top(hwnd) break except win32gui.error: twitch_bot_utils.printer("Error: window not found") win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0, 0, 0), 220,
style = style | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_TOPMOST twitch_bot_utils.printer("Setting style %X" % style) win32api.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, style) twitch_bot_utils.printer("Set style %X" % style) style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X after" % style) def user_wait(duration): stop = time.time()+duration while time.time() < stop: time.sleep(0.5) pygame.display.set_mode((width, height), pygame.NOFRAME , 32) return pygame.init() twitch_bot_utils.printer("Dim scare! %s seconds" % times) pygame.display.set_mode((width, height), pygame.NOFRAME , 32) twitch_bot_utils.printer("Looking for window!") while True: time.sleep(0.001) try: hwnd = win32gui.FindWindow(None,"pygame window") if hwnd: twitch_bot_utils.printer("Found window! hwnd: %s" % hwnd) set_top(hwnd) break except win32gui.error: twitch_bot_utils.printer("Error: window not found") win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0,0,0), 220, win32con.LWA_ALPHA)
def hueCommand(url, command, method): payload = {'token':ACCESSTOKEN}; msg = 'clipmessage={ bridgeId: "'+BRIDGEID+'", clipCommand: { url: "/api/0/'+url+'", method: "'+method+'", body: '+command+' } }' r = requests.post(API_ADDRESS_CONTROL, params=payload,headers=headers,data=msg); twitch_bot_utils.printer("Command: %s Result: %s" % (command,r)) return r.text
def set_top(hwnd): win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, width, height, win32con.SWP_NOACTIVATE) twitch_bot_utils.printer("SetWindowPos to HWND_TOPMOST and SWP_NOACTIVATE") style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X before" % style) style = style | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_TOPMOST twitch_bot_utils.printer("Setting style %X" % style) win32api.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, style) twitch_bot_utils.printer("Set style %X" % style) style = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) twitch_bot_utils.printer("Got style %X after" % style) pygame.init() twitch_bot_utils.printer("Flicker scare! %s times" % times) pygame.display.set_mode((width, height), pygame.NOFRAME, 32) twitch_bot_utils.printer("Looking for window!") while True: time.sleep(0.001) try: hwnd = win32gui.FindWindow(None, "pygame window") if hwnd: twitch_bot_utils.printer("Found window! hwnd: %s" % hwnd) set_top(hwnd) break except win32gui.error: twitch_bot_utils.printer("Error: window not found") for i in range(0, times): twitch_bot_utils.printer("Flicker off")