npb_flash 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: a74af633053bf2f69eb183d931840c3d6850086d
4
+ data.tar.gz: 31b8161c470e9b06ac053c49261865e8952f540d
5
+ SHA512:
6
+ metadata.gz: 385ca8054f5604447c3f6ae729fd0b38d580a36c146f1e2afacb0c1dbf2b6fe84c50b77b3b1a4cb3f32810249a83e0a97eaa0a566d0d768d04b8280fc111bc27
7
+ data.tar.gz: 595ef56c225a5951b4d7cb70e20396dbb42fb414181d0da3a49524790e51e8556624abeba838faa7f1652fd44947e1f9a9d35fc992a67d19066b1a0662949631
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/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ - 2.1.6
5
+ deploy:
6
+ provider: rubygems
7
+ gems: npb_flash
8
+ on:
9
+ tags: true
10
+ api_key:
11
+ secure: iJ0QARCo42CLBLZfbwrizUTmYFdCMtWsOEFNaH+hqShToCvV8ql7yb1UCHI3v6DNCjfQG6kd6cAHikKMjycKupHY1JhvqZq5gQoglIAd61vm4O/B89aodt7rYnTWOy9XM0lO1adG4T/4Mwm1KMuMlSgWcclI74R9Khazc0qrhDw=
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in npb_flash.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # NpbFlash
2
+
3
+ A scraping library for [SportsNaviプロ野球](http://baseball.yahoo.co.jp/npb/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'npb_flash'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install npb_flash
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ crawler = NpbFlash::Crawler.new(Date.new(2015, 4, 25))
25
+
26
+ game = crawler.get_game_by_team('L')
27
+
28
+ game.innings.each do |inning|
29
+ p inning.title
30
+ inning.events.each do |event|
31
+ p event
32
+ end
33
+ end
34
+ ```
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/[my-github-username]/npb_flash/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "npb_flash"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/npb_flash.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'open-uri'
2
+ require 'date'
3
+ require 'nokogiri'
4
+ require 'npb_flash/crawler'
5
+ require "npb_flash/version"
6
+
7
+ module NpbFlash
8
+ end
@@ -0,0 +1,49 @@
1
+ require 'npb_flash/game'
2
+ require 'npb_flash/inning'
3
+
4
+ module NpbFlash
5
+
6
+ class Crawler
7
+
8
+ def initialize(date = Date::today)
9
+ @date = date
10
+ end
11
+
12
+ def get_metadata
13
+ uri = 'http://baseball.yahoo.co.jp/npb/schedule/?date=' + @date.strftime('%Y%m%d')
14
+ html = open(uri) do |f|
15
+ f.read
16
+ end
17
+ doc = Nokogiri::HTML.parse(html, nil, 'utf-8')
18
+ doc.xpath('//div[@id="gm_sch"]//table[@class="teams"]').map do |g|
19
+ visitor, home = g.xpath('.//tr//th[1]//a/div/@class').map do |s|
20
+ s.text.split(/\s/)[1]
21
+ end
22
+ id = g.xpath('.//table[@class="score"]//tr[2]//a')[0]['href'].match(/[\d]+/)[0]
23
+ {
24
+ id: id,
25
+ home: home,
26
+ visitor: visitor
27
+ }
28
+ end
29
+ end
30
+
31
+ def get_game(id)
32
+ uri = 'http://baseball.yahoo.co.jp/npb/game/' + id + '/text'
33
+ html = open(uri) do |f|
34
+ f.read
35
+ end
36
+ doc = Nokogiri::HTML.parse(html, nil, 'utf-8')
37
+ Game::from_node(doc, id)
38
+ end
39
+
40
+ def get_game_by_team(team)
41
+ meta = get_metadata.find do |m|
42
+ m[:home] == team or m[:visitor] == team
43
+ end
44
+ get_game(meta[:id])
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,28 @@
1
+ module NpbFlash
2
+
3
+ class Game
4
+
5
+ attr_accessor :home, :visitor, :innings
6
+
7
+ def initialize(id)
8
+ @id = id
9
+ @innings = []
10
+ end
11
+
12
+ def self.from_node(node, id)
13
+ game = Game.new(id)
14
+
15
+ game.visitor, game.home = node.xpath('//div[@id="txt_ibd"]//table/tbody/tr[position() = 2 or position() = 3]/th/@class').map do |t|
16
+ t.text
17
+ end
18
+
19
+ game.innings = node.xpath('//div[@id="txt_live"]//div[contains(concat(" ", @class, " "), " item ")]').map do |i|
20
+ Inning::from_node(i)
21
+ end
22
+
23
+ game
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,66 @@
1
+ module NpbFlash
2
+
3
+ class Inning
4
+ include Comparable
5
+
6
+ attr_accessor :events, :id, :title
7
+
8
+ def initialize(id, title, team)
9
+ @id = id
10
+ @title = title
11
+ @team = team
12
+ @events = []
13
+ end
14
+
15
+ def inning_index
16
+ @id.match(/[\d]+$/)[0].to_i
17
+ end
18
+
19
+ def top?
20
+ @id.match(/^[i]+/)[0].size == 1
21
+ end
22
+
23
+ def bottom?
24
+ not self.top?
25
+ end
26
+
27
+ def inning
28
+ [self.inning_index, self.top? ? :top : :bottom]
29
+ end
30
+
31
+ def <=>(other)
32
+ e = self.inning_index <=> other.inning_index
33
+ if e == 0
34
+ if self.top?
35
+ if other.top?
36
+ e = 0
37
+ else
38
+ e = -1
39
+ end
40
+ else
41
+ if other.top?
42
+ e = 1
43
+ else
44
+ e = 0
45
+ end
46
+ end
47
+ end
48
+ e
49
+ end
50
+
51
+ def self.from_node(node)
52
+ id = node['id']
53
+ title = node.xpath('.//h3/b/text()')[0].text
54
+ team = node['class'].split(' ')[1]
55
+ inning = Inning.new(id, title, team)
56
+ node.xpath('./ul/li').each do |li|
57
+ raw_text = li.text
58
+ text_index, text = raw_text.split(':', 2)
59
+ inning.events.push(text)
60
+ end
61
+ inning
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,3 @@
1
+ module NpbFlash
2
+ VERSION = "0.1.0"
3
+ end
data/npb_flash.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'npb_flash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "npb_flash"
8
+ spec.version = NpbFlash::VERSION
9
+ spec.authors = ["Shun Takebayashi"]
10
+ spec.email = ["shun@takebayashi.asia"]
11
+
12
+ spec.summary = %q{Scraping library for NPB score flash news of Yahoo! JAPAN SportsNavi}
13
+ spec.homepage = "https://github.com/takebayashi/ruby-npb_bis"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.9"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "minitest"
23
+ spec.add_dependency "nokogiri"
24
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: npb_flash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shun Takebayashi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - shun@takebayashi.asia
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/npb_flash.rb
84
+ - lib/npb_flash/crawler.rb
85
+ - lib/npb_flash/game.rb
86
+ - lib/npb_flash/inning.rb
87
+ - lib/npb_flash/version.rb
88
+ - npb_flash.gemspec
89
+ homepage: https://github.com/takebayashi/ruby-npb_bis
90
+ licenses: []
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Scraping library for NPB score flash news of Yahoo! JAPAN SportsNavi
112
+ test_files: []