Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ikeyoda patch 1 #149

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions mlbgame/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,10 @@
'version=48')
IMPORTANT_DATES = ('http://lookup-service-prod.mlb.com/named.org_history.bam?'
'org_id=1&season={0}')
BCAST_INFO = ('http://mlb.mlb.com/lookup/json/named.mlb_broadcast_info.bam?'
'team_id={}&season={}')
INNINGS_URL = BASE_URL + 'gid_{3}/inning/inning_all.xml'
# Local Directory
PWD = os.path.join(os.path.dirname(__file__))


def get_broadcast_info(team_id, year):
try:
return urlopen(BCAST_INFO.format(team_id, year))
except HTTPError:
raise ValueError('Failed to retrieve MLB broadcast information.')


def get_important_dates(year):
try:
return urlopen(IMPORTANT_DATES.format(year))
Expand Down Expand Up @@ -81,22 +71,26 @@ def get_raw_box_score(game_id):
except HTTPError:
raise ValueError('Could not find a game with that id.')


def get_game_events(game_id):
"""Return the game events file of a game with matching id."""
def does_raw_box_score_exist(game_id) :
"""Checks if box score exists for game id"""
year, month, day = get_date_from_game_id(game_id)
try:
return urlopen(GAME_URL.format(year, month, day, game_id,
'game_events.xml'))
urlopen(GAME_URL.format(year, month, day, game_id,
'rawboxscore.xml'))
return True

except HTTPError:
raise ValueError('Could not find a game with that id.')
return False




def get_innings(game_id):
"""Return the innings file of a game with matching id."""
def get_game_events(game_id):
"""Return the game events file of a game with matching id."""
year, month, day = get_date_from_game_id(game_id)
try:
return urlopen(INNINGS_URL.format(year, month, day, game_id))
return urlopen(GAME_URL.format(year, month, day, game_id,
'game_events.xml'))
except HTTPError:
raise ValueError('Could not find a game with that id.')

Expand Down
36 changes: 24 additions & 12 deletions mlbgame/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""Module that controls getting stats and creating objects to hold that
information."""

from lxml import etree
import lxml.etree as etree

import mlbgame.data
import mlbgame.object
Expand Down Expand Up @@ -69,41 +69,49 @@ def player_stats(game_id):
"""
# get data from data module
box_score = mlbgame.data.get_box_score(game_id)
if mlbgame.data.does_raw_box_score_exist(game_id):
raw_box_score = mlbgame.data.get_raw_box_score(game_id)
# parse XML
box_score_tree = etree.parse(box_score).getroot()
if mlbgame.data.does_raw_box_score_exist(game_id):
raw_box_score_tree = etree.parse(raw_box_score).getroot()
# get pitching and batting info
pitching = box_score_tree.findall('pitching')
batting = box_score_tree.findall('batting')
# get parsed stats
pitching_info = __player_stats_info(pitching, 'pitcher')
batting_info = __player_stats_info(batting, 'batter')
# rawboxscore not available after 2018
try:
raw_box_score = mlbgame.data.get_raw_box_score(game_id)
raw_box_score_tree = etree.parse(raw_box_score).getroot()
additional_stats = __raw_player_stats_info(raw_box_score_tree)
# get parsed additional stats
#additional_stats = __raw_player_stats_info(raw_box_score_tree)
if mlbgame.data.does_raw_box_score_exist(game_id):
addl_home_pitching = additional_stats[0]['pitchers']
addl_home_batting = additional_stats[0]['batters']
addl_away_pitching = additional_stats[1]['pitchers']
addl_away_batting = additional_stats[1]['batters']

if mlbgame.data.does_raw_box_score_exist(game_id):
output = {
'home_pitching': pitching_info[0],
'away_pitching': pitching_info[1],
'home_batting': batting_info[0],
'away_batting': batting_info[1],


# Section of person that

'home_additional_pitching': addl_home_pitching,
'away_additional_pitching': addl_away_pitching,
'home_additional_batting': addl_home_batting,
'away_additional_batting': addl_away_batting
}
except etree.XMLSyntaxError:
return output
else:
output = {
'home_pitching': pitching_info[0],
'away_pitching': pitching_info[1],
'home_batting': batting_info[0],
'away_batting': batting_info[1],
}
return output
return output


def __team_stats_info(data, output, output_key):
Expand Down Expand Up @@ -152,18 +160,21 @@ def team_stats(game_id):
"""
# get data from data module
box_score = mlbgame.data.get_box_score(game_id)
raw_box_score = mlbgame.data.get_raw_box_score(game_id)
if mlbgame.data.does_raw_box_score_exist(game_id):
raw_box_score = mlbgame.data.get_raw_box_score(game_id)
# parse XML
box_score_tree = etree.parse(box_score).getroot()
raw_box_score_tree = etree.parse(raw_box_score).getroot()
if mlbgame.data.does_raw_box_score_exist(game_id):
raw_box_score_tree = etree.parse(raw_box_score).getroot()
# get pitching and batting ingo
pitching = box_score_tree.findall('pitching')
batting = box_score_tree.findall('batting')
# dictionary for output
output = {}
output = __team_stats_info(pitching, output, 'pitching')
output = __team_stats_info(batting, output, 'batting')
output = __raw_team_stats_info(raw_box_score_tree, output)
if mlbgame.data.does_raw_box_score_exist(game_id):
output = __raw_team_stats_info(raw_box_score_tree, output)
return output


Expand Down Expand Up @@ -206,6 +217,7 @@ def __init__(self, data, game_id, player):
self.away_pitching = output['away_pitching']
self.home_batting = output['home_batting']
self.away_batting = output['away_batting']

self.home_additional_pitching = output['home_additional_pitching']
self.away_additional_pitching = output['away_additional_pitching']
self.home_additional_batting = output['home_additional_batting']
Expand Down