Last active
October 22, 2019 18:16
-
-
Save DRBragg/d20e234ffec2d15a9ebed907bbdedca6 to your computer and use it in GitHub Desktop.
NHL data for Sports Club Stats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'json' | |
TEAMS_URL = 'https://statsapi.web.nhl.com/api/v1/teams' | |
DATA_URL = 'https://statsapi.web.nhl.com/api/v1/schedule?startDate=2019-10-02&endDate=2020-04-04&expand=schedule.linescore' | |
def fetch_data(url) | |
uri = URI(url) | |
response = Net::HTTP.get(uri) | |
JSON.parse(response) | |
end | |
# Setup Teams hash | |
data = fetch_data(TEAMS_URL) | |
teams_data = data['teams'].map {|team| [team['id'].to_s, team['locationName']]} | |
teams = teams_data.to_h | |
teams['2'] = 'NY Islanders' | |
teams['3'] = 'NY Rangers' | |
# Get schedule and scores | |
data = fetch_data(DATA_URL) | |
def makeScore(game) | |
ot = game.dig('linescore', 'currentPeriod') > 3 | |
away_score = game.dig('teams', 'away', 'score') | |
home_score = game.dig('teams', 'home', 'score') | |
score = "#{away_score}-#{home_score}" | |
score += '(ot)' if ot | |
score | |
end | |
games = [] | |
data['dates'].each do |date| | |
date_string = date['date'] | |
formatted_games = date['games'].map do |game| | |
{ | |
date: Date.parse(date_string).strftime('%D'), | |
time: Time.zone.parse(game['gameDate']).strftime('%l:%M %p').squish, | |
away: teams[game.dig('teams', 'away', 'team', 'id').to_s], | |
score: game['status']['statusCode'] == '7' ? makeScore(game) : '', | |
home: teams[game.dig('teams', 'home', 'team', 'id').to_s] | |
} | |
end | |
games.concat(formatted_games) | |
end | |
csv = games.map { |game| game.values.to_csv } | |
Dir.chdir(Dir.home + '/desktop') | |
template = File.read('nhl_data_template') | |
File.open('nhl_data', 'w') do |file| | |
file.puts template | |
csv.each { |row| file.puts row.gsub(',', ' ').gsub('""', '').gsub('é', 'e') } | |
file.puts 'GamesEnd' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LeagueBegin | |
League: National Hockey League (NHL) (Sort: Lottery) | |
Conference: Eastern Conference (Eastern) (Sort: Conf) (Playoffs: 8) | |
Division: Atlantic Division (Atlantic) (Sort: Div) (Playoffs: 3) | |
Team: Boston Bruins (Boston) | |
Team: Buffalo Sabres (Buffalo) | |
Team: Detroit Red Wings (Detroit) | |
Team: Florida Panthers (Florida) | |
Team: Montreal Canadiens (Montreal) | |
Team: Ottawa Senators (Ottawa) | |
Team: Tampa Bay Lightning (Tampa Bay) | |
Team: Toronto Maple Leafs (Toronto) | |
Division: Metropolitan (Sort: Div) (Playoffs: 3) | |
Team: Carolina Hurricanes (Carolina) | |
Team: Columbus Blue Jackets (Columbus) | |
Team: New Jersey Devils (New Jersey) | |
Team: New York Islanders (NY Islanders) | |
Team: New York Rangers (NY Rangers) | |
Team: Philadelphia Flyers (Philadelphia) | |
Team: Pittsburgh Penguins (Pittsburgh) | |
Team: Washington Capitals (Washington) | |
Conference: Western Conference (Western) (Sort: Conf) (Playoffs: 8) | |
Division: Central Division (Central) (Sort: Div) (Playoffs: 3) | |
Team: Chicago Blackhawks (Chicago) | |
Team: Colorado Avalanche (Colorado) | |
Team: Dallas Stars (Dallas) | |
Team: Minnesota Wild (Minnesota) | |
Team: Nashville Predators (Nashville) | |
Team: St. Louis Blues (St. Louis) | |
Team: Winnipeg Jets (Winnipeg) | |
Division: Pacific Division (Pacific) (Sort: Div) (Playoffs: 3) | |
Team: Anaheim Ducks (Anaheim) | |
Team: Arizona Coyotes (Arizona) | |
Team: Calgary Flames (Calgary) | |
Team: Edmonton Oilers (Edmonton) | |
Team: Los Angeles Kings (Los Angeles) | |
Team: San Jose Sharks (San Jose) | |
Team: Vancouver Canucks (Vancouver) | |
Team: Vegas Golden Knights (Vegas) | |
Kind: pro | |
Sport: hockey | |
Gender: male | |
Country: USA | |
State: | |
Level: A | |
Season: 2019-2020 (current: True) | |
Author: Drew Bragg | |
Note: 16 teams will qualify for the Stanley Cup Playoffs. The format is a set bracket that is largely division-based with wild cards. The top three teams in each division will make up the first 12 teams in the playoffs. The remaining four spots will be filled by the next two highest-placed finishers in each conference, based on regular-season record and regardless of division. It is possible for one division in each conference to send five teams to the postseason while the other sends just three.<br/><br/>The percent after the team score is the chance of winning in regulation based on current second and<br/>either home/away actual score or home/away goal differential. Please send any issues to drbragg at gmail dot com.<br/> | |
LotteryNote: | |
LeagueEnd | |
SortBegin | |
Div: Points, WinsInRegulationOrOvertime, Average Points HeadToHeadNHL, GoalsDelta, DrawLots | |
Conf: MoveClinchedToFrontBySeed, Points, WinsInRegulationOrOvertime, Average Points HeadToHeadNHL, GoalsDelta, DrawLots | |
Lottery: MoveClinchedToFrontBySeed, Points, WinsInRegulationOrOvertime, Average Points HeadToHeadNHL, GoalsDelta, DrawLots | |
SortEnd | |
RulesBegin | |
PointsForWinInRegulation: 2 | |
PointsForWinInOvertime: 2 | |
PointsForWinInShootout: 2 | |
PointsForLossInRegulation: 0 | |
PointsForLossInOvertime: 1 | |
PointsForLossInShootout: 1 | |
PointsForTie: 1 | |
PercentOfGamesThatEndInTie: 0 | |
PercentOfGamesThatEndInOvertimeWin: 0.120237087214225 | |
PercentOfGamesThatEndInShootoutWin: 0.123624047417443 | |
HomeFieldAdvantage: 0.56 | |
WeightType: PythagenpatIgnoreShootOutWinningGoals | |
WeightExponent: 0.458 | |
WhatDoYouCallATie: tie | |
WhatDoYouCallLottery: lottery | |
WhatDoYouCallPromoted: Pres Trophy | |
Promote: 1 | |
PromotePlus: 0 | |
PromotePlusPercent: 0 | |
WhatDoYouCallDemoted: relegated | |
Demote: 0 | |
DemotePlus: 0 | |
DemotePlusPercent: 0 | |
RulesEnd | |
GamesBegin | |
TeamListedFirst: away | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment