score-scraper 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a64875de25f681592d6a1731c4d35753664c66f
4
+ data.tar.gz: c094900d1123a0bed32970cb68bb2df06d5c66b3
5
+ SHA512:
6
+ metadata.gz: 11dbfe293a878259958478b970e510bc83c79fcf90a5838d5d73c249d3174176c3f3a81e56026b12cd74812a008c8c6bcbfb02751ee598c73e652029cb7f5b37
7
+ data.tar.gz: a1cdcfcf5c978ee06b7cea07962c78d29444add74c93bc506f776e5e74d564d32d4f23598702181e284213e5913e6e9f6f159dd24d487c14efe83267cf02f254
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in score-scraper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Derrick Neier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # score-scraper
2
+
3
+ This gem is used to retrieve NBA or MLB scores. Please note I do not own any of the information provided by this gem, all nba scores are retrieved from a publicly available json file on nba.com and all mlb scores are are retreived from a publicly available json file on mlb.com. It is entirely possible that these files will change and this gem will break, if that happens I will do my best to keep up with any changes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'score-scraper'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install score-scraper
20
+
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'score-scraper'
26
+ ```
27
+
28
+ ## Development
29
+
30
+ Contributions are welcome, feel free to expand on current functionality or add new leagues.
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/DNeier/score-scraper.
35
+
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
4
+
5
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "score/scraper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ module ScoreScraper
2
+ class Game
3
+ attr_reader :home_team, :away_team, :game_state
4
+
5
+ def initialize(attrs)
6
+ @home_team = attrs[:home_team] || {}
7
+ @away_team = attrs[:away_team] || {}
8
+ @game_state = attrs[:game_state] || ''
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ module ScoreScraper
2
+ class Scoreboard
3
+
4
+ def initialize(search_date = nil, teams = [])
5
+ @teams = teams
6
+ @search_date = search_date || Time.now.strftime("%Y-%m-%d")
7
+ end
8
+
9
+ def games(game_separator = '|')
10
+ game_array = []
11
+
12
+ team_games.each do |game|
13
+ game_array << single_game(game)
14
+ end
15
+
16
+ game_array.join(game_separator)
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :search_date, :teams
22
+
23
+ def parser
24
+ raise 'must be implemented by child class'
25
+ end
26
+
27
+ def single_game(game)
28
+ home_team = game.home_team
29
+ away_team = game.away_team
30
+
31
+ " #{away_team.abbreviation} #{away_team.score} - #{home_team.score} #{home_team.abbreviation} #{game.game_state} "
32
+ end
33
+
34
+ def team_games
35
+ return @team_games unless @team_games.nil?
36
+
37
+ @team_games = parser.games_for_teams || {}
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,12 @@
1
+ module ScoreScraper
2
+ module Scoreboards
3
+ class Mlb < Scoreboard
4
+
5
+ private
6
+
7
+ def parser
8
+ @parser ||= Scrapers::Mlb.new(search_date, teams)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module ScoreScraper
2
+ module Scoreboards
3
+ class Nba < Scoreboard
4
+
5
+ private
6
+
7
+ def parser
8
+ @parser ||= Scrapers::Nba.new(search_date, teams)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module ScoreScraper
2
+ module Scoreboards
3
+ extend self
4
+
5
+ attr_reader :registered
6
+ @registered = []
7
+
8
+ def register(class_name, autoload_require)
9
+ ScoreScraper.autoload(class_name, autoload_require)
10
+ self.registered.push(class_name)
11
+ end
12
+
13
+ end
14
+ end
15
+
16
+ ScoreScraper::Scoreboards.register(:Nba, 'score_scraper/scoreboards/nba')
17
+ ScoreScraper::Scoreboards.register(:Mlb, 'score_scraper/scoreboards/mlb')
@@ -0,0 +1,98 @@
1
+ module ScoreScraper
2
+ class Scraper
3
+
4
+ def initialize(search_date = nil, teams = [])
5
+ @teams = teams
6
+ @search_date = search_date || Time.now.strftime("%Y-%m-%d")
7
+ end
8
+
9
+ def games_for_teams
10
+ team_games = []
11
+ (games || []).each do |game|
12
+ if teams.empty? || is_team_game?(game)
13
+ team_games << Games::Game.new({
14
+ home_team: home_team(game),
15
+ away_team: away_team(game),
16
+ game_state: game_state(game)
17
+ })
18
+ end
19
+ end
20
+
21
+ team_games
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :search_date, :teams
27
+
28
+ def search_day
29
+ @current_day ||= search_date.split('-')[2]
30
+ end
31
+
32
+ def search_month
33
+ @current_month ||= search_date.split('-')[1]
34
+ end
35
+
36
+ def search_year
37
+ @current_year ||= search_date.split('-')[0]
38
+ end
39
+
40
+ def formatted_url
41
+ raise 'Must be implemented in child class'
42
+ end
43
+
44
+ def games
45
+ raise 'Must be implemented in child class'
46
+ end
47
+
48
+ def abbreviation(game, is_home = false)
49
+ raise 'Must be implemented in child class'
50
+ end
51
+
52
+ def away_team(game)
53
+ get_game(game)
54
+ end
55
+
56
+ def city(game, is_home = false)
57
+ raise 'Must be implemented in child class'
58
+ end
59
+
60
+ def get_game(game, is_home = false)
61
+ Teams::Team.new({
62
+ city: city(game, is_home),
63
+ nickname: nickname(game, is_home),
64
+ abbreviation: abbreviation(game, is_home),
65
+ seore: score(game, is_home)
66
+ })
67
+ end
68
+
69
+ def game_state(game)
70
+ raise 'Must be implemented in child class'
71
+ end
72
+
73
+ def home_team(game)
74
+ get_game(game, true)
75
+ end
76
+
77
+ def nickname(game, is_home = false)
78
+ raise 'Must be implemented in child class'
79
+ end
80
+
81
+ def score(game, is_home = false)
82
+ raise 'Must be implemented in child class'
83
+ end
84
+
85
+ def is_team_game?(game)
86
+ teams.include?(get_abbreviation(game, true).downcase) || teams.include?(get_abbreviation(game).downcase)
87
+ end
88
+
89
+ def parsed_data
90
+ return @parsed_data unless @parsed_data.nil?
91
+
92
+ file = open(formatted_url)
93
+ file_data = file.read
94
+
95
+ @parsed_data = JSON.parse(file_data)
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,52 @@
1
+ module ScoreScraper
2
+ module Scrapers
3
+ class Mlb < Scraper
4
+
5
+ private
6
+
7
+ def formatted_url
8
+ @formatted_url ||= "http://chicago.cubs.mlb.com/gdcross/components/game/mlb/year_#{search_year}/month_#{search_month}/day_#{search_day}/master_scoreboard.json"
9
+ end
10
+
11
+ def games
12
+ @games ||= parsed_data['data']['games']['game']
13
+ end
14
+
15
+ def abbreviation(game, is_home = false)
16
+ game["#{key_type(is_home)}_name_abbrev"]
17
+ end
18
+
19
+ def city(game, is_home = false)
20
+ game["#{key_type(is_home)}_team_city"]
21
+ end
22
+
23
+ def game_state(game)
24
+ game_state = ''
25
+ game_status = game['status']['status']
26
+
27
+ if game_status.downcase == 'final' || game_status.downcase == 'game over'
28
+ game_state = 'F'
29
+ elsif game_status.downcase == 'preview'
30
+ game_state = game['time']
31
+ elsif game['status']['top_inning'] && game['status']['inning']
32
+ is_top_inning = game['status']['top_inning'].downcase == 'y'
33
+ game_state = is_top_inning ? "T#{game['status']['inning']}" : "B#{game['status']['inning']}"
34
+ end
35
+
36
+ game_state
37
+ end
38
+
39
+ def nickname(game, is_home = false)
40
+ game["#{key_type(is_home)}_team_name"]
41
+ end
42
+
43
+ def score(game, is_home = false)
44
+ game['linescore'] ? game['linescore']['r']["#{key_type(is_home)}"] : ''
45
+ end
46
+
47
+ def key_type(is_home = false)
48
+ is_home ? 'home' : 'away'
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,60 @@
1
+ module ScoreScraper
2
+ module Scrapers
3
+ class Nba < Scraper
4
+
5
+ private
6
+
7
+ def formatted_url
8
+ @formatted_url ||= "http://data.nba.com/jsonp/5s/json/cms/noseason/scoreboard/#{search_year}#{search_month}#{search_day}/games.json"
9
+ end
10
+
11
+ def games
12
+ @games ||= parsed_data['sports_content']['games']['game']
13
+ end
14
+
15
+ def abbreviation(game, is_home = false)
16
+ game["#{key_type(is_home)}"]['abbreviation']
17
+ end
18
+
19
+ def city(game, is_home = false)
20
+ game["#{key_type(is_home)}"]['city']
21
+ end
22
+
23
+ def game_state(game)
24
+ game_state = ''
25
+ game_status = game['period_time']['period_status']
26
+
27
+ if game_status.downcase == 'final'
28
+ game_state = 'F'
29
+ elsif game['period_time']['period_value'].to_i > 0
30
+ game_state = "#{game['period_time']['period_value']}Q"
31
+ else
32
+ game_state = game_status
33
+ end
34
+
35
+ game_state
36
+ end
37
+
38
+ def nickname(game, is_home = false)
39
+ game["#{key_type(is_home)}"]['nickname']
40
+ end
41
+
42
+ def score(game, is_home = false)
43
+ game["#{key_type(is_home)}"]['score']
44
+ end
45
+
46
+ def key_type(is_home = false)
47
+ is_home ? 'home' : 'visitor'
48
+ end
49
+
50
+ def parsed_data
51
+ return @parsed_data unless @parsed_data.nil?
52
+
53
+ file = open(formatted_url)
54
+ file_data = file.read
55
+
56
+ @parsed_data = JSON.parse(file_data[16...-2])
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,17 @@
1
+ module ScoreScraper
2
+ module Scrapers
3
+ extend self
4
+
5
+ attr_reader :registered
6
+ @registered = []
7
+
8
+ def register(class_name, autoload_require)
9
+ ScoreScraper.autoload(class_name, autoload_require)
10
+ self.registered.push(class_name)
11
+ end
12
+
13
+ end
14
+ end
15
+
16
+ ScoreScraper::Scrapers.register(:Nba, 'score_scraper/scrapers/nba')
17
+ ScoreScraper::Scrapers.register(:Mlb, 'score_scraper/scrapers/mlb')
@@ -0,0 +1,12 @@
1
+ module ScoreScraper
2
+ class Team
3
+ attr_reader :city, :nickname, :abbreviation, :score
4
+
5
+ def initialize(attrs)
6
+ @city = attrs[:city] || ''
7
+ @nickname = attrs[:nickname] || ''
8
+ @abbreviation = attrs[:abbreviation] || 'NIL'
9
+ @score = attrs[:score] || 0
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module ScoreScraper
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+
4
+ require "score_scraper/game"
5
+ require "score_scraper/scoreboard"
6
+ require "score_scraper/scoreboards"
7
+ require "score_scraper/scraper"
8
+ require "score_scraper/scrapers"
9
+ require "score_scraper/team"
10
+ require "score_scraper/version"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'score_scraper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "score-scraper"
8
+ spec.version = ScoreScraper::VERSION
9
+ spec.authors = ["Derrick Neier"]
10
+ spec.email = ["derrick.neier@gmail.com"]
11
+
12
+ spec.summary = %q{Retrieve NBA and MLB scores.}
13
+ spec.homepage = "https://github.com/DNeier/score-scraper"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "json", "~> 1.8"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: score-scraper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Derrick Neier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - derrick.neier@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/score_scraper.rb
86
+ - lib/score_scraper/game.rb
87
+ - lib/score_scraper/scoreboard.rb
88
+ - lib/score_scraper/scoreboards.rb
89
+ - lib/score_scraper/scoreboards/mlb.rb
90
+ - lib/score_scraper/scoreboards/nba.rb
91
+ - lib/score_scraper/scraper.rb
92
+ - lib/score_scraper/scrapers.rb
93
+ - lib/score_scraper/scrapers/mlb.rb
94
+ - lib/score_scraper/scrapers/nba.rb
95
+ - lib/score_scraper/team.rb
96
+ - lib/score_scraper/version.rb
97
+ - score-scraper.gemspec
98
+ - tasks/rspec.rake
99
+ homepage: https://github.com/DNeier/score-scraper
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.5.1
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Retrieve NBA and MLB scores.
123
+ test_files: []