espn_nba_fantasy 0.0.3 → 0.0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8af37d5a1de02fb8468cefc41d985f509e0093e8b0c8463785f6142d869e3435
4
- data.tar.gz: 8437fad4fa2f5cca09c90a35672a59299f5272d975f406d9edcfc56f3939ea73
3
+ metadata.gz: 625920e5a357e3f2eda3567e8241d14572c870da13407e3ab92a4c10d2478ca1
4
+ data.tar.gz: d2cd2b8c2bbca0c7ce7bf1af48c9f5e7d2c15191c4d4aac73eef2c280cfbcd67
5
5
  SHA512:
6
- metadata.gz: f108b5042c9c293a39b9024525d78d46dbf55141a71b6fc70cacfa577ad3a8ca04c2fad6243a735c90a68979598e622321787af53b248d5f49c79c76f0bcf6ec
7
- data.tar.gz: ddd3beaea91b74251b6dd7802d779a1421aa683cddd1ee706598ef11c00c1e4df2679aa6b21e3f65446003421b5634e08cdd292b3ab3024c749ae9929f7e04ba
6
+ metadata.gz: 6e7e2f7d59c0a87244665acc1b5a4dc2caffb0198f12eb02a86ee4d666a0dd84d7860fedc6e319c731921f1f1118785de56ad72f7dfef49eb2db5e27248c8425
7
+ data.tar.gz: e7eb379f3712c83520146fef0f7c5b15669954d6068c66d07de0dfbb6b551da9aff543edabb4fb8cf36665ec17afc2ffc0112cf0c0793fd610f0dc3b2e9c321f
@@ -1,94 +1,97 @@
1
1
  #these are some of the constants necessary to map ids from the ESPN Fantasy Library to,
2
2
  #so that things like position, stat name, etc are readable!
3
3
 
4
- POSITION_MAP = ['PG', 'SG', 'SF', 'PF', 'C', 'G', 'F', 'SG/SF', 'G/F', 'PF/C', 'F/C', 'UT', 'BE', 'IR', '', 'Rookie']
4
+ module ESPNNBAFantasy
5
+
6
+ POSITION_MAP = ['PG', 'SG', 'SF', 'PF', 'C', 'G', 'F', 'SG/SF', 'G/F', 'PF/C', 'F/C', 'UT', 'BE', 'IR', '', 'Rookie']
5
7
 
6
- PRO_TEAM_MAP = ['FA', 'ATL','BOS','NOP','CHI','CLE','DAL','DEN','DET','GSW','HOU','IND',
7
- 'LAC','LAL','MIA','MIL','MIN','BKN','NYK','ORL','PHL','PHO','POR','SAC','SAS','OKC',
8
- 'UTA','WAS','TOR','MEM','CHA']
8
+ PRO_TEAM_MAP = ['FA', 'ATL','BOS','NOP','CHI','CLE','DAL','DEN','DET','GSW','HOU','IND',
9
+ 'LAC','LAL','MIA','MIL','MIN','BKN','NYK','ORL','PHL','PHO','POR','SAC','SAS','OKC',
10
+ 'UTA','WAS','TOR','MEM','CHA']
9
11
 
10
- STATS_MAP = {
11
- '0' => 'PTS',
12
- '1' => 'BLK',
13
- '2' => 'STL',
14
- '3' => 'AST',
15
- '4' => 'OREB',
16
- '5' => 'DREB',
17
- '6' => 'REB',
18
- '7'=> '7',
19
- '8'=> '8',
20
- '9'=> 'PF',
21
- '10'=> '10',
22
- '11'=> 'TO',
23
- '12'=> '12',
24
- '13'=> 'FGM',
25
- '14'=> 'FGA',
26
- '15'=> 'FTM',
27
- '16'=> 'FTA',
28
- '17'=> '3PTM',
29
- '18'=> '3PTA',
30
- '19'=> 'FG%',
31
- '20'=> 'FT%',
32
- '21'=> '3PT%',
33
- '22'=> '22',
34
- '23'=> '23',
35
- '24'=> '24',
36
- '25'=> '25',
37
- '26'=> '26',
38
- '27'=> '27',
39
- '28'=> 'MPG',
40
- '29'=> '29',
41
- '30'=> '30',
42
- '31'=> '31',
43
- '32'=> '32',
44
- '33'=> '33',
45
- '34'=> '34',
46
- '35'=> '35',
47
- '36'=> '36',
48
- '37'=> '37',
49
- '38'=> '38',
50
- '39'=> '39',
51
- '40'=> 'MIN',
52
- '41'=> 'GS',
53
- '42'=> 'GP',
54
- '43'=> '43',
55
- '44'=> '44',
56
- '45'=> '45',
57
- }
12
+ STATS_MAP = {
13
+ '0' => 'PTS',
14
+ '1' => 'BLK',
15
+ '2' => 'STL',
16
+ '3' => 'AST',
17
+ '4' => 'OREB',
18
+ '5' => 'DREB',
19
+ '6' => 'REB',
20
+ '7'=> '7',
21
+ '8'=> '8',
22
+ '9'=> 'PF',
23
+ '10'=> '10',
24
+ '11'=> 'TO',
25
+ '12'=> '12',
26
+ '13'=> 'FGM',
27
+ '14'=> 'FGA',
28
+ '15'=> 'FTM',
29
+ '16'=> 'FTA',
30
+ '17'=> '3PTM',
31
+ '18'=> '3PTA',
32
+ '19'=> 'FG%',
33
+ '20'=> 'FT%',
34
+ '21'=> '3PT%',
35
+ '22'=> '22',
36
+ '23'=> '23',
37
+ '24'=> '24',
38
+ '25'=> '25',
39
+ '26'=> '26',
40
+ '27'=> '27',
41
+ '28'=> 'MPG',
42
+ '29'=> '29',
43
+ '30'=> '30',
44
+ '31'=> '31',
45
+ '32'=> '32',
46
+ '33'=> '33',
47
+ '34'=> '34',
48
+ '35'=> '35',
49
+ '36'=> '36',
50
+ '37'=> '37',
51
+ '38'=> '38',
52
+ '39'=> '39',
53
+ '40'=> 'MIN',
54
+ '41'=> 'GS',
55
+ '42'=> 'GP',
56
+ '43'=> '43',
57
+ '44'=> '44',
58
+ '45'=> '45',
59
+ }
58
60
 
59
- STAT_ID_MAP = {
60
- '00': 'total',
61
- '10': 'projected',
62
- '01': 'last_7',
63
- '02': 'last_15',
64
- '03': 'last_30'
65
- }
61
+ STAT_ID_MAP = {
62
+ '00': 'total',
63
+ '10': 'projected',
64
+ '01': 'last_7',
65
+ '02': 'last_15',
66
+ '03': 'last_30'
67
+ }
66
68
 
67
- ACTIVITY_MAP = {
68
- 178 => 'FA ADDED',
69
- 180 => 'WAIVER ADDED',
70
- 179 => 'DROPPED',
71
- 181 => 'DROPPED',
72
- 239 => 'DROPPED',
73
- 244 => 'TRADED',
74
- 'FA': 178,
75
- 'WAIVER': 180,
76
- 'TRADED': 244
77
- }
69
+ ACTIVITY_MAP = {
70
+ 178 => 'FA ADDED',
71
+ 180 => 'WAIVER ADDED',
72
+ 179 => 'DROPPED',
73
+ 181 => 'DROPPED',
74
+ 239 => 'DROPPED',
75
+ 244 => 'TRADED',
76
+ 'FA': 178,
77
+ 'WAIVER': 180,
78
+ 'TRADED': 244
79
+ }
78
80
 
79
- NINE_CAT_STATS = [
80
- '3PTM',
81
- 'AST',
82
- 'BLK',
83
- 'FG%',
84
- 'FT%',
85
- 'PTS',
86
- 'REB',
87
- 'STL',
88
- 'TO'
89
- ]
81
+ NINE_CAT_STATS = [
82
+ '3PTM',
83
+ 'AST',
84
+ 'BLK',
85
+ 'FG%',
86
+ 'FT%',
87
+ 'PTS',
88
+ 'REB',
89
+ 'STL',
90
+ 'TO'
91
+ ]
90
92
 
91
- STATS = ['PTS','BLK','STL','AST','OREB','DREB','TO','FGM','FTM','3PTM', 'FGA', '3PTA', 'FTA']
93
+ STATS = ['PTS','BLK','STL','AST','OREB','DREB','TO','FGM','FTM','3PTM', 'FGA', '3PTA', 'FTA']
92
94
 
93
- EMPTY_STATS = {'PTS'=>0,'BLK'=>0,'STL'=>0,'AST'=>0,'OREB'=>0,'DREB'=>0,'TO'=>0,'FGM'=>0,'FTM'=>0,'3PTM'=>0, 'FGA'=>0, '3PTA'=>0, 'FTA'=>0}
95
+ EMPTY_STATS = {'PTS'=>0,'BLK'=>0,'STL'=>0,'AST'=>0,'OREB'=>0,'DREB'=>0,'TO'=>0,'FGM'=>0,'FTM'=>0,'3PTM'=>0, 'FGA'=>0, '3PTA'=>0, 'FTA'=>0}
94
96
 
97
+ end
@@ -4,67 +4,75 @@ require_relative 'constants'
4
4
  require_relative 'team'
5
5
  require_relative 'player_finder'
6
6
 
7
- class League
7
+ module ESPNNBAFantasy
8
+ class League
8
9
 
9
- include PlayerFinder
10
+ include PlayerFinder
10
11
 
11
- attr_accessor :teams, :stat_data
12
+ attr_accessor :teams, :stat_data
12
13
 
13
- #initializes the league
14
+ #initializes the league
14
15
 
15
- def initialize(league_id, year, s2, sw)
16
- @uri = "https://fantasy.espn.com/apis/v3/games/fba/seasons/#{year}/segments/0/leagues/#{league_id}?view=mTeam&view=mRoster&view=mMatchup&view=mSettings&view=mStandings"
17
- @cookies = {'espn_s2': "#{s2}", 'SWID': "#{sw}"}
18
- @data = JSON.parse(RestClient.get(@uri, {cookies: @cookies}))
19
- @teams = make_team_objects
20
- @stat_data = make_stat_data
21
- end
16
+ def initialize(league_id, year, s2, sw)
17
+ @league_id = league_id
22
18
 
23
- #gives you an array of each team's name and id in the league
19
+ @uri = "https://lm-api-reads.fantasy.espn.com/apis/v3/games/fba/seasons/#{year}/segments/0/leagues/#{league_id}?view=mRoster&view=mSettings&view=mTeam&view=modular&view=mNav"
20
+ @cookies = {'espn_s2': "#{s2}", 'SWID': "#{sw}"}
21
+ @data = JSON.parse(RestClient.get(@uri, {cookies: @cookies}))
22
+ @teams = make_team_objects
23
+ @stat_data = make_stat_data
24
+ end
24
25
 
25
- def team_list
26
- @teams.map{|r| [r.name, r.team_id]}
27
- end
26
+ def to_s
27
+ "League #{@league_id}"
28
+ end
28
29
 
29
- #pull up a Player object based on their name and their team
30
+ #gives you an array of each team's name and id in the league
30
31
 
31
- def findplayer(team_name, str)
32
- team = teams.select{|t| t.name == team_name}.first
33
- return "No team named #{team_name}" unless team
32
+ def team_list
33
+ @teams.map{|r| [r.name, r.team_id]}
34
+ end
34
35
 
35
- find_players(team.players, str) || "#{str} cannot be found in #{team_name}'s roster"
36
- end
36
+ #pull up a Player object based on their name and their team
37
37
 
38
- private
38
+ def findplayer(team_name, str)
39
+ team = teams.select{|t| t.name == team_name}.first
40
+ return "No team named #{team_name}" unless team
39
41
 
40
- def make_team_objects
41
- @data['teams'].map{|team| Team.new(team, self)}
42
- end
42
+ find_players(team.players, str) || "#{str} cannot be found in #{team_name}'s roster"
43
+ end
44
+
45
+ private
43
46
 
44
- def make_stat_data
45
- stats = {}
46
- teams.each do |team|
47
- team.teamstats.each do |stat, value|
48
- stats[stat] ? stats[stat] << value : stats[stat] = [value]
47
+ def make_team_objects
48
+ @data['teams'].map{|team| Team.new(team, self)}
49
+ end
50
+
51
+ def make_stat_data
52
+ stats = {}
53
+ teams.each do |team|
54
+ team.teamstats.each do |stat, value|
55
+ stats[stat] ? stats[stat] << value : stats[stat] = [value]
56
+ end
49
57
  end
58
+ calculate_league_stats(stats)
59
+ end
60
+
61
+ def calculate_league_stats(s)
62
+ calc = {}
63
+ s.each do |stat, values|
64
+ avg = (values.sum/values.length).to_f
65
+ calc["#{stat} Average"] = avg
66
+ calc["#{stat} Standard Deviation"] = std_dev(avg, values)
67
+ end
68
+ calc
50
69
  end
51
- calculate_league_stats(stats)
52
- end
53
70
 
54
- def calculate_league_stats(s)
55
- calc = {}
56
- s.each do |stat, values|
57
- avg = (values.sum/values.length).to_f
58
- calc["#{stat} Average"] = avg
59
- calc["#{stat} Standard Deviation"] = std_dev(avg, values)
71
+ def std_dev(mean, values)
72
+ sum = values.inject(0){|accum, i| accum +(i-mean)**2 }
73
+ variance = sum/(values.length - 1).to_f
74
+ Math.sqrt(variance)
60
75
  end
61
- calc
62
- end
63
76
 
64
- def std_dev(mean, values)
65
- sum = values.inject(0){|accum, i| accum +(i-mean)**2 }
66
- variance = sum/(values.length - 1).to_f
67
- Math.sqrt(variance)
68
77
  end
69
-
70
78
  end
@@ -1,39 +1,41 @@
1
1
  require_relative 'constants'
2
2
  require_relative 'calculate_stats'
3
3
 
4
- class Player
4
+ module ESPNNBAFantasy
5
+ class Player
5
6
 
6
- include CalculateStats
7
+ include CalculateStats
7
8
 
8
- attr_reader :name, :player_id, :position, :stats, :team
9
+ attr_reader :name, :player_id, :position, :stats, :team
9
10
 
10
- #basic initialization with attributes
11
+ #basic initialization with attributes
11
12
 
12
- def initialize(player, team)
13
- @name = player['fullName']
14
- @player_id = player['id']
15
- @position = POSITION_MAP[player['defaultPositionId']-1]
16
- @stats = stat_card_maker(player)
17
- @team = team
18
- end
13
+ def initialize(player, team)
14
+ @name = player['fullName']
15
+ @player_id = player['id']
16
+ @position = POSITION_MAP[player['defaultPositionId']-1]
17
+ @stats = stat_card_maker(player)
18
+ @team = team
19
+ end
19
20
 
20
- private
21
+ def to_s
22
+ "Player: #{name} | Position: #{position} | Stats: #{stats}"
23
+ end
21
24
 
22
- def to_s
23
- "Player: #{name} | Position: #{position} | Stats: #{stats}"
24
- end
25
+ private
25
26
 
26
- def stat_card_maker(player)
27
- avgstats = player['stats'].first['averageStats']
28
- statcard = avgstats ? avgstats.transform_keys!{ |k| STATS_MAP[k] }.slice(*STATS) : EMPTY_STATS
29
- calculate_stats(statcard)
30
- end
27
+ def stat_card_maker(player)
28
+ avgstats = player['stats'].first['averageStats']
29
+ statcard = avgstats ? avgstats.transform_keys!{ |k| STATS_MAP[k] }.slice(*STATS) : EMPTY_STATS
30
+ calculate_stats(statcard)
31
+ end
31
32
 
32
- def calculate_stats(s)
33
- s['FGA'] = s['FGA'] + 0.0001
34
- s['TO'] = s['TO'] + 0.00001
35
- s['FTA'] = s['FTA'] + 0.00001
36
- add_calculated_stats(s)
37
- end
33
+ def calculate_stats(s)
34
+ s['FGA'] = s['FGA'] + 0.0001
35
+ s['TO'] = s['TO'] + 0.00001
36
+ s['FTA'] = s['FTA'] + 0.00001
37
+ add_calculated_stats(s)
38
+ end
38
39
 
40
+ end
39
41
  end
@@ -3,73 +3,75 @@ require_relative 'player'
3
3
  require_relative 'calculate_stats'
4
4
  require_relative 'player_finder'
5
5
 
6
- class Team
6
+ module ESPNNBAFantasy
7
+ class Team
7
8
 
8
- include CalculateStats
9
- include PlayerFinder
9
+ include CalculateStats
10
+ include PlayerFinder
10
11
 
11
- attr_accessor :roster, :players, :name, :teamstats, :league, :team_id
12
+ attr_accessor :roster, :players, :name, :teamstats, :league, :team_id
12
13
 
13
- #basic initialization with the various attributes
14
+ #basic initialization with the various attributes
14
15
 
15
- def initialize(obj, league)
16
- @name = obj['name']
17
- @team_id = obj['id']
18
- @players = roster_maker(obj)
19
- @roster = roster_names
20
- @teamstats = make_team_stats(@players)
21
- @league = league
22
- end
16
+ def initialize(obj, league)
17
+ @name = obj['name']
18
+ @team_id = obj['id']
19
+ @players = roster_maker(obj)
20
+ @roster = roster_names
21
+ @teamstats = make_team_stats(@players)
22
+ @league = league
23
+ end
23
24
 
24
- #method that lets you input who you're trading (Array of names), who you're receiving (Array of names),
25
- #and what team you're trading with (String).
26
-
27
- #returns data including the players trading away and receiving, your stats before and after the trade, and your
28
- #change in stats for each category
29
-
30
- def trade_players(to_trade = [], to_receive = [], other_team_name)
31
- other_team = league.teams.select{|team| team.name == other_team_name}.first
32
- return "No team named #{other_team_name}" unless other_team
33
- own_players = to_trade.map{|player| find_players(players, player)}
34
- other_players = to_receive.map{|player| find_players(other_team.players, player)}
35
- return "This trade included players not one of the specified teams" if (own_players.include?(nil)|| other_players.include?(nil))
36
- {'Trading:' => own_players.map{|p| p.name},
37
- 'Receiving:' => other_players.map{|p| p.name}}.merge(new_team_stats(own_players, other_players))
38
-
39
- end
25
+ #method that lets you input who you're trading (Array of names), who you're receiving (Array of names),
26
+ #and what team you're trading with (String).
40
27
 
41
- private
28
+ #returns data including the players trading away and receiving, your stats before and after the trade, and your
29
+ #change in stats for each category
42
30
 
43
- def to_s
44
- "Team Name: #{name} | Roster: #{roster_names}"
45
- end
31
+ def to_s
32
+ "Team Name: #{name} | Roster: #{roster_names}"
33
+ end
46
34
 
47
- def roster_names
48
- players.map(&:name)
49
- end
35
+ def trade_players(to_trade = [], to_receive = [], other_team_name)
36
+ other_team = league.teams.select{|team| team.name == other_team_name}.first
37
+ return "No team named #{other_team_name}" unless other_team
38
+ own_players = to_trade.map{|player| find_players(players, player)}
39
+ other_players = to_receive.map{|player| find_players(other_team.players, player)}
40
+ return "This trade included players not one of the specified teams" if (own_players.include?(nil)|| other_players.include?(nil))
41
+ {'Trading:' => own_players.map{|p| p.name},
42
+ 'Receiving:' => other_players.map{|p| p.name}}.merge(new_team_stats(own_players, other_players))
43
+
44
+ end
50
45
 
51
- def stat_differences(old, new)
52
- new.map {|stat, val| [stat, val - old[stat]]}.to_h
53
- end
46
+ private
54
47
 
55
- def new_team_stats(own, others)
56
- new_ros = players.dup
57
- others.each{|other| new_ros << other}
58
- own.each {|p| new_ros.delete_at(new_ros.index(p))}
59
- newstats = make_team_stats(new_ros)
60
- {'Before:' => teamstats, 'After:' => newstats, 'Changes:' => stat_differences(teamstats, newstats) }
61
- end
48
+ def roster_names
49
+ players.map(&:name)
50
+ end
62
51
 
63
- def roster_maker(obj)
64
- obj['roster']['entries'].map {|e| Player.new(e['playerPoolEntry']['player'], self)}
65
- end
52
+ def stat_differences(old, new)
53
+ new.map {|stat, val| [stat, val - old[stat]]}.to_h
54
+ end
66
55
 
67
- def make_team_stats(ros)
68
- stats = {}
69
- ros.each do |player|
70
- player.stats.each {|stat, value| stats[stat] ? stats[stat] += value : stats[stat] = value}
56
+ def new_team_stats(own, others)
57
+ new_ros = players.dup
58
+ others.each{|other| new_ros << other}
59
+ own.each {|p| new_ros.delete_at(new_ros.index(p))}
60
+ newstats = make_team_stats(new_ros)
61
+ {'Before:' => teamstats, 'After:' => newstats, 'Changes:' => stat_differences(teamstats, newstats) }
71
62
  end
72
- add_calculated_stats(stats)
73
- end
74
63
 
64
+ def roster_maker(obj)
65
+ obj['roster']['entries'].map {|e| Player.new(e['playerPoolEntry']['player'], self)}
66
+ end
67
+
68
+ def make_team_stats(ros)
69
+ stats = {}
70
+ ros.each do |player|
71
+ player.stats.each {|stat, value| stats[stat] ? stats[stat] += value : stats[stat] = value}
72
+ end
73
+ add_calculated_stats(stats)
74
+ end
75
+
76
+ end
75
77
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: espn_nba_fantasy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Usborn Ocampo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-18 00:00:00.000000000 Z
11
+ date: 2024-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  requirements: []
116
- rubygems_version: 3.4.20
116
+ rubygems_version: 3.5.13
117
117
  signing_key:
118
118
  specification_version: 4
119
119
  summary: Get info from your ESPN NBA Fantasy League as JSON data