runners_update 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: efe74795320f6537438d59ac05ac67074bcb9d3e
4
+ data.tar.gz: cdaf1338274c63aeac6a1fb3cb1a34f2883739df
5
+ SHA512:
6
+ metadata.gz: ed65ed7c575eb6c619ab19e5b44f36a74d1d57418d3d9ea58837c6ca3f6610879ea25cd610677f14e305a707eb1eb14d6092ccd79ab047269a5abaa0a44eee71
7
+ data.tar.gz: 30a0b6d8ca3b534a09497acf54cc4a0b604c7a5c7c38ce912ea6af4be3f114783ecca4aefb491d2b4312c0ffd730b6eb0b9b46bd59fc635f4475113c91d4d01e
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ .bundle
2
+ .yardoc
3
+ .idea
4
+ .rubocop.yml
5
+ log
6
+ _yardoc
7
+ cache
8
+ coverage
9
+ doc
10
+ pkg
11
+ spec/reports
12
+ tmp
13
+ vendor/bundle
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.0.0
4
+ before_install: gem install bundler -v 1.10.0.pre.2
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'mechanize'
6
+
7
+ group :development do
8
+ gem 'rubocop'
9
+ gem 'guard'
10
+ gem 'guard-rubocop'
11
+ gem 'terminal-notifier-guard'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,113 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ runners_update (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.0.0)
10
+ astrolabe (1.3.1)
11
+ parser (~> 2.2)
12
+ coderay (1.1.0)
13
+ diff-lcs (1.2.5)
14
+ domain_name (0.5.24)
15
+ unf (>= 0.0.5, < 1.0.0)
16
+ ffi (1.9.10)
17
+ formatador (0.2.5)
18
+ guard (2.12.9)
19
+ formatador (>= 0.2.4)
20
+ listen (>= 2.7, <= 4.0)
21
+ lumberjack (~> 1.0)
22
+ nenv (~> 0.1)
23
+ notiffany (~> 0.0)
24
+ pry (>= 0.9.12)
25
+ shellany (~> 0.0)
26
+ thor (>= 0.18.1)
27
+ guard-rubocop (1.2.0)
28
+ guard (~> 2.0)
29
+ rubocop (~> 0.20)
30
+ http-cookie (1.0.2)
31
+ domain_name (~> 0.5)
32
+ listen (3.0.3)
33
+ rb-fsevent (>= 0.9.3)
34
+ rb-inotify (>= 0.9)
35
+ lumberjack (1.0.9)
36
+ mechanize (2.7.3)
37
+ domain_name (~> 0.5, >= 0.5.1)
38
+ http-cookie (~> 1.0)
39
+ mime-types (~> 2.0)
40
+ net-http-digest_auth (~> 1.1, >= 1.1.1)
41
+ net-http-persistent (~> 2.5, >= 2.5.2)
42
+ nokogiri (~> 1.4)
43
+ ntlm-http (~> 0.1, >= 0.1.1)
44
+ webrobots (>= 0.0.9, < 0.2)
45
+ method_source (0.8.2)
46
+ mime-types (2.6.1)
47
+ mini_portile (0.6.2)
48
+ nenv (0.2.0)
49
+ net-http-digest_auth (1.4)
50
+ net-http-persistent (2.9.4)
51
+ nokogiri (1.6.6.2)
52
+ mini_portile (~> 0.6.0)
53
+ notiffany (0.0.6)
54
+ nenv (~> 0.1)
55
+ shellany (~> 0.0)
56
+ ntlm-http (0.1.1)
57
+ parser (2.2.2.6)
58
+ ast (>= 1.1, < 3.0)
59
+ powerpack (0.1.1)
60
+ pry (0.10.1)
61
+ coderay (~> 1.1.0)
62
+ method_source (~> 0.8.1)
63
+ slop (~> 3.4)
64
+ rainbow (2.0.0)
65
+ rake (10.4.2)
66
+ rb-fsevent (0.9.5)
67
+ rb-inotify (0.9.5)
68
+ ffi (>= 0.5.0)
69
+ rspec (3.3.0)
70
+ rspec-core (~> 3.3.0)
71
+ rspec-expectations (~> 3.3.0)
72
+ rspec-mocks (~> 3.3.0)
73
+ rspec-core (3.3.2)
74
+ rspec-support (~> 3.3.0)
75
+ rspec-expectations (3.3.1)
76
+ diff-lcs (>= 1.2.0, < 2.0)
77
+ rspec-support (~> 3.3.0)
78
+ rspec-mocks (3.3.2)
79
+ diff-lcs (>= 1.2.0, < 2.0)
80
+ rspec-support (~> 3.3.0)
81
+ rspec-support (3.3.0)
82
+ rubocop (0.32.1)
83
+ astrolabe (~> 1.3)
84
+ parser (>= 2.2.2.5, < 3.0)
85
+ powerpack (~> 0.1)
86
+ rainbow (>= 1.99.1, < 3.0)
87
+ ruby-progressbar (~> 1.4)
88
+ ruby-progressbar (1.7.5)
89
+ shellany (0.0.1)
90
+ slop (3.6.0)
91
+ terminal-notifier-guard (1.6.4)
92
+ thor (0.19.1)
93
+ unf (0.1.4)
94
+ unf_ext
95
+ unf_ext (0.0.7.1)
96
+ webrobots (0.1.1)
97
+
98
+ PLATFORMS
99
+ ruby
100
+
101
+ DEPENDENCIES
102
+ bundler (~> 1.10.a)
103
+ guard
104
+ guard-rubocop
105
+ mechanize
106
+ rake (~> 10.0)
107
+ rspec
108
+ rubocop
109
+ runners_update!
110
+ terminal-notifier-guard
111
+
112
+ BUNDLED WITH
113
+ 1.10.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 kyoshidajp
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,86 @@
1
+ # RunnersUpdate
2
+
3
+ ランナーズアップデートのデータを取得します。ランナーズアップデートについては、[ランナーズアップデートとは - ランナーズアップデート](http://update.runnet.jp/2015chitose/help.html) を参照してください。
4
+
5
+ ## インストール
6
+
7
+ Gemfile に次の行を追加します。
8
+
9
+ ```ruby
10
+ gem 'runners_update', :git => 'https://github.com/kyoshidajp/runners_update.git'
11
+ gem 'mechanize'
12
+ ```
13
+
14
+ そして、次のコマンドを実行します。
15
+
16
+ $ bundle install
17
+
18
+ ## 使い方
19
+
20
+ 最初にレースIDを確認します。レースIDとは、レースのランナーズアップデート検索画面URLが
21
+
22
+ ```
23
+ http://update.runnet.jp/2015sibamata100/
24
+ ```
25
+
26
+ または
27
+
28
+ ```
29
+ http://update.runnet.jp/2015sibamata100/index.html
30
+ ```
31
+
32
+ の場合、`2015sibamata100` が該当します。
33
+
34
+ 次に、データを取得したいランナーのナンバーをレース公式サイトの参加者名簿等を参考にして調べます。ナンバーは一人の場合は `String` または `Fixnum`、複数名の場合は `Range` の可変長引数で指定可能です。
35
+
36
+ ソースコードでは `runners_update` を `require` します。
37
+
38
+ ```ruby
39
+ require 'runners_update'
40
+ ```
41
+
42
+ `RunnersUpdate` の `get` メソッドに対して、レースID、ナンバーを渡します。
43
+
44
+ ```ruby
45
+ result = RunnersUpdate.get(レースID, ナンバー)
46
+ ```
47
+
48
+ 結果は `RunnersUpdate::Runner` クラスの `Array` になっています。
49
+
50
+ ## サンプル
51
+
52
+ 次は2015年6月6日に行われた、[東京・柴又100K](http://tokyo100k.jp/)の結果について、ナンバー 1〜100, 1000〜1010 のデータを CSV 形式で出力するサンプル `sample.rb` です。
53
+
54
+ ```ruby
55
+ require 'runners_update'
56
+ require 'csv'
57
+
58
+ ID = '2015sibamata100/' # レースID
59
+
60
+ result = RunnersUpdate.get(ID, 1..100, 1000..1010)
61
+
62
+ CSV do |writer|
63
+ result.each do |runner|
64
+ points = []
65
+ runner.splits.each do |s|
66
+ points << s.split
67
+ end
68
+ writer << [runner.number, runner.name].concat(points)
69
+ end
70
+ end
71
+ ```
72
+
73
+ ```
74
+ $ bundle exec ruby sample.rb
75
+
76
+ ... CSV形式の結果
77
+
78
+ ```
79
+
80
+ ## ライセンス
81
+
82
+ - [MIT License](http://opensource.org/licenses/MIT)
83
+
84
+ ## 注意事項
85
+
86
+ [総合利用規約](http://runnet.jp/help/rule/detail_n6.html) の「第4条 財産権について」に記載されていますが、取得したデータを公開する事は規約に違反すると思われます。
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'runners_update'
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
@@ -0,0 +1,15 @@
1
+ require 'runners_update/version'
2
+ require 'runners_update/client'
3
+
4
+ # ランナーズアップデートクラス
5
+ module RunnersUpdate
6
+ # データの取得
7
+ #
8
+ # @param [String] race_id レースID
9
+ # @param [Array] *args ナンバー
10
+ # @return [Array] データ
11
+ def self.get(race_id, *args)
12
+ client = Client.new(race_id, *args)
13
+ return client.result
14
+ end
15
+ end
@@ -0,0 +1,184 @@
1
+ require 'logger'
2
+ require 'mechanize'
3
+ require 'runners_update/runner'
4
+
5
+ module RunnersUpdate
6
+ BASE_URL = 'http://update.runnet.jp/'
7
+ class Client
8
+ # キャッシュディレクトリ
9
+ CASHE_DIR = 'cache'
10
+
11
+ # ログ・ファイル
12
+ LOG = 'log'
13
+
14
+ # initialize
15
+ #
16
+ # @param [String] race_id レースID
17
+ # @param [Array] *args ナンバー
18
+ def initialize(race_id, *args)
19
+ @agent = Mechanize.new
20
+ @url = File.join(RunnersUpdate::BASE_URL, race_id)
21
+ @ranges = *args
22
+ @logger = Logger.new(LOG)
23
+ @cache_dir = File.join(Dir.pwd, CASHE_DIR, race_id)
24
+ end
25
+
26
+ # 結果データを取得
27
+ #
28
+ # @return [Array] 結果データ
29
+ def result
30
+ result = []
31
+
32
+ @ranges.each do |r|
33
+
34
+ if r.instance_of?(Fixnum) || r.instance_of?(String)
35
+ result << _result(r.to_s)
36
+ next
37
+ end
38
+
39
+ unless r.instance_of?(Range)
40
+ @logger.error("Illigal number #{r}")
41
+ next
42
+ end
43
+
44
+ r.each do |n|
45
+ runner = _result(n.to_s)
46
+ unless runner.nil?
47
+ result << runner
48
+ end
49
+ end
50
+ end
51
+
52
+ return result.compact
53
+ end
54
+
55
+ private
56
+
57
+ # キャッシュを取得
58
+ #
59
+ # @param [String] number ナンバー
60
+ # @return [Mechanize::Page|nil] 結果ページデータ
61
+ def cache(number)
62
+ number_file = File.join(@cache_dir, number + '.html')
63
+ if File.exist?(number_file)
64
+ @logger.info("Found cache at #{number_file}")
65
+ return @agent.get("file://#{number_file}")
66
+ end
67
+ return nil
68
+ end
69
+
70
+ # ランナーが存在するか
71
+ #
72
+ # @param [String] number ナンバー
73
+ # @return [Boolean] 存在するか
74
+ def exist_runner?(number)
75
+ number_file = File.join(@cache_dir, number + '_error.html')
76
+ if File.exist?(number_file)
77
+ @logger.info("Found cache at #{number_file}")
78
+ return true
79
+ end
80
+ return false
81
+ end
82
+
83
+ # 結果ページデータを取得
84
+ #
85
+ # @param [String] number ナンバー
86
+ # @return [Mechanize::Page|nil] 結果ページデータ
87
+ def page(number)
88
+ cache = cache(number)
89
+ unless cache.nil?
90
+ return cache
91
+ end
92
+
93
+ if exist_runner?(number)
94
+ return nil
95
+ end
96
+
97
+ search_page = @agent.get(@url)
98
+
99
+ # レースデータ取得エラー
100
+ if search_page.uri.path == '/record'
101
+ raise(RaceError.new(@url))
102
+ end
103
+
104
+ form = search_page.forms[0]
105
+ form.number = number
106
+ result_page = @agent.submit(form)
107
+ return mkcache(result_page, number)
108
+ end
109
+
110
+ # キャッシュを作成
111
+ #
112
+ # @param [Mechanize::Page] result_page Mechanize:Page
113
+ # @param [String] number ナンバー
114
+ def mkcache(result_page, number)
115
+ mkcache_dir
116
+
117
+ @logger.info("Download #{result_page.uri.path}")
118
+
119
+ # ランナーデータエラー(存在しない)でもキャッシュを作成する
120
+ if File.basename(result_page.uri.path) == 'number_error.html'
121
+ number_file = File.join(@cache_dir, "#{number}_error.html")
122
+ @agent.download(result_page.uri.path, number_file)
123
+ return nil
124
+ end
125
+
126
+ number_file = File.join(@cache_dir, "#{number}.html")
127
+ @agent.download(result_page.uri.path, number_file)
128
+ return result_page
129
+ end
130
+
131
+ # キャッシュディレクトリを作成
132
+ #
133
+ def mkcache_dir
134
+ unless Dir.exist?(CASHE_DIR)
135
+ Dir.mkdir(CASHE_DIR)
136
+ end
137
+
138
+ unless Dir.exist?(@cache_dir)
139
+ Dir.mkdir(@cache_dir)
140
+ end
141
+ end
142
+
143
+ # ランナーの結果データを取得
144
+ #
145
+ # @param [String] number ナンバー
146
+ # @return [Runner] 結果データ
147
+ def _result(number)
148
+ page = page(number)
149
+
150
+ # 取得出来ず
151
+ if page.nil?
152
+ return nil
153
+ end
154
+
155
+ name = page.at('div#personalBlock/dl/dd').text.sub(': ', '')
156
+
157
+ runner = Runner.new(number, name)
158
+ trs = page.search('table.sarchList/tr[@align="center"]')
159
+ trs.each do |tr|
160
+ tds = tr.xpath('td')
161
+ name, split, lap, pass = tds.map(&:text)
162
+ runner.add_point(name, split, lap, pass)
163
+ end
164
+
165
+ return runner
166
+ end
167
+ end
168
+
169
+ # レース結果取得エラークラス
170
+ #
171
+ # 公開が終了または存在しないレースIDの場合
172
+ class RaceError < StandardError
173
+ attr_reader(:url)
174
+
175
+ # initialize
176
+ #
177
+ # @param [String] url URL
178
+ #
179
+ def initialize(url)
180
+ @url = url
181
+ super("Cant't get #{url}. Probably race url is closed or not exist.")
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,38 @@
1
+ module RunnersUpdate
2
+ # ランナークラス
3
+ class Runner
4
+ attr_accessor :number, :name, :splits
5
+ def initialize(number, name)
6
+ @number = number
7
+ @name = name
8
+ @splits = []
9
+ end
10
+
11
+ # ポイントの追加
12
+ #
13
+ # @param [String] name 測定ポイント名
14
+ # @param [String] split スプリットタイム
15
+ # @param [String] lap ラップタイム
16
+ # @param [String] pass 通過タイム
17
+ def add_point(name, split, lap, pass)
18
+ @splits << Point.new(name, split, lap, pass)
19
+ end
20
+ end
21
+
22
+ # 測定ポイントクラス
23
+ class Point
24
+ attr_accessor :name, :split, :lap, :pass
25
+ # initialize
26
+ #
27
+ # @param [String] name 測定ポイント名
28
+ # @param [String] split スプリットタイム
29
+ # @param [String] lap ラップタイム
30
+ # @param [String] pass 通過タイム
31
+ def initialize(name, split, lap, pass)
32
+ @name = name
33
+ @split = split
34
+ @lap = lap
35
+ @pass = pass
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module RunnersUpdate
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'runners_update/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'runners_update'
8
+ spec.version = RunnersUpdate::VERSION
9
+ spec.authors = ['kyoshidajp']
10
+ spec.email = ['claddvd@gmail.com']
11
+
12
+ spec.summary = 'ランナーズアップデートのデータ取得'
13
+ spec.description = 'ランナーズアップデートのデータ取得'
14
+ spec.homepage = 'https://github.com/kyoshidajp/runners_update'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10.a'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec'
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: runners_update
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kyoshidajp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-31 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.10.a
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.10.a
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: rspec
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
+ description: "ランナーズアップデートのデータ取得"
56
+ email:
57
+ - claddvd@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/runners_update.rb
73
+ - lib/runners_update/client.rb
74
+ - lib/runners_update/runner.rb
75
+ - lib/runners_update/version.rb
76
+ - runners_update.gemspec
77
+ homepage: https://github.com/kyoshidajp/runners_update
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: "ランナーズアップデートのデータ取得"
101
+ test_files: []