Пример #1
0
def ws1_on_close(ws):  #get's called when the websocket connection was closed
    global pingTwitch, SUBdidWork
    print("### ws1 closed ###")
    pingTwitch = 0
    SUBdidWork = 0
    LED_Blue.off()

    #log file
    logOutput = open(loggies, "a")
    logOutput.write(datetime.utcnow().strftime('%Y,%m,%d,%H:%M:%S:%f'))
    logOutput.write(" ws1 closed\n")
    logOutput.close()
Пример #2
0
def pingTwitchServersToKeepTheConnectionAliveTask(
):  #This PINGs the server every 4 minutes as per twitch api docs
    while True:
        if pingTwitch:
            print("Pinging Twitch")
            ws1.send(json.dumps({"type": "PING"}))
            LED_Blue.off()
            pingstarttime = time.time(
            )  #we could later do something with this time but we don't have to
            time.sleep(10)  #wait 10 sec for ping response
            if not pingstarttime:  #is pingstarttime was not reset, close the connection
                ws.close()
            time.sleep(
                240 + random.randrange(-10, 10)
            )  #Sleep 4 min +/- 10sec (random is required by twitch api)
Пример #3
0
def ws1_on_error(
        ws, error):  #get's called when there was a websocket connection error
    global pingTwitch, SUBdidWork
    print(error)
    pingTwitch = 0
    SUBdidWork = 0
    LED_Blue.blink()

    #log file
    logOutput = open(loggies, "a")
    logOutput.write(datetime.utcnow().strftime('%Y,%m,%d,%H:%M:%S:%f'))
    logOutput.write(" ws1 error: ")
    logOutput.write(error)
    logOutput.write("\n")
    logOutput.close()
Пример #4
0
def ws1_on_close(ws):
    global pingTwitch, SUBdidWork
    
    #flags scrubbed
    pingTwitch = 0
    SUBdidWork = 0
    
    #front panel
    LED_Blue.off()
    
    #console
    consoleClass.thread2 = "closing websocket"
    
    #log file
    logOutput = open(loggies, "a")
    logOutput.write(datetime.utcnow().strftime('%Y,%m,%d,%H:%M:%S:%f'))
    logOutput.write(" ws1 closed\n")
    logOutput.close()
Пример #5
0
def pingTwitchServersToKeepTheConnectionAliveTask(): 
    global pingstarttime
    
    while True:
        #check flag
        if pingTwitch:
            #console
            consoleClass.thread2 = "Pinging Twitch"
            
            #ping twitch
            ws1.send(json.dumps({"type": "PING"}))
            pingstarttime = time.time() #we could later do something with this time but we don't have to
            
            #front panel
            LED_Blue.off()
            
            countdown = 10
            
            #wait for ping verbose
            while countdown > 0:
                #console
                consoleClass.thread2 = "Pinging Twitch, waiting " + str(countdown) + "s for response"

                #new timer
                time.sleep(1)
                countdown -= 1

            #if pingstarttime was not reset, close the connection
            if pingstarttime: 
                consoleClass.thread2 = "start time was not reset?"
                ws.close()
                
            #this is for the console readout + ping timer
            countdown = 240 + random.randrange(-10, 10)
            
            while countdown >= 0:
                #might want to make mm:ss time format here
                
                #console
                consoleClass.thread2 = "Next Ping in " + str(countdown) + "s"
                
                #new timer
                time.sleep(1)
                countdown -= 1
Пример #6
0
def ws1_on_error(ws, error): 
    global pingTwitch, SUBdidWork
    
    #flags are no bueno
    pingTwitch = 0
    SUBdidWork = 0
    
    #front panel
    LED_Blue.blink()
    
    #console
    consoleClass.thread2 = "WEBSOCKET ERROR"
    
    #log file
    logOutput = open(loggies, "a")
    logOutput.write(datetime.utcnow().strftime('%Y,%m,%d,%H:%M:%S:%f'))
    logOutput.write(" ws1 error: ")
    logOutput.write(error)
    logOutput.write("\n")
    logOutput.close()
Пример #7
0
def ws1_on_message(ws, message):
    jsonReturn = json.loads(message)

    #log file
    logOutput = open(loggies, "a")
    logOutput.write(datetime.utcnow().strftime('%Y,%m,%d,%H:%M:%S:%f'))
    logOutput.write(" raw message: ")
    logOutput.write(message)
    logOutput.write("\n")
    logOutput.close()

    if "type" in jsonReturn:
        if jsonReturn["type"] == "PONG":  #Take care of pong responses
            pingstarttime = 0
            print("PONG received")
            LED_Blue.on()
        elif jsonReturn[
                "type"] == "RECONNECT":  #Close if twitch tells us so and reconnect
            print(jsonReturn)
            try:
                ws.close()
            except:
                pass
        elif jsonReturn[
                "type"] == "RESPONSE":  #We get this as a response to our subToTopic request
            if jsonReturn["nonce"] == "e4t5v345nz3sm" and jsonReturn[
                    "error"] == "":  #validate this is the right response and there was no error
                print("socket sub successful")
                SUBdidWork = 1
            else:  #If there was something wrong
                print(jsonReturn)
        elif jsonReturn[
                "type"] == "MESSAGE":  #This is the message you get when an event itself happens
            #print(jsonReturn["data"]["message"])
            makeEntry(json.loads(jsonReturn["data"]["message"]))
        else:
            print(
                jsonReturn
            )  #if there is anything else, just print it(shouldn't be the case)
Пример #8
0
def ws1_on_message(ws, message):
    global pingstarttime
    
    jsonReturn = json.loads(message)
    
    #log file
    logOutput = open(loggies, "a")
    logOutput.write(datetime.utcnow().strftime('%Y,%m,%d,%H:%M:%S:%f'))
    logOutput.write(" raw message: ")
    logOutput.write(message)
    logOutput.write("\n")
    logOutput.close()
    
    if "type" in jsonReturn:
        #Take care of pong responses
        if jsonReturn["type"] == "PONG": 
            #reset flag
            pingstarttime = 0
            
            #console
            consoleClass.thread2 = "Pinging Twitch, pong'd"
            
            #front panel
            LED_Blue.on()
        
        #Close if twitch tells us so and reconnect        
        elif jsonReturn["type"] == "RECONNECT":            
            #console
            consoleClass.thread2 = "Reconnect!"
            
            try:
                ws.close()
            except:
                pass
       
        #We get this as a response to our subToTopic request
        elif jsonReturn["type"] == "RESPONSE": 
        
            #validate this is the right response and there was no error
            if jsonReturn["nonce"] == "e4t5v345nz3sm" and jsonReturn["error"] == "": 
                #console
                consoleClass.thread2 = "good nonce"
                
                SUBdidWork = 1
                
            #If there was something wrong    
            else:
                #console
                consoleClass.thread2 = "JSON response error!?"
                
        #This is the message you get when an event itself happens (a sub event) 
        elif jsonReturn["type"] == "MESSAGE": 
            #print(jsonReturn["data"]["message"])
            makeEntry(json.loads(jsonReturn["data"]["message"]))
            #for borts, this is f**k
            #poops = json.loads(jsonReturn["data"]["message"])
            #makeEntry(poops["data"])
            
        else:
            #if there is anything else, shouldn't be
            consoleClass.thread2 = "JSON message error!?"