Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: crsven/score_watch
base: 864fa11f04
...
head fork: crsven/score_watch
compare: ea8273e309
  • 20 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
4 .gitignore
View
@@ -0,0 +1,4 @@
+*.sh
+*.gem
+*.swp
+*.swo
5 bin/score_watch
View
@@ -0,0 +1,5 @@
+#!/usr/bin/ruby
+
+require File.dirname(__FILE__) + '/../lib/ScoreWatch'
+
+ScoreWatch::Match.new ARGV[0]
BIN  lib/.ScoreWatch.rb.swp
View
Binary file not shown
10 lib/ScoreWatch.rb
View
@@ -0,0 +1,10 @@
+#!/usr/bin/ruby
+
+require 'open-uri'
+require 'nokogiri'
+
+module ScoreWatch
+ VERSION = "0.2"
+end
+
+require 'lib/ScoreWatch/match'
BIN  lib/ScoreWatch/.match.rb.swp
View
Binary file not shown
125 lib/ScoreWatch/match.rb
View
@@ -0,0 +1,125 @@
+class ScoreWatch::Match
+ GROWL_OPTIONS = "-n 'Score Watch'"
+ PING_TIME = 30
+
+ def initialize(id)
+ @match_id = id
+ @match_url = "http://soccernet.espn.go.com/gamecast?id=#{@match_id}"
+ @current_time = nil
+ @old_score = "0 - 0"
+ @current_score = nil
+ @game_on = false
+ @status = nil
+ get_teams
+ watch
+ end
+
+ def watch
+ refresh
+ get_score
+ get_status
+ if @old_score != @current_score
+ message("#{@home_team} #{@current_score} #{@away_team}")
+ comment = get_goal_comment
+ if comment
+ message(comment)
+ end
+ end
+ wait
+ watch
+ end
+
+ private
+
+ def refresh
+ @html = Nokogiri::HTML(open(@match_url))
+ @current_time = get_current_time
+ end
+
+ def get_teams
+ refresh
+ @home_team = @html.css(".team.home h3").first.content
+ @away_team = @html.css(".team.away h3").first.content
+ end
+
+ def get_score
+ @old_score = @current_score
+ scoreline = @html.css(".matchup-score").first.content
+ scoreline.gsub("?","")
+ @current_score = scoreline
+ end
+
+ def get_goal_comment
+ refresh
+ goal_text = @html.css(".select-comment").first
+ goal_comment = ""
+ if goal_text
+ goal_comment = goal_text.css(".comment").first.content
+ end
+
+ if goal_comment.strip! == ""
+ return nil
+ end
+ end
+
+ def get_status
+ if @game_on == true
+ if is_over?
+ end_match
+ end
+ return
+ elsif is_started?
+ message("Match has started!")
+ @game_on = true
+ elsif is_over?
+ end_match
+ else
+ message("Waiting for match to start.")
+ end
+ end
+
+ def wait
+ sleep PING_TIME
+ end
+
+ def is_started?
+ refresh
+ clock = @html.css("##{@match_id.to_s}clock").first
+ if clock['style'].include?("display:none")
+ return false
+ else
+ return true
+ end
+ end
+
+ def get_current_time
+ clock = @html.css("##{@match_id.to_s}clock").first.content
+ time = clock.gsub(" ","").gsub("-","").gsub("'","")
+ end
+
+ def is_over?
+ refresh
+ timeStatus = @html.css("##{@match_id.to_s}statusTabText").first.content
+ timeStatus.strip!
+ if timeStatus == "Full-time"
+ return true
+ else
+ return false
+ end
+ end
+
+ def end_match
+ message("Match ended! Final score: #{@home_team} #{@current_score} #{@away_team}")
+ exit
+ end
+
+ def growl(text)
+ growl_input = "#{GROWL_OPTIONS} -m '#{text}'"
+ system("growlnotify #{growl_input}")
+ end
+
+ def message(text)
+ growl(text)
+ puts(text)
+ end
+end
14 score_watch.gemspec
View
@@ -0,0 +1,14 @@
+require 'lib/ScoreWatch'
+
+Gem::Specification.new do |s|
+ s.name = 'ScoreWatch'
+ s.version = ScoreWatch::VERSION
+ s.summary = 'Watches ESPN Gamecast pages for updates and broadcasts those updates.'
+ s.description = 'Given an ESPN Gamecast match ID, Score Watch will scrape the Gamecast Page for updates and broadcast them on the command line and also via GrowlNotify.'
+ s.authors = ["Chris Svenningsen"]
+ s.email = 'crsven@gmail.com'
+ s.executables = ["score_watch"]
+ s.files = Dir["{lib}/**/*", "[A-Z]*"] - ["Gemfile.lock"]
+ s.homepage = 'http://www.crsvenningsen.com/projects'
+ s.require_path = 'lib'
+end
76 score_watch.rb
View
@@ -1,76 +0,0 @@
-#!/usr/bin/ruby
-
-require 'open-uri'
-require 'nokogiri'
-
-url = "http://soccernet.espn.go.com/gamecast?id="
-game = ARGV[0]
-url+=game
-
-old_score = "0 - 0"
-
-html = Nokogiri::HTML(open(url))
-home_team = html.css(".team.home h3").first.content
-away_team = html.css(".team.away h3").first.content
-
-# If not started, wait to announce start
-started = FALSE
-waiting = FALSE
-while started == FALSE do
- clock = html.css("##{game.to_s}clock").first
- if clock['style'].include?("display:none") && waiting == FALSE
- puts "Waiting for match to start..."
- waiting = TRUE
- sleep 30
- elsif clock['style'].include?("display:none") && waiting == TRUE
- sleep 30
- else
- start_message = "Match has started: #{home_team} v #{away_team}"
- puts start_message
- growl_input = "-n 'Gamecast' -m '#{start_message}'"
- system("growlnotify #{growl_input}")
- started = TRUE
- waiting = FALSE
- game_on = TRUE
- end
-end
-
-while game_on == TRUE do
- # Open gamecast page
- html = Nokogiri::HTML(open(url))
-
- # Check status of match
- timeStatus = html.css("##{game.to_s}statusTabText").first.content
- timeStatus.strip!
- # If Full Time, end watcher
- if timeStatus == "Full-time"
- game_on = FALSE
- puts "All over!"
- growl_input = "-n 'Gamecast' -m 'Game over!'"
- system("growlnotify #{growl_input}")
- exit
- end
-
- time = html.css("##{game.to_s}clock").first.content
- time = time.gsub(" ","").gsub("-","").gsub("'","")
-
- scoreline = html.css(".matchup-score").first.content
- scoreline.gsub("?","")
- if scoreline != old_score
- puts "#{time}': #{home_team} #{scoreline} #{away_team}"
- growl_input = "-n 'Gamecast' -m '#{home_team} v. #{away_team} #{time}':\nScore was #{old_score}, but is now #{scoreline}'"
- system("growlnotify #{growl_input}")
- old_score = scoreline
- goal_comment = html.css(".select-comment").first
- goal_text = ""
- if goal_comment
- goal_comment.css("b").each do |text|
- goal_text+=text.content
- end
- puts "#{goal_text}\n"
- growl_input = "-n 'Gamecast' -m '#{goal_text}'"
- system("growlnotify #{growl_input}")
- end
- end
- sleep 30
-end

No commit comments for this range

Something went wrong with that request. Please try again.