weather_fetcher 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/weather_fetcher/providers/html_based/onet_pl.rb +60 -108
- data/lib/weather_fetcher/providers/html_based/world_weather_online.rb +6 -1
- data/lib/weather_fetcher/providers/html_based_provider.rb +5 -0
- data/lib/weather_fetcher/providers/metar_provider.rb +5 -0
- data/lib/weather_fetcher/providers/provider.rb +8 -0
- data/lib/weather_fetcher/weather_data.rb +11 -1
- metadata +15 -15
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
@@ -9,32 +9,25 @@ module WeatherFetcher
|
|
9
9
|
|
10
10
|
def process(string)
|
11
11
|
a = Array.new
|
12
|
-
a += WeatherData.factory(
|
13
|
-
a += WeatherData.factory( _process_daily(string) )
|
12
|
+
a += WeatherData.factory(_process_body(string))
|
14
13
|
return a
|
15
14
|
end
|
16
15
|
|
17
16
|
# Process response body and rip out weather data, details
|
18
|
-
def
|
17
|
+
def _process_body(body_raw)
|
19
18
|
|
20
19
|
body_tmp = body_raw.downcase
|
21
|
-
# if body_tmp =~ /szczeg..owa(.*)Prognoza/
|
22
|
-
# body = $1
|
23
|
-
# else
|
24
|
-
# return []
|
25
|
-
# end
|
26
20
|
body = body_tmp
|
27
21
|
|
28
|
-
|
29
|
-
|
22
|
+
times = body.scan(/<span class=\"time\">godz. <strong>(\d+)-(\d+)/i)
|
23
|
+
temperatures = body.scan(/<span class="temp">\s*(-*\d+)[^<]+<\/span>/i)
|
24
|
+
pressures = body.scan(/(\d*)\s*hpa/i)
|
30
25
|
winds = body.scan(/(\d*)\s*km\/h/)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
#
|
35
|
-
|
36
|
-
# time soon
|
37
|
-
time_soon = body.scan(/<span class="ar2b gold">wkr.tce <\/span><span class="ar2 gold">(\d*)-(\d*)<\/span>/)
|
26
|
+
# no more snow :(
|
27
|
+
#snows = body.scan(/nieg:<\/td><td class=\"[^"]*\">\s*([0-9.]*)\s*mm</)
|
28
|
+
snow = []
|
29
|
+
#rains = body.scan(/Deszcz:\s*<\/label>\s*([0-9,]+)\s*mm/i)
|
30
|
+
rains = body.scan(/Deszcz:[^0-9]*([0-9,]+)\s*mm/i)
|
38
31
|
|
39
32
|
unix_time_today = Time.mktime(
|
40
33
|
Time.now.year,
|
@@ -42,120 +35,79 @@ module WeatherFetcher
|
|
42
35
|
Time.now.day,
|
43
36
|
0, 0, 0, 0)
|
44
37
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
# next day
|
49
|
-
unix_time_now_to += 24 * 3600
|
50
|
-
end
|
38
|
+
# should be ok
|
39
|
+
# puts times.size, temperatures.size, pressures.size, winds.size, rains.size
|
40
|
+
# puts times.inspect, temperatures.inspect, pressures.inspect, winds.inspect, rains.inspect
|
51
41
|
|
52
|
-
|
53
|
-
unix_time_soon_to = unix_time_today + 3600 * time_soon[0][1].to_i
|
54
|
-
if time_soon[0][1].to_i < time_soon[0][0].to_i
|
55
|
-
# next day
|
56
|
-
unix_time_soon_to += 24 * 3600
|
57
|
-
end
|
58
|
-
if time_now[0][0].to_i > time_soon[0][0].to_i
|
59
|
-
# time soon is whole new day
|
60
|
-
unix_time_soon_from += 24 * 3600
|
61
|
-
unix_time_soon_to += 24 * 3600
|
62
|
-
end
|
42
|
+
data = Array.new
|
63
43
|
|
64
|
-
|
65
|
-
|
44
|
+
# today
|
45
|
+
(0..3).each do |i|
|
46
|
+
h = {
|
66
47
|
:time_created => Time.now,
|
67
|
-
:time_from =>
|
68
|
-
:time_to =>
|
69
|
-
:temperature => temperatures[
|
48
|
+
:time_from => unix_time_today + times[i][0].to_i * 3600,
|
49
|
+
:time_to => unix_time_today + times[i][1].to_i * 3600,
|
50
|
+
:temperature => temperatures[1 + i][0].to_f,
|
70
51
|
:pressure => pressures[0][0].to_f,
|
71
52
|
:wind_kmh => winds[0][0].to_f,
|
72
53
|
:wind => winds[0][0].to_f / 3.6,
|
73
|
-
:snow => snows[0][0].to_f,
|
74
|
-
:rain => rains[0][0].to_f,
|
54
|
+
:snow => nil, #snows[0][0].to_f,
|
55
|
+
:rain => rains[0][0].gsub(/,/, '.').to_f,
|
75
56
|
:provider => self.class.provider_name
|
76
|
-
}
|
77
|
-
|
57
|
+
}
|
58
|
+
h[:time_to] += 24*3600 if h[:time_to] < h[:time_from]
|
59
|
+
|
60
|
+
data << h
|
61
|
+
end
|
62
|
+
|
63
|
+
# tomorrow
|
64
|
+
(4..7).each do |i|
|
65
|
+
h = {
|
78
66
|
:time_created => Time.now,
|
79
|
-
:time_from =>
|
80
|
-
:time_to =>
|
81
|
-
:temperature => temperatures[
|
67
|
+
:time_from => unix_time_today + times[i][0].to_i * 3600 + 24*3600,
|
68
|
+
:time_to => unix_time_today + times[i][1].to_i * 3600 + 24*3600,
|
69
|
+
:temperature => temperatures[6 + i][0].to_f,
|
82
70
|
:pressure => pressures[1][0].to_f,
|
83
71
|
:wind_kmh => winds[1][0].to_f,
|
84
72
|
:wind => winds[1][0].to_f / 3.6,
|
85
|
-
:snow => snows[
|
86
|
-
:rain => rains[1][0].to_f,
|
73
|
+
:snow => nil, #snows[0][0].to_f,
|
74
|
+
:rain => rains[1][0].gsub(/,/, '.').to_f,
|
87
75
|
:provider => self.class.provider_name
|
88
76
|
}
|
89
|
-
|
90
|
-
|
91
|
-
return data
|
92
|
-
end
|
93
|
-
|
94
|
-
# Process response body and rip out weather data, daily
|
95
|
-
def _process_daily(body_raw)
|
77
|
+
h[:time_to] += 24*3600 if h[:time_to] < h[:time_from]
|
96
78
|
|
97
|
-
|
98
|
-
|
79
|
+
data << h
|
80
|
+
end
|
81
|
+
# puts data.to_yaml
|
99
82
|
|
100
|
-
#
|
101
|
-
|
102
|
-
times = times.collect { |t|
|
103
|
-
t_from = Time.mktime(
|
104
|
-
t[2].to_i,
|
105
|
-
t[1].to_i,
|
106
|
-
t[0].to_i,
|
107
|
-
0, 0, 0, 0)
|
108
|
-
|
109
|
-
{ :time_from => t_from, :time_to => t_from + 24*3600 }
|
110
|
-
}
|
111
|
-
#puts times.size
|
112
|
-
#puts times.inspect
|
113
|
-
|
114
|
-
temperatures = body.scan(/<span title=\"temperatura w dzie.\">([-.0-9]+)<\/span>\s*<span title=\"temperatura w nocy\"[^>]*>([-.0-9]+)<\/span>/)
|
115
|
-
temperatures = temperatures.collect { |t|
|
116
|
-
{ :temperature_night => t[1].to_f, :temperature_day => t[0].to_f, :temperature => (t[0].to_f + t[1].to_f)/2.0 }
|
117
|
-
}
|
118
|
-
#puts temperatures.size
|
119
|
-
#puts temperatures.inspect
|
120
|
-
#exit!
|
121
|
-
|
122
|
-
#pressures = body.scan(/>(\d+)\s*hpa</)
|
123
|
-
pressures = body.scan(/(\d+)\s*hpa/)
|
124
|
-
pressures = pressures.collect { |t| t[0].to_i }
|
125
|
-
# puts pressures.inspect
|
126
|
-
|
127
|
-
# wind speed in m/s
|
128
|
-
winds = body.scan(/(\d+)\s*km\/h/)
|
129
|
-
winds = winds.collect { |t| t[0].to_f / 3.6 }
|
130
|
-
# puts winds.inspect
|
131
|
-
|
132
|
-
snows = body.scan(/nieg:<\/td><td class="[^"]*">([0-9.]*)\s*mm</)
|
133
|
-
snows = snows.collect { |t| t[0].to_f }
|
134
|
-
# puts snows.inspect
|
135
|
-
|
136
|
-
rains = body.scan(/eszcz:<\/td><td class=\"[^"]*\">\s*([0-9.]*)\s*mm</)
|
137
|
-
rains = rains.collect { |t| t[0].to_f }
|
138
|
-
# puts rains.inspect
|
83
|
+
# longer
|
84
|
+
dates = body.scan(/<time datetime=\"(\d{4})-(\d{1,2})-(\d{1,2})\">/i)
|
139
85
|
|
86
|
+
(0..10).each do |i|
|
87
|
+
unix_time = Time.mktime(
|
88
|
+
dates[i][0].to_i,
|
89
|
+
dates[i][1].to_i,
|
90
|
+
dates[i][2].to_i
|
91
|
+
)
|
92
|
+
# puts unix_time, dates[i].inspect
|
140
93
|
|
141
|
-
data = Array.new
|
142
|
-
# temperatures array because some last times is for detailed weather prediction
|
143
|
-
# not for days
|
144
|
-
(0...(temperatures.size)).each do |i|
|
145
94
|
h = {
|
146
95
|
:time_created => Time.now,
|
147
|
-
:time_from =>
|
148
|
-
:time_to =>
|
149
|
-
:temperature => temperatures[i][
|
150
|
-
:pressure => pressures[i],
|
151
|
-
:wind_kmh => winds[i]
|
152
|
-
:wind => winds[i],
|
153
|
-
:snow => snows[
|
154
|
-
:rain => rains[i],
|
96
|
+
:time_from => unix_time,
|
97
|
+
:time_to => unix_time + 24*3600,
|
98
|
+
:temperature => temperatures[13 + i][0].to_f,
|
99
|
+
:pressure => pressures[3 + i][0].to_f,
|
100
|
+
:wind_kmh => winds[3 + i][0].to_f,
|
101
|
+
:wind => winds[3 + i][0].to_f / 3.6,
|
102
|
+
:snow => nil, #snows[0][0].to_f,
|
103
|
+
:rain => rains[3 + i][0].gsub(/,/, '.').to_f,
|
155
104
|
:provider => self.class.provider_name
|
156
105
|
}
|
106
|
+
h[:time_to] += 24*3600 if h[:time_to] < h[:time_from]
|
107
|
+
|
157
108
|
data << h
|
158
109
|
end
|
110
|
+
# puts data.to_yaml
|
159
111
|
|
160
112
|
return data
|
161
113
|
end
|
@@ -8,6 +8,11 @@ module WeatherFetcher
|
|
8
8
|
"WorldWeatherOnline"
|
9
9
|
end
|
10
10
|
|
11
|
+
# How often weather is updated
|
12
|
+
def self.weather_updated_every
|
13
|
+
12*HOUR - 240
|
14
|
+
end
|
15
|
+
|
11
16
|
# This provider required API key
|
12
17
|
def self.api=(_api)
|
13
18
|
@@api = _api
|
@@ -84,7 +89,7 @@ module WeatherFetcher
|
|
84
89
|
|
85
90
|
end
|
86
91
|
|
87
|
-
return
|
92
|
+
return WeatherData.factory(weather_archives)
|
88
93
|
|
89
94
|
end
|
90
95
|
|
@@ -4,6 +4,9 @@ module WeatherFetcher
|
|
4
4
|
# kind of provider: standard, html (self web based), gem (other gem)
|
5
5
|
TYPE = :standard
|
6
6
|
|
7
|
+
# just a constant, seconds in 1 hour
|
8
|
+
HOUR = 3600
|
9
|
+
|
7
10
|
# Create an instance, definitions can be set here
|
8
11
|
def initialize(_defs = Array.new)
|
9
12
|
@weathers = Array.new
|
@@ -47,6 +50,11 @@ module WeatherFetcher
|
|
47
50
|
a = Array.new
|
48
51
|
defs.each do |d|
|
49
52
|
p = fetch_and_process_single(d)
|
53
|
+
p.each do |pw|
|
54
|
+
pw.just_fetched!
|
55
|
+
pw.next_within!(self.class.weather_updated_every)
|
56
|
+
end
|
57
|
+
|
50
58
|
a += p unless p.nil?
|
51
59
|
end
|
52
60
|
# add to result array
|
@@ -27,7 +27,17 @@ module WeatherFetcher
|
|
27
27
|
return ao
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
# Mark this weather as just downloaded
|
31
|
+
def just_fetched!
|
32
|
+
@fetch_time ||= Time.now
|
33
|
+
end
|
34
|
+
|
35
|
+
def next_within!(_interval)
|
36
|
+
# just_fetched!
|
37
|
+
@next_fetch_time = @fetch_time + _interval
|
38
|
+
end
|
39
|
+
|
40
|
+
attr_reader :temperature, :wind, :time_from, :time_to, :fetch_time, :next_fetch_time
|
31
41
|
|
32
42
|
end
|
33
43
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weather_fetcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-15 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: simple_metar_parser
|
16
|
-
requirement: &
|
16
|
+
requirement: &23192640 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.0.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *23192640
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &23192160 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.3.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *23192160
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: bundler
|
38
|
-
requirement: &
|
38
|
+
requirement: &23191640 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *23191640
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: jeweler
|
49
|
-
requirement: &
|
49
|
+
requirement: &23191120 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.6.4
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *23191120
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: simplecov
|
60
|
-
requirement: &
|
60
|
+
requirement: &23190620 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *23190620
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rdoc
|
71
|
-
requirement: &
|
71
|
+
requirement: &23190100 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *23190100
|
80
80
|
description: Fetch weather from various Polish websites and via other gems. At the
|
81
81
|
moment it is only polish portal Onet.pl but more providers will come soon.
|
82
82
|
email: bobikx@poczta.fm
|
@@ -125,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
125
|
version: '0'
|
126
126
|
segments:
|
127
127
|
- 0
|
128
|
-
hash: -
|
128
|
+
hash: -806040383706677552
|
129
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
130
|
none: false
|
131
131
|
requirements:
|