mlb_terminal 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,15 +2,21 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  mlb_terminal (0.0.1)
5
- commander
6
- nokogiri
5
+ activesupport (~> 3.2.8)
6
+ commander (~> 4.1.2)
7
+ nokogiri (~> 1.5.5)
7
8
 
8
9
  GEM
9
10
  remote: http://rubygems.org/
10
11
  specs:
12
+ activesupport (3.2.8)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
11
15
  commander (4.1.2)
12
16
  highline (~> 1.6.11)
13
17
  highline (1.6.15)
18
+ i18n (0.6.1)
19
+ multi_json (1.3.6)
14
20
  nokogiri (1.5.5)
15
21
 
16
22
  PLATFORMS
data/bin/mlb CHANGED
@@ -8,8 +8,8 @@ require 'date'
8
8
  program :version, MLBTerminal::VERSION
9
9
  program :description, 'Stream MLB games into the terminal'
10
10
 
11
- command :list do |c|
12
- c.syntax = 'mlb list [options]'
11
+ command :games do |c|
12
+ c.syntax = 'mlb games [options] [date]'
13
13
  c.summary = 'Print out a list of scheduled games for the specified date'
14
14
  c.description = 'Print out a tab-seperated value list to STDOUT wih columns ' \
15
15
  'for index, team names, game status, and current score.'
@@ -30,14 +30,40 @@ command :list do |c|
30
30
  end
31
31
  end
32
32
 
33
- command :watch do |c|
34
- c.syntax = 'mlb watch [options]'
35
- c.summary = ''
36
- c.description = ''
33
+ command :game do |c|
34
+ c.syntax = 'mlb game [options] [game-number]'
35
+ c.summary = 'Print game events'
36
+ c.description = 'Print play-by-play action for the corresponding game. Columns: time, inning, balls/strikes/outs/description.'
37
37
  c.example 'description', 'command example'
38
- c.option '--some-switch', 'Some switch that does something'
38
+ c.option '--date STRING', String, 'Specify the date for the corresponding game number.'
39
39
  c.action do |args, options|
40
- # Do something or c.when_called Mlbterminal::Commands::Watch
40
+ options.default :date => Time.now.to_date.to_s
41
+
42
+ # Input validation.
43
+ date = (Date.parse(options.date) rescue Time.now.to_date)
44
+ if (Float(args.first) rescue nil).nil?
45
+ puts "Error: Game number must be a valid integer"
46
+ break
47
+ end
48
+
49
+ game_number = args.first.to_i
50
+ game_info = MLBTerminal::Client.list_games(date)[game_number]
51
+
52
+ if game_info.nil?
53
+ puts "Error: Game does not exist"
54
+ break
55
+ end
56
+
57
+ game = MLBTerminal::Client.new(game_info[:game_id])
58
+
59
+ game.events.each do |event|
60
+ puts [
61
+ event[:time].getlocal.strftime("%Y-%m-%d %H:%M:%S"),
62
+ event[:inning],
63
+ event[:num],
64
+ "#{event[:balls]}/#{event[:strikes]}/#{event[:outs]}",
65
+ event[:desc]].join("\t")
66
+ end
41
67
  end
42
68
  end
43
69
 
@@ -1,14 +1,15 @@
1
1
  require 'date'
2
2
  require 'nokogiri'
3
3
  require 'open-uri'
4
+ require 'active_support/core_ext/integer/inflections'
4
5
 
5
6
  module MLBTerminal
6
7
  MLB_BASE_URL = 'http://gd2.mlb.com/components/game/mlb'
7
8
 
8
9
  class Client
9
10
  def self.list_games(date = Time.now.to_date)
10
- doc = Nokogiri::HTML(open "#{base_url_for_date date}/scoreboard_iphone.xml")
11
- doc.xpath("//games/game").map{|game|
11
+ doc = Nokogiri::HTML(open "#{base_url_for_date date}/epg.xml")
12
+ doc.xpath("//epg/game").map{|game|
12
13
  {:home_team => {
13
14
  :name => game["home_team_name"],
14
15
  :wins => game["home_win"],
@@ -18,16 +19,67 @@ module MLBTerminal
18
19
  :wins => game["away_win"],
19
20
  :losses => game["away_loss"]},
20
21
  :score => {
21
- :home => (game.xpath("linescore/r").first["home"] rescue nil),
22
- :away => (game.xpath("linescore/r").first["away"] rescue nil)},
22
+ :home => game["home_team_runs"],
23
+ :away => game["away_team_runs"]},
23
24
  :starts => "#{game["time"]} #{game["time_zone"]}",
24
- :status => game.xpath("status").first["ind"]}}
25
+ :status => "#{game["status"]}" \
26
+ "#{game["status"] == "In Progress" ?
27
+ ", #{game["top_inning"]=="Y" ? "Top" : "Bot"} of #{game["inning"].to_i.ordinalize}" :
28
+ ""}",
29
+ :game_id => game["gameday"]}}
25
30
  end
26
31
 
27
- private
32
+ def initialize(gameday)
33
+ @gameday = gameday
34
+ year, month, day, game_name = /([0-9]{4})_([0-9]{2})_([0-9]{2})_(.*)/.match(@gameday).to_a.slice(1,4)
35
+ @base_url = "#{Client.base_url_for_date(Date.new(year.to_i, month.to_i, day.to_i))}/gid_#{@gameday}"
36
+ end
37
+
38
+ def events(start_inning = 1, end_inning = 9, delay = 5, &block)
39
+ current_inning = start_inning
40
+ current_inning_loc= "top"
41
+
42
+ innings = ["top", "bottom"]
43
+
44
+ Enumerator.new do |y|
45
+ while current_inning <= end_inning and not current_inning_loc.nil?
46
+ doc = Nokogiri::HTML(open "#{@base_url}/game_events.xml")
47
+
48
+ doc.xpath("//game/inning[@num >= #{current_inning}]").each do |inning|
49
+
50
+ current_inning_loc ||= "top"
51
+
52
+ innings.slice(innings.index(current_inning_loc), 2).each do |inning_loc|
53
+ if inning.xpath(inning_loc).nil?
54
+ break
55
+ else
56
+ inning.xpath("#{inning_loc}/atbat").each do |atbat|
57
+ y.yield({:inning => current_inning,
58
+ :num => atbat["num"],
59
+ :balls => atbat["b"],
60
+ :strikes => atbat["s"],
61
+ :outs => atbat["o"],
62
+ :time => Time.parse(atbat["start_tfs_zulu"], "%Y-%m-%dT%H:%M:%SZ"),
63
+ :desc => atbat["des"]})
64
+ end
65
+
66
+ current_inning_loc = innings[innings.index(current_inning_loc) + 1]
67
+ end
68
+ end
69
+ current_inning += 1
70
+ end
71
+
72
+ # If we had to break early, wait before trying again.
73
+ if current_inning != end_inning and not current_inning_loc.nil?
74
+ sleep delay
75
+ end
76
+
77
+ end
78
+ end.each(&block)
79
+ end
28
80
 
29
81
  def self.base_url_for_date(date = Time.now.to_date)
30
- "#{MLB_BASE_URL}/year_#{date.year}/month_#{"%02d" % date.month}/day_#{"%02d" % date.day}/"
82
+ "#{MLB_BASE_URL}/year_#{date.year}/month_#{"%02d" % date.month}/day_#{"%02d" % date.day}"
31
83
  end
32
84
  end
33
85
  end
@@ -1,3 +1,3 @@
1
1
  module MLBTerminal
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_dependency "commander"
21
- s.add_dependency "nokogiri"
20
+ s.add_dependency "commander", "~> 4.1.2"
21
+ s.add_dependency "nokogiri", "~> 1.5.5"
22
+ s.add_dependency "activesupport", "~> 3.2.8"
22
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mlb_terminal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,40 +9,56 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-29 00:00:00.000000000 Z
12
+ date: 2012-09-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: commander
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 4.1.2
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 4.1.2
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: nokogiri
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 1.5.5
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 1.5.5
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.8
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.8
46
62
  description: A small terminal app to translate MLB baseball feeds into the terminal
47
63
  for easy manipulation.
48
64
  email: