Пример #1
0
def week_schedule(year, stype, week):
    """
    Returns a list of dictionaries with information about each game in
    the week specified. The games are ordered by gsis_id. `year` should
    be an integer, `stype` should be one of the strings `PRE`, `REG` or
    `POST`, and `gsis_week` should be a value in the range `[1, 17]`.
    """
    url = schedule_url(year, stype, week)
    try:
        dom = xml.parse(urllib.urlopen(url))
    except urllib.HTTPError:
        print('Could not load %s' % url, file=sys.stderr)
        return []

    games = []
    for g in dom.getElementsByTagName("g"):
        gsis_id = g.getAttribute('eid')
        games.append({
            'eid': gsis_id,
            'wday': g.getAttribute('d'),
            'year': year,
            'month': int(gsis_id[4:6]),
            'day': int(gsis_id[6:8]),
            'time': g.getAttribute('t'),
            'season_type': stype,
            'week': week,
            'home': g.getAttribute('h'),
            'away': g.getAttribute('v'),
            'gamekey': g.getAttribute('gsis'),
        })
    return games
Пример #2
0
def _get_json_data(eid=None, fpath=None):
    """
    Returns the JSON data corresponding to the game represented by eid.

    If the JSON data is already on disk, it is read, decompressed and returned.

    Otherwise, the JSON data is downloaded from the NFL web site. If the data
    doesn't exist yet or there was an error, _get_json_data returns None.

    If eid is None, then the JSON data is read from the file at fpath.
    """
    assert eid is not None or fpath is not None

    if fpath is not None:
        with gzip.open(fpath) as handler:
            return force_text(handler.read())

    fpath = _jsonf % eid
    if os.access(fpath, os.R_OK):
        with gzip.open(fpath) as handler:
            return force_text(handler.read())

    try:
        response = urllib.urlopen(_json_base_url % (eid, eid),
                                  timeout=5).read()
        return force_text(response)
    except urllib.HTTPError:
        pass
    except socket.timeout:
        pass
    return None
Пример #3
0
def week_schedule(year, stype, week):
    """
    Returns a list of dictionaries with information about each game in
    the week specified. The games are ordered by gsis_id. `year` should
    be an integer, `stype` should be one of the strings `PRE`, `REG` or
    `POST`, and `gsis_week` should be a value in the range `[1, 17]`.
    """
    url = schedule_url(year, stype, week)
    try:
        dom = xml.parse(urllib.urlopen(url))
    except urllib.HTTPError:
        print('Could not load %s' % url, file=sys.stderr)
        return []

    games = []
    for g in dom.getElementsByTagName("g"):
        gsis_id = g.getAttribute('eid')
        games.append({
            'eid': gsis_id,
            'wday': g.getAttribute('d'),
            'year': year,
            'month': int(gsis_id[4:6]),
            'day': int(gsis_id[6:8]),
            'time': g.getAttribute('t'),
            'season_type': stype,
            'week': week,
            'home': g.getAttribute('h'),
            'away': g.getAttribute('v'),
            'gamekey': g.getAttribute('gsis'),
        })
    return games
Пример #4
0
def _update_week_number():
    global _cur_week, _cur_year, _cur_season_phase
    dom = xml.parse(urllib.urlopen(_CUR_SCHEDULE, timeout=5))
    gms = dom.getElementsByTagName('gms')[0]
    _cur_week = int(gms.getAttribute('w'))
    _cur_year = int(gms.getAttribute('y'))

    phase = gms.getAttribute('t').strip()
    if phase == 'P':
        _cur_season_phase = 'PRE'
    elif phase == 'POST' or phase == 'PRO':
        _cur_season_phase = 'POST'
        _cur_week -= 17
    else:
        _cur_season_phase = 'REG'
    return time.time()
Пример #5
0
def _update_week_number():
    global _cur_week, _cur_year, _cur_season_phase

    dom = xml.parse(urllib.urlopen(_CUR_SCHEDULE, timeout=5))
    gms = dom.getElementsByTagName('gms')[0]
    _cur_week = int(gms.getAttribute('w'))
    _cur_year = int(gms.getAttribute('y'))

    phase = gms.getAttribute('t').strip()
    if phase == 'P':
        _cur_season_phase = 'PRE'
    elif phase == 'POST' or phase == 'PRO':
        _cur_season_phase = 'POST'
        _cur_week -= 17
    else:
        _cur_season_phase = 'REG'
    return time.time()
Пример #6
0
def week_schedule(year, stype, week):
    """
    Returns a list of dictionaries with information about each game in
    the week specified. The games are ordered by gsis_id. `year` should
    be an integer, `stype` should be one of the strings `PRE`, `REG` or
    `POST`, and `gsis_week` should be a value in the range `[1, 17]`.
    """
    url = schedule_url(year, stype, week)
    try:
        dom = xml.parse(urllib.urlopen(url))
    except urllib.HTTPError:
        print('Could not load %s' % url, file=sys.stderr)
        return []

    games = []
    for g in dom.getElementsByTagName("g"):
        gsis_id = g.getAttribute('eid')
        games.append({
            'eid': gsis_id,
            'wday': g.getAttribute('d'),
            'year': year,
            'month': int(gsis_id[4:6]),
            'day': int(gsis_id[6:8]),
            'time': g.getAttribute('t'),
            'meridiem': None,
            'season_type': stype,
            'week': week,
            'home': g.getAttribute('h'),
            'away': g.getAttribute('v'),
            'gamekey': g.getAttribute('gsis'),
        })

    for game in games:
        h = int(game['time'].split(':')[0])
        m = int(game['time'].split(':')[1])
        if 0 < h <= 5:  # All games before "6:00" are PM until proven otherwise
            game['meridiem'] = 'PM'

        if game['meridiem'] is None:

            days_games = [g for g in games if g['wday'] == game['wday']]
            preceeding = [g for g in days_games if g['eid'] < game['eid']]
            proceeding = [g for g in days_games if g['eid'] > game['eid']]

            # If any games *after* this one are AM then so is this
            if any(g['meridiem'] == 'AM' for g in proceeding):
                game['meridiem'] = 'AM'
            # If any games *before* this one are PM then so is this one
            elif any(g['meridiem'] == 'PM' for g in preceeding):
                game['meridiem'] = 'PM'
            # If any games *after* this one have an "earlier" start it's AM
            elif any(h > t for t in
                     [int(g['time'].split(':')[0]) for g in proceeding]):
                game['meridiem'] = 'AM'
            # If any games *before* this one have a "later" start time it's PM
            elif any(h < t for t in
                     [int(g['time'].split(':')[0]) for g in preceeding]):
                game['meridiem'] = 'PM'

        if game['meridiem'] is None:
            if game['wday'] not in ['Sat', 'Sun']:
                game['meridiem'] = 'PM'
            if game['season_type'] == 'POST':
                game['meridiem'] = 'PM'

    return games