The api output bounces alot

i am trying to make some python code detect the start and end of a stream. here is what i got so far:

def streamStart():
    print("stream Start")

def streamStop():
    print("stream Stop")

def streamCheck():
    global status,info
    while True:
        info = json.loads(urlopen("https://api.twitch.tv/kraken/streams/%s?client_id=<my id>"%channel, timeout = 15).read().decode('utf-8'))
        if info.get("stream") == None:
            if status == True:
                streamStop()
            status = False
        else:
            if status == False:
                streamStart()
            status = True
status = False
streamCheck()

the problem im having is that when i do start streaming it takes it about two minutes to detect it (including delay) and when it does detect it i get this output:

stream Start
stream Stop
stream Start

and when i stop streaming i get the same type of bounce but this time 11 times and 5 minutes after i stopped:

stream Stop
stream Start
stream Stop
stream Start
stream Stop
stream Start
stream Stop
stream Start
stream Stop
stream Start
stream Stop
stream Start
stream Stop
stream Start
stream Stop
stream Start
stream Stop
stream Start
stream Stop
stream Start
stream Stop

this is really ennoying and i dont want to deal with it. i can confirm that this is not a bug in my code as if i refresh my browser with the api link i can also see it changing from refresh to refresh.
im wondering if anybody else has this problem and if there is anything i can do to fix it.

API endpoints are cached and you are probably getting different versions of the cache. You just can’t expect updates in realtime from it, so hitting more often than every minute or so might not make too much sense.

Hopefully at some point there will be a documented PubSub topic for these kinds of things.

i thought it had something to do with cacheing but i didnt know it could cache 2 different versions. thx for the response

Just a note from another python user: please use snake_case for functions as of PEP 8

Good luck and have fun streaming!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.