npb_api 0.1.1 → 0.1.2
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 +4 -4
- data/lib/npb_api/hitting_stats.rb +129 -0
- data/lib/npb_api/pitching_stats.rb +77 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a2e67d3ccac6e050f0a33e2f89f8a5031e626c4
|
4
|
+
data.tar.gz: a7fabb597936315bd3d3dce7bfef2346e076d4d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60dc59e4b162d29ff9a8538998db0c0408654c832e18744da1f054f82309ecc65438684892f73c212213450e8887ee428c9528a12c1ac0b70467b58b38fa24ae
|
7
|
+
data.tar.gz: e1156de4db32c3af3333bcf787491034b4d618f80a5553772163cb8234e27c569c1a6d7eaa8547cdc06edfa3b0ea6af076569bd2728f69b3d7bfb59e50b5ef22
|
@@ -0,0 +1,129 @@
|
|
1
|
+
class NpbApi::HittingStats
|
2
|
+
def initialize(date: '', id: '')
|
3
|
+
@date = date
|
4
|
+
@game_id = date.year.to_s + format('%02d', date.month) + format('%02d', date.day) + id
|
5
|
+
@url = "http://baseball.yahoo.co.jp/npb/game/#{@game_id}/stats"
|
6
|
+
@doc = TableExtraction.new(url: @url, klass: 'yjS').doc
|
7
|
+
end
|
8
|
+
|
9
|
+
def away
|
10
|
+
team = Nokogiri::HTML(open(@url)).css('.pitcher h4')[0].children[0].text
|
11
|
+
stats(@doc[0]).map { |s| s.merge({ team: team }) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def home
|
15
|
+
team = Nokogiri::HTML(open(@url)).css('.pitcher h4')[1].children[0].text
|
16
|
+
stats(@doc[1]).map { |s| s.merge({ team: team }) }
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def stats(doc)
|
22
|
+
rows = doc.css('tr')
|
23
|
+
rows.shift
|
24
|
+
rows.pop
|
25
|
+
order = 0
|
26
|
+
rows.each_with_object([]) do |row, arr|
|
27
|
+
order += 1 if starting_lineup?(row)
|
28
|
+
arr << {
|
29
|
+
date: @date,
|
30
|
+
game_id: @game_id,
|
31
|
+
order: order,
|
32
|
+
starting_lineup: starting_lineup?(row),
|
33
|
+
position: position(row),
|
34
|
+
player: row.children[3].text,
|
35
|
+
player_id: player_id(row),
|
36
|
+
batting_average: row.children[5].text.to_f,
|
37
|
+
at_bats: row.children[7].text.to_i,
|
38
|
+
runs: row.children[9].text.to_i,
|
39
|
+
hits: row.children[11].text.to_i,
|
40
|
+
runs_batted_in: row.children[13].text.to_i,
|
41
|
+
strikeouts: row.children[15].text.to_i,
|
42
|
+
sacrifice_hits: row.children[19].text.to_i,
|
43
|
+
stolen_bases: row.children[21].text.to_i,
|
44
|
+
errors: row.children[23].text.to_i,
|
45
|
+
home_runs: row.children[25].text.to_i,
|
46
|
+
walks: walks(row),
|
47
|
+
hit_by_pitch: hit_by_pitch(row),
|
48
|
+
sacrifice_flies: sacrifice_flies(row),
|
49
|
+
double_plays: double_plays(row),
|
50
|
+
plate_appearances: plate_appearances(row),
|
51
|
+
doubles: doubles(row),
|
52
|
+
triples: triples(row),
|
53
|
+
total_bases: total_bases(row),
|
54
|
+
details: details(row),
|
55
|
+
}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def starting_lineup?(row)
|
60
|
+
row.children[1].text.match(/\A\(.+\)\z/) ? true : false
|
61
|
+
end
|
62
|
+
|
63
|
+
def position(row)
|
64
|
+
if row.children[1].text.match(/\A\(.+\)\z/)
|
65
|
+
row.children[1].text.match(/\A\((.+)\)\z/)
|
66
|
+
$1
|
67
|
+
else
|
68
|
+
row.children[1].text
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def player_id(row)
|
73
|
+
row.children[3].css('a').attribute('href').value =~ /\A\/npb\/player\?id=(\d+)\z/
|
74
|
+
$1
|
75
|
+
end
|
76
|
+
|
77
|
+
def walks(row)
|
78
|
+
details(row).values.count('四球')
|
79
|
+
end
|
80
|
+
|
81
|
+
def hit_by_pitch(row)
|
82
|
+
details(row).values.count('死球')
|
83
|
+
end
|
84
|
+
|
85
|
+
def sacrifice_flies(row)
|
86
|
+
details(row).values.count { |v| v.include?('犠飛') }
|
87
|
+
end
|
88
|
+
|
89
|
+
def double_plays(row)
|
90
|
+
details(row).values.count { |v| v.include?('併') }
|
91
|
+
end
|
92
|
+
|
93
|
+
def plate_appearances(row)
|
94
|
+
details(row).values.reject { |v| v == '' }.count
|
95
|
+
end
|
96
|
+
|
97
|
+
def doubles(row)
|
98
|
+
details(row).values.count { |v| v.include?('2') }
|
99
|
+
end
|
100
|
+
|
101
|
+
def triples(row)
|
102
|
+
details(row).values.count { |v| v.include?('3') }
|
103
|
+
end
|
104
|
+
|
105
|
+
def total_bases(row)
|
106
|
+
bases = details(row).values.count { |v| v.include?('安') }
|
107
|
+
bases += doubles(row) * 2
|
108
|
+
bases += triples(row) * 3
|
109
|
+
bases += details(row).values.count { |v| v.include?('本') } * 4
|
110
|
+
end
|
111
|
+
|
112
|
+
def details(row)
|
113
|
+
hash = {
|
114
|
+
first: row.children[27].text,
|
115
|
+
second: row.children[28].text,
|
116
|
+
third: row.children[29].text,
|
117
|
+
fourth: row.children[30].text,
|
118
|
+
fifth: row.children[31].text,
|
119
|
+
sixth: row.children[32].text,
|
120
|
+
seventh: row.children[33].text,
|
121
|
+
eighth: row.children[34].text,
|
122
|
+
ninth: row.children[35].text,
|
123
|
+
}
|
124
|
+
hash.merge!({ tenth: row.children[36].text }) if row.children[36]
|
125
|
+
hash.merge!({ eleventh: row.children[37].text }) if row.children[37]
|
126
|
+
hash.merge!({ twelfth: row.children[38].text }) if row.children[38]
|
127
|
+
hash
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
class NpbApi::PitchingStats
|
2
|
+
def initialize(date: '', id: '')
|
3
|
+
@date = date
|
4
|
+
@game_id = date.year.to_s + format('%02d', date.month) + format('%02d', date.day) + id
|
5
|
+
@url = "http://baseball.yahoo.co.jp/npb/game/#{@game_id}/stats"
|
6
|
+
@doc = TableExtraction.new(url: @url).doc
|
7
|
+
end
|
8
|
+
|
9
|
+
def away
|
10
|
+
team = Nokogiri::HTML(open(@url)).css('.pitcher h4')[0].children[0].text
|
11
|
+
stats(@doc[-2]).map { |s| s.merge({ team: team }) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def home
|
15
|
+
team = Nokogiri::HTML(open(@url)).css('.pitcher h4')[1].children[0].text
|
16
|
+
stats(@doc[-1]).map { |s| s.merge({ team: team }) }
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def stats(doc)
|
22
|
+
rows = doc.css('tr')
|
23
|
+
rows.shift
|
24
|
+
rows
|
25
|
+
order = 0
|
26
|
+
rows.each_with_object([]) do |row, arr|
|
27
|
+
order += 1
|
28
|
+
arr << {
|
29
|
+
date: @date,
|
30
|
+
game_id: @game_id,
|
31
|
+
order: order,
|
32
|
+
result: result(row),
|
33
|
+
player: row.children[3].text,
|
34
|
+
player_id: player_id(row),
|
35
|
+
earned_run_average: row.children[5].text.to_f,
|
36
|
+
innings_pitched: innings_pitched(row),
|
37
|
+
batters_faced: row.children[9].text.to_i,
|
38
|
+
number_of_pitches_thrown: row.children[11].text.to_i,
|
39
|
+
hits: row.children[13].text.to_i,
|
40
|
+
home_runs: row.children[15].text.to_i,
|
41
|
+
strikeouts: row.children[17].text.to_i,
|
42
|
+
hit_batsmen_and_bases_on_balls: row.children[19].text.to_i,
|
43
|
+
runs: row.children[21].text.to_i,
|
44
|
+
earned_runs: row.children[23].text.to_i,
|
45
|
+
}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def player_id(row)
|
50
|
+
row.children[3].css('a').attribute('href').value =~ /\A\/npb\/player\?id=(\d+)\z/
|
51
|
+
$1
|
52
|
+
end
|
53
|
+
|
54
|
+
def innings_pitched(row)
|
55
|
+
# \xc2\xa0 =>
|
56
|
+
if row.children[7].text =~ /(\d{1,2})\xc2\xa0(\d)\/3\z/
|
57
|
+
$1.to_i + Rational($2.to_i, 3)
|
58
|
+
else
|
59
|
+
row.children[7].text.to_i
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def result(row)
|
64
|
+
case row.children[1].text
|
65
|
+
when '勝'
|
66
|
+
'win'
|
67
|
+
when '負'
|
68
|
+
'lose'
|
69
|
+
when 'H'
|
70
|
+
'hold'
|
71
|
+
when 'S'
|
72
|
+
'save'
|
73
|
+
else
|
74
|
+
nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: npb_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ohtsuka Takahiro
|
@@ -60,6 +60,8 @@ extra_rdoc_files: []
|
|
60
60
|
files:
|
61
61
|
- lib/npb_api.rb
|
62
62
|
- lib/npb_api/game.rb
|
63
|
+
- lib/npb_api/hitting_stats.rb
|
64
|
+
- lib/npb_api/pitching_stats.rb
|
63
65
|
homepage: https://github.com/to0526/npb_api
|
64
66
|
licenses:
|
65
67
|
- MIT
|