def get_venues(league, api_key): context = {} url = 'http://api.sportsdatallc.org/%s-t%d/venues/venues.xml?api_key=%s' xmlns_pattern = r"\sxmlns=\"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+\"" try: f = None if league == 'mlb': f = urllib2.urlopen(url % (league, 4, api_key)) xml_input = re.sub(xmlns_pattern, '', f.read()) f.close() root = ET.XML(xml_input) for venue_tree in root.findall('venue'): venue_id = venue_tree.get('id') venue_name = venue_tree.get("name") if venue_tree.get('market'): venue_address = venue_name + ', ' + venue_tree.get('market') else: venue_address = venue_name venue = Venue(venue_id=venue_id, name=venue_name, address=venue_address) venue.save() except Exception, e: print >> sys.stderr, e
def get_players(league, api_key, year): xmlns_pattern = r"\sxmlns=\"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+\"" try: f = None if league == 'mlb': url = 'http://api.sportsdatallc.org/mlb-t4/rosters-full/%s.xml?api_key=%s' f = urllib2.urlopen(url % (year, api_key)) xml_input = re.sub(xmlns_pattern, '', f.read()) f.close() root = ET.XML(xml_input) if league == "mlb": teams = root.findall('team') for team_tree in teams: team_id = team_tree.get('id') if team_tree.find('players'): for player_tree in team_tree.find('players').findall( 'player'): jersey = player_tree.get('jersey') position = player_tree.get('primary_position') player_id = player_tree.get('id') last_name = player_tree.get('last_name') first_name = player_tree.get('first_name') weight = player_tree.get('weight') height = player_tree.get('height') birth_date = '' if player_tree.get('birthdate'): birth_date = datetime.datetime.strptime( player_tree.get('birthdate'), "%Y-%m-%d") birth_city = player_tree.get('birthcity') birth_state = player_tree.get('birthstate') birth_country = player_tree.get('birthcountry') college = player_tree.get('college') new_player = Player(jersey=jersey, position=position, player_id=player_id, last_name=last_name, first_name=first_name, weight=weight, height=height, birth_city=birth_city, birth_state=birth_state, birth_country=birth_country, college=college, team=Team.objects.get(pk=team_id)) if birth_date: new_player.birth_date = birth_date new_player.save() except Exception, e: print >> sys.stderr, e
def get_teams(league, api_key, year=None): url = 'http://api.sportsdatallc.org/%s-t%d/league/hierarchy.xml?api_key=%s' xmlns_pattern = r"\sxmlns=\"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+\"" try: f = None if league == 'nba' or league == 'nhl': f = urllib2.urlopen(url % (league, 3, api_key)) if league == 'nfl': url = 'http://api.sportsdatallc.org/nfl-t1/teams/hierarchy.xml?api_key=%s' f = urllib2.urlopen(url % (api_key)) if league == 'mlb': url = 'http://api.sportsdatallc.org/mlb-t4/teams/%s.xml?api_key=%s' f = urllib2.urlopen(url % (year, api_key)) xml_input = re.sub(xmlns_pattern, '', f.read()) f.close() root = ET.XML(xml_input) teams = None if league == 'nba' or league == 'nhl' or league == 'nfl': teams = [] for conference in root.findall('conference'): for division in conference.findall('division'): for team in division.findall('team'): teams.append(team) if league == 'mlb': teams = root.findall('team') for team_tree in teams: id = team_tree.get('id') if team_tree.get('market'): market = team_tree.get('market') name = team_tree.get('name') whole_name = market + ' ' + name if league == 'nba' or league == 'nhl' or league == 'nfl': alias = team_tree.get('alias') venue_tree = team_tree.find('venue') venue_id = venue_tree.get('id') venue_name = venue_tree.get('name') venue_address = venue_tree.get( 'address') + ', ' + venue_tree.get( 'city') + ', ' + venue_tree.get( 'state') + ', ' + venue_tree.get( 'zip') + ', ' + venue_tree.get('country') venue_capcity = venue_tree.get('capacity') venue = Venue(venue_id=venue_id, name=venue_name, address=venue_address, capacity=int(venue_capcity)) venue.save() if league == 'mlb': if team_tree.get('venue'): alias = team_tree.get('abbr') venue_id = team_tree.get('venue') venue = Venue.objects.get(pk=venue_id) team = Team(team_id=id, market=market, name=name, whole_name=whole_name, league=league, venue=venue, alias=alias) team.save() except Exception, e: print >> sys.stderr, e
def get_season_schedule(league, api_key, year, season_type=None): context = {} # key_dict = {'nba':'y6ytb65x4aa65anucqefq2up', 'nhl':'vrnt63n7zg54symwn3m7tw4s', 'nfl':'yt477s5drjxn2eb6xumnu237', 'mlb':'z883vkekaeb48rhzkhr2bvsh'} xmlns_pattern = r"\sxmlns=\"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+\"" try: f = None if league == 'nba' or league == 'nhl': url = 'http://api.sportsdatallc.org/%s-t%d/games/%s/%s/schedule.xml?api_key=%s' f = urllib2.urlopen(url % (league, 3, year, season_type, api_key)) if league == 'nfl': url = 'http://api.sportsdatallc.org/nfl-t1/%s/%s/schedule.xml?api_key=%s' f = urllib2.urlopen(url % (year, season_type, api_key)) if league == 'mlb': url = 'http://api.sportsdatallc.org/mlb-t4/schedule/%s.xml?api_key=%s' f = urllib2.urlopen(url % (year, api_key)) # delete the xmlns information associated with each tag xml_input = re.sub(xmlns_pattern, '', f.read()) f.close() root = ET.XML(xml_input) games = None if league == 'nba' or league == 'nhl': games = root.find('season-schedule').find('games') if league == 'nfl': games = [] for week_schedule in root.findall('week'): for game in week_schedule.findall('game'): games.append(game) if league == 'mlb': games = root.findall('event') for game_tree in games: id = game_tree.get('id') home_team_id = None away_team_id = None start_time = None venue_id = None # print >> sys.stderr, id if league == 'nba' or league == 'nhl': home_team_id = game_tree.get('home_team') away_team_id = game_tree.get('away_team') if league == 'nfl': home_team_id = game_tree.get('home') away_team_id = game_tree.get('away') if league == 'mlb': home_team_id = game_tree.get('home') away_team_id = game_tree.get('visitor') if league == 'nba' or league == 'nhl' or league == 'nfl': start_time = dateutil.parser.parse(game_tree.get('scheduled')) venue_tree = game_tree.find('venue') venue_id = venue_tree.get('id') if league == 'mlb': start_time = dateutil.parser.parse( game_tree.find('scheduled_start').text) venue_id = game_tree.get('venue') if not Venue.objects.filter(pk=venue_id).count(): venue_name = venue_tree.get('name') venue_address = '' if venue_tree.get('address'): venue_address = venue_tree.get( 'address') + ', ' + venue_tree.get( 'city') + ', ' + venue_tree.get('country') else: venue_address = venue_tree.get( 'city') + ', ' + venue_tree.get('country') venue_capcity = venue_tree.get('capacity') new_venue = Venue(venue_id=venue_id, name=venue_name, address=venue_address, capacity=int(venue_capcity)) new_venue.save() if not Team.objects.filter(pk=home_team_id).count(): home_tree = game_tree.find('home') team_name = home_tree.get('name') team_alias = home_tree.get('alias') new_team = Team(team_id=home_team_id, name=team_name, alias=team_alias, league=league, venue=Venue.objects.get(pk=venue_id)) new_team.save() if not Team.objects.filter(pk=away_team_id).count(): away_tree = game_tree.find('away') team_name = away_tree.get('name') team_alias = away_tree.get('alias') new_team = Team(team_id=away_team_id, name=team_name, alias=team_alias, league=league, venue=Venue.objects.get(pk=venue_id)) new_team.save() new_game = Game(game_id=id, venue=Venue.objects.get(pk=venue_id), league=league, start_time=start_time, home_team=Team.objects.get(pk=home_team_id), away_team=Team.objects.get(pk=away_team_id)) new_game.save() except Exception, e: print >> sys.stderr, e