<commit_before># Copyright 2015 jydo inc. All rights reserved.
import json
import time
from steam_alerts import logger
from steam_alerts.messaging_service import MessagingService
from steam_alerts.person import Person
from steam_alerts.steam_service import SteamService, status_types


def run_loop(config_path):
    with open(config_path) as file:
        config = json.load(file)

    steam_key = config['steam_key']
    twilio_sid = config['twilio_sid']
    twilio_auth_token = config['twilio_auth_token']
    twilio_number = config['twilio_number']
    messages = config['messages']
    players = {}

    messaging_service = MessagingService(twilio_sid, twilio_auth_token, twilio_number, messages)
    steam_service = SteamService(steam_key)

    for player_json in config['players']:
        if player_json['phone_number'] != "" and player_json['steam_id'] != "":
            player = Person(**player_json)
            logger.info("Adding {}".format(player.name))
            players[player.steam_id] = player

    logger.info('Starting run loop.')
    while True:
        statuses = steam_service.get_player_statuses(','.join(players.keys()))

        for status in statuses:
            player = players[status['steamid']]
            persona_state = status['personastate']

            logger.info('{} is "{}"'.format(player.name, status_types[persona_state]))

            if persona_state == 1:
                if 'gameextrainfo' in status:
                    game = status['gameextrainfo']
                    logger.info('{} is in a game: "{}"'.format(player.name, game))

                    if 'dota' in game.lower():
                        logger.info('{} is playing DOTA, annoy them!'.format(player.name))
                        messaging_service.send_message(player)
                else:
                    logger.info('{} is not in a game.'.format(player.name))

        time.sleep(60 * 10)<commit_msg>Add poll_rate and message_rate to run_loop.   - Only send a message if we haven't sent one longer than message_rate (in seconds)   - Poll steam APIs at the rate of message_rate (in seconds)   - Only log user status on persona state change or game state change.<commit_after># Copyright 2015 jydo inc. All rights reserved.
import json
import time
from steam_alerts import logger
from steam_alerts.messaging_service import MessagingService, MockMessagingService
from steam_alerts.person import Person
from steam_alerts.steam_service import SteamService, status_types


def run_loop(config_path):
    with open(config_path) as file:
        config = json.load(file)

    steam_key = config['steam_key']
    twilio_sid = config['twilio_sid']
    twilio_auth_token = config['twilio_auth_token']
    twilio_number = config['twilio_number']
    poll_rate = config.get('poll_rate', 60)
    message_rate = config.get('message_rate', 60 * 5)
    messages = config['messages']
    players = {}

    messaging_service = MessagingService(twilio_sid, twilio_auth_token, twilio_number, messages)
    steam_service = SteamService(steam_key)

    for player_json in config['players']:
        if player_json['phone_number'] != "" and player_json['steam_id'] != "":
            person = Person(**player_json)
            logger.info("Adding {}".format(person.name))
            players[person.steam_id] = person

    logger.info('Starting run loop.')
    while True:
        statuses = steam_service.get_player_statuses(','.join(players.keys()))

        for status in statuses:
            person = players[status['steamid']]
            persona_state = status['personastate']
            old_state = status_types.get(person.persona_state, None)
            new_state = status_types[persona_state]
            old_game = person.game
            new_game = status.get('gameextrainfo')
            person.persona_state = persona_state
            person.game = new_game
            msg = '{} is {}'.format(person.name, new_state)
            can_send = person.last_messaged is None or ((time.time() - person.last_messaged) >= message_rate)

            if new_game is not None:
                msg += ' and is playing "{}"'.format(new_game)
            else:
                msg += ' and not in a game'

            if old_state != new_state or new_game != old_game:
                logger.info(msg)

            if person.game is not None and can_send:
                if 'thomas' in person.game.lower():
                    messaging_service.send_message(person)
                    person.last_messaged = time.time()

        time.sleep(poll_rate)
{'fraction_non_alphanumeric': 0.10228270412642669, 'fraction_numerical': 0.003950834064969272, 'mean_word_length': 2.7260834014717905, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4520170', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1291, 'n_words': 320}