def game_ratio(game):
    global tweetmode
    try:
        r = requests.get("https://api.twitch.tv/kraken/streams?game=" + game)
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        print "uh oh caught exception when connecting. try again. see game_ratio(game)."
        return game_ratio(game)
    if not r:
        return game_ratio(game)
    while r.status_code != 200:
        print r.status_code, ", service unavailable"
        r = requests.get("https://api.twitch.tv/kraken/streams?game=" + game)
    try:
        gamedata = r.json()
    except ValueError:
        print "could not decode json. recursing"
        return game_ratio(game)
    # TODO make a dictionary with keys as the game titles and values as the average and count
    count = 0  # number of games checked
    avg = 0
    while "streams" not in gamedata.keys():
        r = requests.get("https://api.twitch.tv/kraken/streams?game=" + game)
        while r.status_code != 200:
            print r.status_code, ", service unavailable"
            r = requests.get("https://api.twitch.tv/kraken/streams?game=" + game)
        try:
            gamedata = r.json()
        except ValueError:
            print "couldn't json; recursing"
            return game_ratio(game)
    if len(gamedata["streams"]) > 0:
        for i in range(0, len(gamedata["streams"])):
            viewers = gamedata["streams"][i]["viewers"]
            if viewers < user_threshold:
                break

            user = gamedata["streams"][i]["channel"]["name"].lower()
            name = "http://www.twitch.tv/" + user

            ratio = -1
            ratio = user_ratio(user)
            if ratio == 0:
                print "ratio is 0... abort program?"
            handle_twitter.send_tweet(user, ratio, game, viewers, tweetmode, ratio_threshold)
            avg += ratio
            count += 1
    else:
        print "couldn't find " + game + " :("
        return 0
    if count != 0:
        avg /= count
    # for the game specified, go through all users more than <user_threshold> viewers, find ratio, average them
    return avg
def game_ratio(game):
    global tweetmode
    try:
        r = requests.get('https://api.twitch.tv/kraken/streams?game=' + game, headers={"Client-ID": CLIENT_ID})
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        print "uh oh caught exception when connecting. try again. see game_ratio(game)."
        time.sleep(5)
        return game_ratio(game)
    if not r:
        time.sleep(5)
        return game_ratio(game)
    while r.status_code != 200:
        print r.status_code, ", service unavailable"
        r = requests.get('https://api.twitch.tv/kraken/streams?game=' + game, headers={"Client-ID": CLIENT_ID})
    try:
        gamedata = r.json()
    except ValueError:
        print "could not decode json. recursing"
        time.sleep(5)
        return game_ratio(game)
#TODO make a dictionary with keys as the game titles and values as the average and count
    count = 0 # number of games checked
    avg = 0
    while 'streams' not in gamedata.keys():
        r = requests.get('https://api.twitch.tv/kraken/streams?game=' + game, headers={"Client-ID": CLIENT_ID})
        while r.status_code != 200:
            print r.status_code, ", service unavailable"
            r = requests.get('https://api.twitch.tv/kraken/streams?game=' + game, headers={"Client-ID": CLIENT_ID})
            time.sleep(1)
        try:
            gamedata = r.json()
        except ValueError:
            print "couldn't json; recursing"
            continue
    if len(gamedata['streams']) > 0:
        for i in range(0, len(gamedata['streams'])):
            viewers =  gamedata['streams'][i]['viewers']
            if viewers < user_threshold:
                break

            user = gamedata['streams'][i]['channel']['name'].lower() 
            name = "http://www.twitch.tv/" + user

            ratio = -1
            ratio = user_ratio(user)
            if ratio == 0:
                print "ratio is 0... abort program?"
            handle_twitter.send_tweet(user, ratio, game, viewers, tweetmode, 
                                      ratio_threshold, confirmed, suspicious)
            avg += ratio
            count += 1
            time.sleep(1) #don't spam servers
    else:
        print "couldn't find " + game + " :("
        return 0
    global global_sum
    global global_cnt
    global_sum += avg
    global_cnt += count
    if count != 0:
        avg /= count
    # for the game specified, go through all users more than <user_threshold> viewers, find ratio, average them
    return avg
Esempio n. 3
0
def game_ratio(game):
    """
    Returns the average chatter:viewer ratio for a certain game
    :param game: string (game to search)
    :return:
    """
    global tweetmode
    try:
        r = requests.get('https://api.twitch.tv/kraken/streams?game=' + game, headers={"Client-ID": CLIENT_ID, "Accept": "application/vnd.twitchtv.v5+json"})
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        print "uh oh caught exception when connecting. try again. see game_ratio(game)."
        time.sleep(5)
        return game_ratio(game)
    if not r:
        time.sleep(5)
        return game_ratio(game)
    while r.status_code != 200:
        print r.status_code, ", service unavailable"
        r = requests.get('https://api.twitch.tv/kraken/streams?game=' + game, headers={"Client-ID": CLIENT_ID, "Accept": "application/vnd.twitchtv.v5+json"})
    try:
        gamedata = r.json()
    except ValueError:
        print "could not decode json. recursing"
        time.sleep(5)
        return game_ratio(game)
    # TODO make a dictionary with keys as the game titles and values as the average and count
    count = 0  # Number of games checked
    avg = 0
    while 'streams' not in gamedata.keys():
        r = requests.get('https://api.twitch.tv/kraken/streams?game=' + game, headers={"Client-ID": CLIENT_ID, "Accept": "application/vnd.twitchtv.v5+json"})
        while r.status_code != 200:
            print r.status_code, ", service unavailable"
            r = requests.get('https://api.twitch.tv/kraken/streams?game=' + game, headers={"Client-ID": CLIENT_ID, "Accept": "application/vnd.twitchtv.v5+json"})
            time.sleep(1)
        try:
            gamedata = r.json()
        except ValueError:
            print "couldn't json; recursing"
            continue
    if len(gamedata['streams']) > 0:
        for i in range(0, len(gamedata['streams'])):
            viewers = gamedata['streams'][i]['viewers']
            if viewers < user_threshold:
                break

            user = gamedata['streams'][i]['channel']['name'].lower()

            ratio = user_ratio(user)
            if ratio == 0:
                print "ratio is 0... abort program?"
            handle_twitter.send_tweet(user, ratio, game, viewers, tweetmode,
                                      ratio_threshold, confirmed, suspicious)
            avg += ratio
            count += 1
            time.sleep(1)  # don't spam servers
    else:
        print "couldn't find " + game + " :("
        return 0
    global global_sum
    global global_cnt
    global_sum += avg
    global_cnt += count
    if count != 0:
        avg /= count
    # For the game specified, go through all users more than <user_threshold> viewers, find ratio, average them.
    return avg