Beispiel #1
0
def live_h2h(team1, team2, update=True, save_file=None):
    """
    A convenience method that updates data then displays h2h for most recent game between specified tams.

    :param team1: str or int, team
    :param team2: str or int, other team
    :param update: bool, should data be updated first?
    :param save_file: str, specify a valid filepath to save to file. If None, merely shows on screen.

    :return: nothing
    """
    if update:
        from scrapenhl2.scrape import autoupdate
        autoupdate.autoupdate()
    from scrapenhl2.scrape import games
    game = games.most_recent_game_id(team1, team2)
    return game_h2h(2017, game, save_file)
def add_players_to_file(filename,
                        focus_team,
                        season=None,
                        gamecol='Game',
                        periodcol='Period',
                        timecol='Time',
                        time_format='elapsed',
                        update_data=False,
                        player_output='names'):
    """
    Adds names of on-ice players to the end of each line, and writes to file in the same folder as input file.
    Specifically, adds 1 second to the time in the spreadsheet and adds players who were on the ice at that time.

    You cannot necessarily trust results when times coincide with stoppages--and it's worth checking faceoffs as well.

    :param filename: str, the file to read. Will save output as this filename but ending in "on-ice.csv"
    :param focus_team: str or int, e.g. 'WSH' or 'WPG'
    :param season: int. For 2007-08, use 2007. Defaults to current season.
    :param gamecol: str. The column holding game IDs (e.g. 20001). By default, looks for column called "Game"
    :param periodcol: str. The column holding period number/name (1, 2, 3, 4 or OT, etc). By default: "Period"
    :param timecol: str. The column holding time in period in M:SS format.
    :param time_format: str, how to interpret timecol. Use 'elapsed' or 'remaining'.
        E.g. the start of a period is 0:00 with elapsed and 20:00 in remaining.
    :param update_data: bool. If True, will autoupdate() data for given season. If not, will not update game data.
        Use when file includes data from games not already scraped.
    :param player_output: str, use 'names' or 'nums'. Currently only supports 'names'

    :return: nothing
    """
    # TODO handle date instead of season and game

    if season is None:
        season = schedules.get_current_season()
    if update_data:
        autoupdate.autoupdate()

    df = _read_tracking_file(filename)
    df = add_times_to_file(df, periodcol, timecol, time_format)
    df = add_onice_players_to_df(df, focus_team, season, gamecol,
                                 player_output)
    _write_tracking_file(df, filename)
Beispiel #3
0
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
from scrapenhl2.scrape import autoupdate

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--season", type=int, default=None)
    arguments = parser.parse_args()

    if arguments.season is not None and 2017 < arguments.season < 2005:
        print("Invalid season")

    autoupdate.autoupdate(season=arguments.season)
Beispiel #4
0
    def on_success(self, data):
        if 'text' in data:
            print(data['text'])

            if r'https://t.co/' in data['text']:
                print('This looks like an image')
                return
            if data['text'][:3] == 'RT ':
                print('This looks like a retweet')
                return

            global LAST_UPDATE, SCRAPED_NEW
            try:
                if player_cf_graphs(data):
                    return

                try:
                    season, gameid = games.find_playoff_game(data['text'])
                except ValueError:
                    season = None
                    gameid = None

                # Get season with a 4-digit regex
                if season is None:
                    text = data['text'] + ' '
                    if re.search(r'\s\d{4}\s', text) is not None:
                        season = int(re.search(r'\s\d{4}\s', text).group(0))
                        if season < 2015 or season > schedules.get_current_season():
                            tweet_error("Sorry, I don't have data for this season yet", data)
                            print('Invalid season')
                            return
                    else:
                        season = schedules.get_current_season()

                # Get game with a 5-digit regex
                if gameid is None:
                    if re.search(r'\s\d{5}\s', text) is not None:
                        gameid = int(re.search(r'\s\d{5}\s', text).group(0))
                        if not schedules.check_valid_game(season, gameid):
                            tweet_error("Sorry, this game ID doesn't look right", data)
                            print('Game ID not right')
                            return
                    else:
                        pass

                if gameid is None:
                    # Get team names
                    parts = data['text'].replace('@h2hbot', '').strip().split(' ')
                    teams = []
                    for part in parts:
                        if re.match(r'[A-z]{3}', part.strip()):
                            part = part.upper()
                            if team_info.team_as_id(part) is not None:
                                teams.append(part)
                    if len(teams) == 0:
                        print('Think this was a tagged discussion')
                        return
                    elif len(teams) != 2:
                        tweet_error("Sorry, I need 2 teams. Found {0:d}. Make sure abbreviations are correct"
                                    .format(len(teams)), data)
                        return

                    team1, team2 = teams[:2]
                    gameid = games.most_recent_game_id(team1, team2)

                h2hfile = 'bot/{0:d}0{1:d}h2h.png'.format(season, gameid)
                tlfile = 'bot/{0:d}0{1:d}tl.png'.format(season, gameid)

                oldstatus = schedules.get_game_status(season, gameid)

                # Scrape only if:
                # Game is in current season AND
                # Game is today, and my schedule says it's "scheduled", OR
                # Game is today, and my schedule doesn't say it's final yet, and it's been at least
                #   5 min since last scrape, OR
                # Game was before today and my schedule doesn't say "final"
                # Update in these cases
                scrapeagain = False
                if season == schedules.get_current_season():
                    today = datetime.datetime.now().strftime('%Y-%m-%d')
                    gdata = schedules.get_game_data_from_schedule(season, gameid)
                    if gdata['Date'] == today:
                        if gdata['Status'] == 'Scheduled':
                            scrapeagain = True
                        elif gdata['Status'] != 'Final' and \
                                (LAST_UPDATE is None or time.time() - LAST_UPDATE >= 60 * 5):
                            scrapeagain = True
                    elif gdata['Date'] < today and gdata['Status'] != 'Final':
                        scrapeagain = True
                if scrapeagain:
                    autoupdate.autoupdate(season, update_team_logs=False)
                    LAST_UPDATE = time.time()
                    SCRAPED_NEW = True

                hname = schedules.get_home_team(season, gameid)
                rname = schedules.get_road_team(season, gameid)
                status = schedules.get_game_status(season, gameid)

                if 'In Progress' in oldstatus or status != oldstatus or not os.path.exists(tlfile):
                    try:
                        game_timeline.game_timeline(season, gameid, save_file=tlfile)
                        game_h2h.game_h2h(season, gameid, save_file=h2hfile)
                        tweet_game_images(h2hfile, tlfile, hname, rname, status, data)
                        print('Success!')
                    except Exception as e:
                        print(data['text'], time.time(), e, e.args)
                        tweet_error("Sorry, there was an unknown error while making the charts (cc @muneebalamcu)",
                                    data)

            except Exception as e:
                print('Unexpected error')
                print(time.time(), data['text'], e, e.args)