-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoreboard
executable file
·218 lines (169 loc) · 3.62 KB
/
scoreboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
require 'open-uri'
Bundler.require
class Scoreboard
def initialize(json)
@json = json
end
def title
event['title']
end
def status
event['status']
end
def start_time
Time.parse(event['start_time'])
end
def period
event['period']
end
def time_remaining
event['time_remaining']
end
def columns
event['score']['cols']
end
def home_team
@home_team ||= Team.new(
name: event['home_team']['name'],
scores: event['score']['home']
)
end
def away_team
@away_team ||= Team.new(
name: event['away_team']['name'],
scores: event['score']['away']
)
end
def teams
[home_team, away_team]
end
def date
@date ||= @html.css('.m-scoreboard__box-score_title').text
end
private
def event
@json['events'].first
end
class Team
attr_reader :name, :scores
def initialize(name:, scores:)
@name = name
@scores = scores
end
def runs
scores[-3].to_i
end
end
end
class Printer
FIRST_COLUMN_PADDING = 3
COLUMN_PADDING = 2
def initialize(board)
@board = board
end
def to_s
[
header,
team(@board.away_team),
team(@board.home_team),
].map(&:to_s).join("\n")
end
private
def header
s = " " * max_team_name_length
s += first_column_padding
s += @board.columns.each_with_index.map { |column, index|
pad(column, max_column_width(index)).colorize(column_header_color(index))
}.join(column_padding)
end
def team(team)
color = team.name =~ /mariners/i ? :light_blue : :light_red
s = pad(team.name, max_team_name_length).colorize(color)
s += first_column_padding
team.scores.each_with_index do |score, index|
s += pad(score, max_column_width(index))
.colorize(column_score_color(index, team))
s += column_padding
end
s
end
def pad(string, column_width)
spaces = column_width - string.length
string + (" " * spaces)
end
def winning?(team)
other = other_team(team)
team.runs > other.runs
end
def other_team(team)
@board.teams.reject { |t| t == team }.first
end
def column_padding
" " * COLUMN_PADDING
end
def column_header_color(index)
if index >= @board.columns.length - 3
:blue
else
:yellow
end
end
def column_score_color(index, team)
if index == @board.columns.length - 3 && winning?(team)
:green
else
:white
end
end
def first_column_padding
" " * FIRST_COLUMN_PADDING
end
def max_team_name_length
length = [
@board.away_team.name,
@board.home_team.name
].map { |name| name.length }.max
end
def max_column_width(index)
columns = [
@board.away_team.scores[index],
@board.home_team.scores[index],
@board.columns[index]
]
columns.map { |col| col.length }.max
end
end
def scoreboard_id
html = open("http://www.lookoutlanding.com").read
doc = Nokogiri::HTML(html)
scoreboard = doc.css(".m-scoreboard")
if scoreboard.empty?
nil
else
scoreboard.attr("data-scoreboard-event-id").value
end
end
def scoreboard_json(id)
json = open(
"http://www.lookoutlanding.com/sbn_scoreboard/"\
"ajax_leagues_and_events?events%5B%5D=#{id}"
).read
JSON.parse(json)
end
def passed_in_board_id
unless ARGV.nil? || ARGV.first == ""
ARGV.first
end
end
board_id = passed_in_board_id || scoreboard_id
unless board_id
puts "No game in progress right now."
exit
else
board = Scoreboard.new(scoreboard_json(board_id))
printer = Printer.new(board)
puts printer.to_s
end