Esempio n. 1
0
    def validate_pick(cls, data):

        week = GameModel.get_max_week()
        GameController.update_games()

        game = GameModel.find_by_game_id(data['game_id'])
        nfl_team = NFLTeamModel.find_by_team_name(data['nfl_team_name'])

        if game is None:
            return {'message': 'Cannot find game with that id.'}, 401

        if nfl_team is None:
            return {'message': 'Cannot find nfl_team with that name'}, 401

        if game.has_started:
            return {
                'message':
                'This game has already started. Please try a different game.'
            }, 403

        if data['nfl_team_name'] not in [
                game.home_team_name, game.away_team_name
        ]:
            return {
                'message':
                'Team for selected pick is not a part of game with id {}.'.
                format(data['game_id'])
            }, 403

        if (PickModel.is_duplicate_team_pick(data['team_id'],
                                             data['nfl_team_name'])):
            return {'message': "You've already chosen this team."}, 401

        pick = PickModel.find_pick_by_week_and_team_id(week, data['team_id'])

        if pick is None:
            pick = PickModel(data['team_id'], data['game_id'], week,
                             data['nfl_team_name'])
        else:
            if pick.game.has_started:
                return {
                    'message':
                    'Current pick game already started. Cannot choose new team.'
                }, 401

            pick.game_id = data['game_id']
            pick.nfl_team_name = data['nfl_team_name']

        try:
            pick.upsert()
        except:
            return {'message': 'error upserting pick.'}, 500

        return {'pick': pick.json()}
Esempio n. 2
0
    def update_games(cls):
        rss_feed = requests.get(cls.nfl_endpoint)
        StadiumController.upsert_stadiums()

        json_data = rss_feed.json()

        week_info = json_data['week']
        week_num = week_info['number']

        events = json_data['events']

        for event in events:
            competitions = event['competitions']
            competition = competitions[0]
            teams = competition['competitors']

            home_team = next(
                filter(lambda team: team['homeAway'] == 'home', teams))
            away_team = next(
                filter(lambda team: team['homeAway'] == 'away', teams))
            status = event['status']
            type = status['type']
            has_started = not type['state'] == 'pre'

            game_id = event['id']

            game_model = GameModel.find_by_game_id(game_id)

            if game_model is None:

                if 'name' not in home_team['team']:
                    home_team_name = home_team['team']['displayName']
                else:
                    home_team_name = home_team['team']['name']

                if 'name' not in away_team['team']:
                    away_team_name = away_team['team']['displayName']
                else:
                    away_team_name = away_team['team']['name']

                home_team_score = home_team['score'] or 0
                away_team_score = away_team['score'] or 0

                if status['period'] == 0 and type['state'] == 'pre':
                    quarter = 'P'
                elif type['state'] == 'post':
                    quarter = 'F'
                else:
                    quarter = status['period']

                quarter_time = status['displayClock']

                game_date = parser.parse(competition['startDate'])
                game_date_eastern = game_date - timedelta(hours=5)
                day_of_week = calendar.day_name[game_date_eastern.weekday()]
                site_id = competition['venue']['id']

                game_model = GameModel(game_id, home_team_name,
                                       home_team_score, away_team_name,
                                       away_team_score, day_of_week, game_date,
                                       quarter, quarter_time, site_id,
                                       week_num, has_started)

                game_model.upsert()
            else:
                game_model.home_team_score = home_team['score'] or 0
                game_model.away_team_score = away_team['score'] or 0

                if status['period'] == 0 and type['state'] == 'pre':
                    quarter = 'P'
                elif type['state'] == 'post':
                    quarter = 'F'
                else:
                    quarter = status['period']

                game_model.quarter = quarter
                game_model.quarter_time = status['displayClock']
                game_model.has_started = has_started
                game_date = parser.parse(competition['startDate'])

                game_date_eastern = game_date - timedelta(hours=5)
                day_of_week = calendar.day_name[game_date_eastern.weekday()]

                game_model.game_date = game_date
                game_model.day_of_week = day_of_week
                game_model.site_id = competition['venue']['id']

                game_model.upsert()

            if 'odds' in competition:
                odds = competition['odds']

                if odds:
                    odds_model = OddsModel.find_by_game_id(game_id)

                    if odds_model is None:
                        if 'details' in odds[0] and 'overUnder' in odds[0]:
                            odds_model = OddsModel(game_id, odds[0]['details'],
                                                   odds[0]['overUnder'])
                            odds_model.upsert()
                    else:
                        odds_model.details = odds[0]['details']
                        odds_model.over_under = odds[0]['overUnder']
                        odds_model.upsert()

                if odds_model:
                    game_model.odds_id = odds_model.odds_id

        return_games = GameModel.get_games_by_week(week_num)
        return {'games': [game.json() for game in return_games]}
Esempio n. 3
0
File: game.py Progetto: aberardi/API
    def update_games(cls):
        rss_feed = requests.get(cls.nfl_endpoint)

        json_data = rss_feed.json()

        week_num = json_data['week']
        games = json_data['gameScores']

        default_score_info = {
            'homeTeamScore': {
                'pointTotal': 0
            },
            'visitorTeamScore': {
                'pointTotal': 0
            },
            'phase': 'PREGAME',
            'time': '15:00'
        }

        for game in games:
            schedule_info = game['gameSchedule']
            score_info = game['score'] if game['score'] else default_score_info
            home_score_info = score_info['homeTeamScore']
            away_score_info = score_info['visitorTeamScore']
            home_team_info = schedule_info['homeTeam']
            away_team_info = schedule_info['visitorTeam']
            site_info = schedule_info['site']
            game_model = GameModel.find_by_game_id(schedule_info['gameKey'])

            if game_model is None:
                game_id = schedule_info['gameKey']
                home_team_name = home_team_info['nick']
                home_team_score = home_score_info['pointTotal'] or 0
                away_team_name = away_team_info['nick']
                away_team_score = away_score_info['pointTotal'] or 0

                quarter = score_info['phase'][0] if len(
                    score_info['phase']) > 3 else score_info['phase']

                quarter_time = score_info['time']

                game_time_24hr = time.strptime(
                    schedule_info['gameTimeEastern'], "%H:%M:%S")
                game_time = time.strftime("%I:%M %p", game_time_24hr)
                game_time = time.strptime(game_time, "%I:%M %p")

                game_date = datetime.strptime(
                    schedule_info['gameDate'] + ' ' +
                    time.strftime('%I:%M %p', game_time), '%m/%d/%Y %I:%M %p')

                day_of_week = calendar.day_name[game_date.weekday()]
                site_id = site_info['siteId']
                game_model = GameModel(game_id, home_team_name,
                                       home_team_score, away_team_name,
                                       away_team_score, day_of_week, game_date,
                                       quarter, quarter_time, site_id,
                                       week_num)
                game_model.upsert()
            else:
                game_model.home_team_score = home_score_info['pointTotal'] or 0
                game_model.away_team_score = away_score_info['pointTotal'] or 0
                game_model.quarter = score_info['phase'][0] if len(
                    score_info['phase']) > 3 else score_info['phase']
                game_model.quarter_time = score_info['time']
                game_time_24hr = time.strptime(
                    schedule_info['gameTimeEastern'], "%H:%M:%S")
                game_time = time.strftime("%I:%M %p", game_time_24hr)
                game_time = time.strptime(game_time, "%I:%M %p")

                game_model.game_date = datetime.strptime(
                    schedule_info['gameDate'] + ' ' +
                    time.strftime('%I:%M %p', game_time), '%m/%d/%Y %I:%M %p')
                game_model.day_of_week = calendar.day_name[
                    game_model.game_date.weekday()]
                game_model.site_id = site_info['siteId']
                game_model.upsert()

        return_games = GameModel.get_games_by_week(week_num)
        return {'games': [game.json() for game in return_games]}