ishikawa_air_pollution 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .ruby-version
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ishikawa_air_pollution.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Keisuke KITA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # IshikawaAirPollution
2
+
3
+ 石川県の大気汚染物質の測定値を取得するライブラリです.
4
+ [石川県の大気環境の状況](http://www.pref.ishikawa.jp/cgi-bin/taiki/top.pl)からデータを取得しています.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'ishikawa_air_pollution'
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install ishikawa_air_pollution
19
+
20
+ ## Usage
21
+ ```ruby
22
+ require 'ishikawa_air_pollution'
23
+
24
+ client = IshikawaAirPollution.new
25
+
26
+ # 取得できる物質一覧
27
+ # so2 二酸化硫黄
28
+ # no 一酸化窒素
29
+ # no2 二酸化窒素
30
+ # nox 窒素酸化物
31
+ # co 一酸化炭素
32
+ # ox 光化学オキシダント
33
+ # nmhc 非メタン炭素水素
34
+ # ch4 メタン
35
+ # thc 全炭素水素
36
+ # spm 浮遊粒子状物質
37
+ # pm25 微小粒子状物質
38
+
39
+ pp client.pm25
40
+ # PM2.5の測定データを取得
41
+ # {"name_ja"=>"微小粒子状物質",
42
+ # "name"=>"PM2.5",
43
+ # "unit"=>"μg/m3",
44
+ # "locations"=>
45
+ # [{"name"=>"小松", "value"=>19.0},
46
+ # {"name"=>"七尾", "value"=>22.0},
47
+ # {"name"=>"野々市自排", "value"=>23.0}]}
48
+
49
+ pp client.location("津幡")
50
+ # 津幡の測定局で測定したデータ
51
+ # {"no"=>{"value"=>0.001, "name_ja"=>"一酸化窒素", "name"=>"NO", "unit"=>"ppm"},
52
+ # "no2"=>{"value"=>0.006, "name_ja"=>"二酸化窒素", "name"=>"NO2", "unit"=>"ppm"},
53
+ # "nox"=>{"value"=>0.007, "name_ja"=>"窒素酸化物", "name"=>"NOx", "unit"=>"ppm"},
54
+ # "ox"=>{"value"=>0.06, "name_ja"=>"光化学オキシダント", "name"=>"Ox", "unit"=>"ppm"},
55
+ # "spm"=>{"value"=>0.025, "name_ja"=>"浮遊粒子状物質", "name"=>"SPM", "unit"=>"mg/m3"}}
56
+
57
+ pp client.observation
58
+ # 各測定局の測定開始時間.元サイトは一時間毎に更新される
59
+ # {"start"=>
60
+ # #<DateTime: 2013-03-06T15:00:00+09:00 ((2456358j,21600s,0n),+32400s,2299161j)>,
61
+ # "end"=>
62
+ # #<DateTime: 2013-03-06T16:00:00+09:00 ((2456358j,25200s,0n),+32400s,2299161j)>,
63
+ # "period_sec"=>3600}
64
+
65
+ client.refetch! # データの再取得
66
+ ```
67
+
68
+ ## Origin
69
+ Thanks, sharapeco! [IshikawaAirPollutionAPI.rb](https://gist.github.com/sharapeco/5089792)
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 'ishikawa_air_pollution/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ishikawa_air_pollution"
8
+ spec.version = IshikawaAirPollution::VERSION
9
+ spec.authors = ["Keisuke KITA"]
10
+ spec.email = ["kei.kita2501@gmail.com"]
11
+ spec.description = %q{Fetch the observed value of air pollutants in Ishikawa pref}
12
+ spec.summary = %q{Fetch the observed value of air pollutants in Ishikawa pref}
13
+ spec.homepage = "https://github.com/kitak/ishikawa_air_pollution"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'mechanize'
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,3 @@
1
+ class IshikawaAirPollution
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,135 @@
1
+ # coding: utf-8
2
+ require "ishikawa_air_pollution/version"
3
+ require "date"
4
+ require "mechanize"
5
+
6
+ class IshikawaAirPollution
7
+ def initialize
8
+ @agent = Mechanize.new
9
+ @agent.user_agent_alias = 'Mac Safari'
10
+ @agent.request_headers = {
11
+ 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
12
+ 'Accept-language' => 'ja-jp',
13
+ 'Accept-charset' => '',
14
+ 'Accept-encoding' => 'gzip, deflate',
15
+ 'Keep-alive' => nil
16
+ }
17
+ parse_page(fetch_page)
18
+ end
19
+
20
+ def refetch!
21
+ parse_page(fetch_page)
22
+ end
23
+
24
+ def location(name)
25
+ raise "unexist location" unless @location.has_key?(name)
26
+ @location[name].inject({}) { |buf, (key, val)|
27
+ buf[key] = val.merge(@material[key])
28
+ buf
29
+ }
30
+ end
31
+
32
+ def observation
33
+ {
34
+ "start" => @start_observation,
35
+ "end" => @start_observation + Rational(1, 24),
36
+ "period_sec" => 3600
37
+ }
38
+ end
39
+
40
+ private
41
+ def fetch_page
42
+ @agent.get('http://www.pref.ishikawa.jp/cgi-bin/taiki/top.pl')
43
+ end
44
+
45
+ def method_missing(name, *args)
46
+ if @material.has_key? name.to_s
47
+ locations = @location.inject([]) { |buf, (key, val)|
48
+ if val.has_key? name.to_s
49
+ buf << {
50
+ "name" => key,
51
+ "value" => val[name.to_s]["value"]
52
+ }
53
+ end
54
+ buf
55
+ }
56
+ @material[name.to_s].merge({
57
+ "locations" => locations
58
+ })
59
+ else
60
+ super
61
+ end
62
+ end
63
+
64
+ def respond_to?(name, *args)
65
+ return true if @material.has_key? name.to_s
66
+ super
67
+ end
68
+
69
+ def parse_page(page)
70
+ @start_observation = fetch_datetime(page)
71
+ @material = fetch_material_header(page)
72
+ @location = fetch_value_each_location(page)
73
+ rescue => e
74
+ raise "parse failed: #{e.message}"
75
+ end
76
+
77
+ def fetch_datetime(page)
78
+ y = 0; mon = 0; d = 0; h = 0; min = 0
79
+ page.search('#subhead div').each do |div|
80
+ y, mon, d = $1.to_i, $2.to_i, $3.to_i if /(\d{4})年(\d{2})月(\d{2})日/ =~ div.text
81
+ h, min = $1.to_i, $2.to_i if /(\d{2})\:(\d{2})/ =~ div.text
82
+ end
83
+ DateTime.new(y, mon, d, h, min, 0, 'GMT+09')
84
+ end
85
+
86
+ def fetch_material_header(page)
87
+ tables = page.search('table')
88
+ tables.detect { |table|
89
+ table.search('td')[0].text == '測定局名'
90
+ }.instance_eval {
91
+ trs = self.search('tr')
92
+ thead = trs[0]
93
+ material = thead.search('td').inject({}) do |buf, td|
94
+ if /((?:\p{Han}|\p{katakana}|\p{Hiragana})+)(.+)\((.+)\)/u =~ td.text
95
+ name_ja = $1; name = $2; unit = $3
96
+ buf[$2.downcase.gsub(/\./, '')] = {
97
+ "name_ja" => name_ja,
98
+ "name" => name,
99
+ "unit" => unit
100
+ }
101
+ end
102
+ buf
103
+ end
104
+ }
105
+ end
106
+
107
+ def fetch_value_each_location(page)
108
+ tables = page.search('table')
109
+ tables.select { |table|
110
+ table.search('td')[0].text == '測定局名'
111
+ }.map { |table|
112
+ trs = table.search('tr')
113
+ trs[1..-1].inject({}) { |buf, tr|
114
+ tds = tr.search('td')
115
+ loc_name = tds[0].text.gsub(/\302\240/, ' ').strip
116
+ buf[loc_name] = tds[1..-1].each_with_index.inject({}) do |buf2, (td, i)|
117
+ value = td.text.gsub(/\302\240/, ' ').strip
118
+ buf2[@material.keys[i]] = { "value" => value.to_f } unless value.empty? || value=="***"
119
+ buf2
120
+ end
121
+ buf
122
+ }
123
+ }.instance_eval {
124
+ self[0].merge(self[1])
125
+ }
126
+ end
127
+ end
128
+
129
+ if __FILE__ == $0
130
+ client = IshikawaAirPollution.new
131
+ require 'pp'
132
+ pp client.pm25
133
+ pp client.location("津幡")
134
+ pp client.observation
135
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ishikawa_air_pollution
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keisuke KITA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Fetch the observed value of air pollutants in Ishikawa pref
63
+ email:
64
+ - kei.kita2501@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - ishikawa_air_pollution.gemspec
75
+ - lib/ishikawa_air_pollution.rb
76
+ - lib/ishikawa_air_pollution/version.rb
77
+ homepage: https://github.com/kitak/ishikawa_air_pollution
78
+ licenses:
79
+ - MIT
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.23
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Fetch the observed value of air pollutants in Ishikawa pref
102
+ test_files: []
103
+ has_rdoc: