nba-stats 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/nba-stats +5 -0
- data/lib/nba_stats.rb +4 -0
- data/lib/nba_stats/cli.rb +73 -0
- data/lib/nba_stats/player.rb +39 -0
- data/lib/nba_stats/scraper.rb +64 -0
- data/lib/nba_stats/team.rb +34 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 135e3449da27b9a969d28c42c820ff4c1339cd22
|
4
|
+
data.tar.gz: c18b967ef7fb3aca35e915b3c109bc97df995cf0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7f22a038243150407057d84d4a9c6c8022f38b1cc9ba10a8b57489008105cf1f3f61951569c1c3e9e8070d33cb8d1975020d2fe53decfcf2f5d69d15b77d2fe4
|
7
|
+
data.tar.gz: 80cfb062fa46fb28d61e68ecd2b0ebc1a3baf12f5afcfeffc56f55946ae6179d6bae28e0a8a58e482a83b0e0c112e3897aea69f2550064db5f1b30edf0433839
|
data/bin/nba-stats
ADDED
data/lib/nba_stats.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
class NbaStats::CLI
|
2
|
+
|
3
|
+
def call
|
4
|
+
start
|
5
|
+
end
|
6
|
+
|
7
|
+
def start
|
8
|
+
puts "Welcome to the NBA Stats CLI Gem"
|
9
|
+
puts "Here's the list of current teams"
|
10
|
+
make_teams
|
11
|
+
NbaStats::Team.all.each do |team|
|
12
|
+
puts team.name
|
13
|
+
end
|
14
|
+
|
15
|
+
choose_team
|
16
|
+
choose_player
|
17
|
+
|
18
|
+
puts "Do you want to look up another player on this team? y/n"
|
19
|
+
response = gets.strip
|
20
|
+
while response == "y"
|
21
|
+
choose_player
|
22
|
+
puts "Do you want to look up another player on this team? y/n"
|
23
|
+
response = gets.strip
|
24
|
+
end
|
25
|
+
|
26
|
+
puts "Do you want to look up another team? y/n"
|
27
|
+
response = gets.strip
|
28
|
+
if response == "y"
|
29
|
+
start
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def make_teams
|
34
|
+
teams_array = NbaStats::Scraper.get_teams
|
35
|
+
NbaStats::Team.create_from_collection(teams_array)
|
36
|
+
end
|
37
|
+
|
38
|
+
def choose_team
|
39
|
+
puts "Enter a team name to see their roster: "
|
40
|
+
requested_team = gets.strip
|
41
|
+
while !NbaStats::Team.team_names.include? requested_team
|
42
|
+
puts "That team doesn't exist. Try again."
|
43
|
+
requested_team = gets.strip
|
44
|
+
end
|
45
|
+
team = NbaStats::Team.all.detect {|team| team.name == requested_team}
|
46
|
+
team.add_players
|
47
|
+
puts team.name + " roster:"
|
48
|
+
rows = [["Number", "Name", "Position", "Height", "Experience"]]
|
49
|
+
team.players.each do |player|
|
50
|
+
rows << [player.number, player.name, player.position, player.height, player.experience]
|
51
|
+
end
|
52
|
+
roster_table = Terminal::Table.new rows: rows
|
53
|
+
puts roster_table
|
54
|
+
end
|
55
|
+
|
56
|
+
def choose_player
|
57
|
+
puts "Enter a player name to see their individual stats: "
|
58
|
+
requested_player = gets.strip
|
59
|
+
while !NbaStats::Player.player_names.include? requested_player
|
60
|
+
puts "That player isn't on this team. Try again."
|
61
|
+
requested_player = gets.strip
|
62
|
+
end
|
63
|
+
player = NbaStats::Player.all.detect {|player| player.name == requested_player}
|
64
|
+
stats_hash = NbaStats::Scraper.get_player_stats(player)
|
65
|
+
player.add_player_stats(stats_hash)
|
66
|
+
rows = [["Points/Game", "Assists/Game", "Rebounds/Game", "Blocks/Game", "Steals/Game", "FG%", "3P%", "FT%", "Minutes/Game",]]
|
67
|
+
rows << [player.points_pg, player.assists_pg, player.rebounds_pg, player.blocks_pg, player.steals_pg, player.fg_percentage, player.three_percentage, player.ft_percentage, player.minutes_pg]
|
68
|
+
puts "Here are #{player.name}'s 2015-16 stats: "
|
69
|
+
stats_table = Terminal::Table.new rows: rows
|
70
|
+
puts stats_table
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class NbaStats::Player
|
2
|
+
attr_accessor :name, :team, :number, :player_url, :position, :height, :experience, :points_pg, :assists_pg, :rebounds_pg, :blocks_pg, :steals_pg, :minutes_pg, :fg_percentage, :three_percentage, :ft_percentage
|
3
|
+
|
4
|
+
@@all = []
|
5
|
+
def initialize(player_hash)
|
6
|
+
player_hash.each do |key, value|
|
7
|
+
self.send("#{key}=", value)
|
8
|
+
end
|
9
|
+
@@all << self
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.create_from_collection(players_array)
|
13
|
+
players_array.each do |player|
|
14
|
+
new_player = NbaStats::Player.new(player)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.create_from_collection_with_team(players_array, team)
|
19
|
+
players_array.each do |player|
|
20
|
+
new_player = NbaStats::Player.new(player)
|
21
|
+
new_player.team = team
|
22
|
+
team.players << new_player
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_player_stats(stats_hash)
|
27
|
+
stats_hash.each do |key,value|
|
28
|
+
self.send("#{key}=", value)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.all
|
33
|
+
@@all
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.player_names
|
37
|
+
@@all.collect {|player| player.name}
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class NbaStats::Scraper
|
2
|
+
|
3
|
+
def self.open_page(url)
|
4
|
+
Nokogiri::HTML(open(url))
|
5
|
+
end
|
6
|
+
|
7
|
+
# Returns an array of hashes with current team names and links to 2015-16 team page
|
8
|
+
def self.get_teams
|
9
|
+
page = open_page("http://www.basketball-reference.com/teams")
|
10
|
+
teams_array = []
|
11
|
+
teams = page.css("table#active tr.full_table a")
|
12
|
+
teams.each do |team|
|
13
|
+
name = team.text
|
14
|
+
team_url = "http://www.basketball-reference.com"+ team["href"] + "2016.html"
|
15
|
+
hash = {name: name, team_url: team_url}
|
16
|
+
teams_array << hash
|
17
|
+
end
|
18
|
+
teams_array
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.get_roster(team)
|
22
|
+
page = open_page(team.team_url)
|
23
|
+
players_array = []
|
24
|
+
players = page.css("table#roster tr")
|
25
|
+
players.drop(1).each do |player|
|
26
|
+
data_array = player.text.split("\n").map {|x| x.strip}
|
27
|
+
number = data_array[1]
|
28
|
+
name = data_array[2]
|
29
|
+
position = data_array[3]
|
30
|
+
height = data_array[4]
|
31
|
+
data_array[8] == "R" ? experience = "Rookie" : experience = data_array[8] + " Years"
|
32
|
+
|
33
|
+
#Below is messy workaround
|
34
|
+
#Cleaner player.css("a").first["href"] works outside of .each loop, but returns "undefined method '[]' for nil" inside .each
|
35
|
+
player_url = player.css("a").map {|element| element["href"]}.first
|
36
|
+
player_url = "http://www.basketball-reference.com" + player_url.to_s
|
37
|
+
|
38
|
+
hash = {name: name, number: number, position: position, height: height, experience: experience, player_url: player_url}
|
39
|
+
players_array << hash
|
40
|
+
end
|
41
|
+
players_array
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.get_player_stats(player)
|
45
|
+
page = open_page(player.player_url)
|
46
|
+
|
47
|
+
season = page.css("table#per_game tr.full_table").last
|
48
|
+
stats_array = season.text.gsub("\n", "").strip.split(" ")
|
49
|
+
|
50
|
+
stats_hash = {
|
51
|
+
points_pg: stats_array[29],
|
52
|
+
assists_pg: stats_array[24],
|
53
|
+
rebounds_pg: stats_array[23],
|
54
|
+
blocks_pg: stats_array[26],
|
55
|
+
steals_pg: stats_array[25],
|
56
|
+
minutes_pg: stats_array[7],
|
57
|
+
fg_percentage: stats_array[10],
|
58
|
+
three_percentage: stats_array[13],
|
59
|
+
ft_percentage: stats_array[20]
|
60
|
+
}
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class NbaStats::Team
|
2
|
+
|
3
|
+
attr_accessor :name, :team_url, :players
|
4
|
+
|
5
|
+
@@all = []
|
6
|
+
|
7
|
+
def initialize(team_hash)
|
8
|
+
team_hash.each do |key, value|
|
9
|
+
self.send("#{key}=", value)
|
10
|
+
end
|
11
|
+
@players = []
|
12
|
+
@@all << self
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_players
|
16
|
+
players_array = NbaStats::Scraper.get_roster(self)
|
17
|
+
NbaStats::Player.create_from_collection_with_team(players_array, self)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.create_from_collection(teams_array)
|
21
|
+
teams_array.each do |team|
|
22
|
+
new_team = NbaStats::Team.new(team)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.all
|
27
|
+
@@all
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.team_names
|
31
|
+
@@all.collect {|team| team.name }
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nba-stats
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Barron
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: term-ansicolor
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: terminal-table
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.5.2
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.5.2
|
111
|
+
description: A command line interface for looking 2015-16 NBA player stats
|
112
|
+
email: alexbarron@gmail.com
|
113
|
+
executables:
|
114
|
+
- nba-stats
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- bin/nba-stats
|
119
|
+
- lib/nba_stats.rb
|
120
|
+
- lib/nba_stats/cli.rb
|
121
|
+
- lib/nba_stats/player.rb
|
122
|
+
- lib/nba_stats/scraper.rb
|
123
|
+
- lib/nba_stats/team.rb
|
124
|
+
homepage: http://rubygems.org/gems/nba-stats
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 2.4.8
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: NBA Stats
|
148
|
+
test_files: []
|