weatherscout 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.travis.yml +9 -0
- data/Gemfile +9 -0
- data/LICENSE +21 -0
- data/README.md +49 -0
- data/Rakefile +8 -0
- data/bin/weatherscout +25 -0
- data/lib/weatherdesc.rb +1 -0
- data/lib/weatherdesc/daily_weather.rb +151 -0
- data/lib/weatherdesc/version.rb +5 -0
- data/spec/weatherdesc_spec.rb +42 -0
- data/weatherscout.gemspec +21 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 84e48b9981ddf468a62c8ad682ff6f3cd9359def
|
4
|
+
data.tar.gz: 159c408e906319a9d9fac6da41ba580a5a068323
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 63b20fea572f9647e0b6c3b66ca37a38b3a7c31be5925d17ab261490564e4d908c1dc3d5ea8be708de740ed57045f59264025ca801645432d0f2ee6519f6f3c3
|
7
|
+
data.tar.gz: f7f881c5dbcf2c3d1a1c0d6709c0005200e3d84cc61a31b69ca16e49c68089549f4d1c6eb6c6b0bf8865b895fdb78f0e8226266d549c0a6b01bbd11bf24f783c
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Kiwi-Learn
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# weather-scout
|
2
|
+
[![Build Status](https://travis-ci.org/vicky-sunshine/weather-scout-scraper.svg)](https://travis-ci.org/vicky-sunshine/weather-scout-scraper)
|
3
|
+
|
4
|
+
A simple CLI project to know the weather today!
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
Install it with the following command:
|
9
|
+
```sh
|
10
|
+
$ bundle install
|
11
|
+
```
|
12
|
+
|
13
|
+
Run it from the command line.
|
14
|
+
```sh
|
15
|
+
$ ./bin/weatherscout today 台北
|
16
|
+
台北12/31天氣狀況:
|
17
|
+
白天氣象:多雲時陰,氣溫 15 ~ 16 度
|
18
|
+
晚上氣象:多雲,氣溫 15 ~ 22 度
|
19
|
+
降雨機率:20 %
|
20
|
+
溫馨叮嚀:
|
21
|
+
【跨年夜氣溫偏冷,元旦水氣少、天氣穩定】。
|
22
|
+
今天(31日)東北季風影響,山區有局部短暫雨,其他地區為多雲到陰的天氣;氣溫方面,一整天較涼冷,台北站區高溫17.0度,低溫15.2度。
|
23
|
+
今晚至明(1月1日)晨仍受東北季風影響,台北市各區低溫約15至17度左右,提醒參加跨年活動或迎接日出的朋友做好保暖的措施。
|
24
|
+
天氣方面,水氣由濕轉乾的過程,入夜之後水氣更少,元旦清晨雲量較少,大多可以看到日出,高山欣賞曙光的條件相對理想許多。
|
25
|
+
明天白天之後,東北季風減弱,氣溫略回升,高溫可以來到22至23度;天氣方面,水氣更為減少,各區大多為多雲到晴的天氣。
|
26
|
+
此外,今晚至明晨東北季風影響,空曠地區易有較強陣風。
|
27
|
+
```
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
Use it from your Ruby code:
|
32
|
+
````ruby
|
33
|
+
require './lib/coursesdesc/courses.rb'
|
34
|
+
CITY = "新竹"
|
35
|
+
weather = DailyWeatherScraper::DailyWeather.new(CITY)
|
36
|
+
|
37
|
+
temp = weather.temperature_day
|
38
|
+
puts "今天白天氣溫:#{temp}"
|
39
|
+
|
40
|
+
tips = weather.tips
|
41
|
+
puts tips
|
42
|
+
|
43
|
+
````
|
44
|
+
|
45
|
+
## Test
|
46
|
+
|
47
|
+
```sh
|
48
|
+
$ rake
|
49
|
+
```
|
data/Rakefile
ADDED
data/bin/weatherscout
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'thor'
|
3
|
+
require './lib/weatherdesc/daily_weather.rb' # for cmd line testing purposes
|
4
|
+
|
5
|
+
class WeatherCLI < Thor
|
6
|
+
desc 'today CITY', 'see the weather of the city today'
|
7
|
+
def today(city)
|
8
|
+
weather = DailyWeatherScraper::DailyWeather.new(city)
|
9
|
+
date = weather.date
|
10
|
+
temperature_day = weather.temperature_day
|
11
|
+
temperature_night = weather.temperature_night
|
12
|
+
rain_probability = weather.rain_probability
|
13
|
+
condition_day = weather.condition_day
|
14
|
+
condition_night = weather.condition_night
|
15
|
+
tips = weather.tips
|
16
|
+
puts "#{city}(#{date})天氣狀況:"
|
17
|
+
puts "白天氣象:#{condition_day},氣溫 #{temperature_day} 度"
|
18
|
+
puts "晚上氣象:#{condition_night},氣溫 #{temperature_night} 度"
|
19
|
+
puts "降雨機率:#{rain_probability}"
|
20
|
+
puts "溫馨叮嚀:"
|
21
|
+
puts "#{tips}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
WeatherCLI.start(ARGV)
|
data/lib/weatherdesc.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'weatherdesc/daily_weather.rb'
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'oga'
|
2
|
+
require 'open-uri'
|
3
|
+
|
4
|
+
module DailyWeatherScraper
|
5
|
+
# parse course description from sharecourse web
|
6
|
+
class DailyWeather
|
7
|
+
CWB_URL = "http://www.cwb.gov.tw"
|
8
|
+
CITY_WEATHER_URL = "#{CWB_URL}/V7/forecast/taiwan/"
|
9
|
+
|
10
|
+
def initialize(city)
|
11
|
+
parse_html(city)
|
12
|
+
end
|
13
|
+
|
14
|
+
def date
|
15
|
+
@data ||= parse_date
|
16
|
+
end
|
17
|
+
|
18
|
+
def temperature_day
|
19
|
+
@temperature_day ||= parse_temp_day
|
20
|
+
end
|
21
|
+
|
22
|
+
def temperature_night
|
23
|
+
@temperature_night ||= parse_temp_night
|
24
|
+
end
|
25
|
+
|
26
|
+
def rain_probability
|
27
|
+
@rain_probability ||= parse_rain_prob
|
28
|
+
end
|
29
|
+
|
30
|
+
def condition_day
|
31
|
+
@condition_day ||= parse_condition_day
|
32
|
+
end
|
33
|
+
|
34
|
+
def condition_night
|
35
|
+
@condition_night ||= parse_condition_night
|
36
|
+
end
|
37
|
+
|
38
|
+
def tips
|
39
|
+
@tips ||= parse_tips
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def parse_html(city)
|
45
|
+
english_name = name_mapping(city)
|
46
|
+
url = "#{CITY_WEATHER_URL}/#{english_name}.htm"
|
47
|
+
@document = Oga.parse_html(open(url))
|
48
|
+
end
|
49
|
+
|
50
|
+
def parse_date
|
51
|
+
data = @document.xpath("//table[@class='FcstBoxTable01']")
|
52
|
+
result = data[2].children[3].children[0].children[1].children.text.split(" ")[1]
|
53
|
+
end
|
54
|
+
|
55
|
+
def parse_temp_day
|
56
|
+
data = @document.xpath("//table[@class='FcstBoxTable01']")
|
57
|
+
result = data[2].children[3].children[0].children[3].children.text
|
58
|
+
end
|
59
|
+
|
60
|
+
def parse_temp_night
|
61
|
+
data = @document.xpath("//table[@class='FcstBoxTable01']")
|
62
|
+
result = data[2].children[3].children[1].children[3].children.text
|
63
|
+
end
|
64
|
+
|
65
|
+
def parse_rain_prob
|
66
|
+
data = @document.xpath("//table[@class='FcstBoxTable01']")
|
67
|
+
result = data[2].children[3].children[0].children[9].children.text
|
68
|
+
end
|
69
|
+
|
70
|
+
def parse_condition_day
|
71
|
+
data = @document.xpath("//table[@class='FcstBoxTable01']")
|
72
|
+
result = data[2].children[3].children[0].children[5].children[1].attributes[1].value.force_encoding('UTF-8')
|
73
|
+
end
|
74
|
+
|
75
|
+
def parse_condition_night
|
76
|
+
data = @document.xpath("//table[@class='FcstBoxTable01']")
|
77
|
+
result = data[2].children[3].children[1].children[5].children[1].attributes[1].value.force_encoding('UTF-8')
|
78
|
+
end
|
79
|
+
|
80
|
+
def parse_tips
|
81
|
+
data = @document.xpath("//div[@id='box8']")
|
82
|
+
tips_api = data[0].children[13].children[0].text.split("'")[1]
|
83
|
+
tips_url = "#{CWB_URL}/#{tips_api}"
|
84
|
+
tips_doc = Oga.parse_html(open(tips_url))
|
85
|
+
result = ""
|
86
|
+
tips_doc.children.each do |element|
|
87
|
+
if element.text.empty?
|
88
|
+
result << "\n"
|
89
|
+
elsif element.text.split(":")[0].force_encoding('UTF-8') == "更新時間"
|
90
|
+
break
|
91
|
+
else
|
92
|
+
result << element.text
|
93
|
+
end
|
94
|
+
end
|
95
|
+
result = result.gsub("\n\n","\n")
|
96
|
+
result.force_encoding('UTF-8')
|
97
|
+
end
|
98
|
+
|
99
|
+
def name_mapping(city)
|
100
|
+
case city
|
101
|
+
when "臺北", "台北"
|
102
|
+
result = "Taipei_City"
|
103
|
+
when "新北"
|
104
|
+
result = "New_Taipei_City"
|
105
|
+
when "桃園"
|
106
|
+
result = "Taoyuan_City"
|
107
|
+
when "臺中", "台中"
|
108
|
+
result = "Taichung_City"
|
109
|
+
when "臺南", "台南"
|
110
|
+
result = "Tainan_City"
|
111
|
+
when "高雄"
|
112
|
+
result = "Kaohsiung_City"
|
113
|
+
when "基隆"
|
114
|
+
result = "Keelung_City"
|
115
|
+
when "新竹"
|
116
|
+
result = "Hsinchu_City"
|
117
|
+
when "新竹"
|
118
|
+
result = "Hsinchu_County"
|
119
|
+
when "苗栗"
|
120
|
+
result = "Miaoli_County"
|
121
|
+
when "彰化"
|
122
|
+
result = "Changhua_County"
|
123
|
+
when "南投"
|
124
|
+
result = "Nantou_County"
|
125
|
+
when "雲林"
|
126
|
+
result = "Yunlin_County"
|
127
|
+
when "嘉義"
|
128
|
+
result = "Chiayi_City"
|
129
|
+
when "嘉義"
|
130
|
+
result = "Chiayi_County"
|
131
|
+
when "屏東"
|
132
|
+
result = "Pingtung_County"
|
133
|
+
when "宜蘭"
|
134
|
+
result = "Yilan_County"
|
135
|
+
when "花蓮"
|
136
|
+
result = "Hualien_County"
|
137
|
+
when "臺東", "台東"
|
138
|
+
result = "Taitung_County"
|
139
|
+
when "澎湖"
|
140
|
+
result = "Penghu_County"
|
141
|
+
when "金門"
|
142
|
+
result = "Kinmen_County"
|
143
|
+
when "連江"
|
144
|
+
result = "Lienchiang_County"
|
145
|
+
else
|
146
|
+
result = ""
|
147
|
+
end
|
148
|
+
result
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require './lib/weatherdesc.rb'
|
3
|
+
|
4
|
+
CITY = "新竹"
|
5
|
+
weather = DailyWeatherScraper::DailyWeather.new(CITY)
|
6
|
+
|
7
|
+
describe 'Daily weather desc scrap' do
|
8
|
+
it 'should return date with the "month/day" style' do
|
9
|
+
date = weather.date
|
10
|
+
date.must_match /\d\d\/\d\d/
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should return temperature in daytime' do
|
14
|
+
temp = weather.temperature_day
|
15
|
+
temp.must_match /(-|\d)\d+\s~\s(-|\d)\d+/
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should return temperature in nighttime' do
|
19
|
+
temp = weather.temperature_night
|
20
|
+
temp.must_match /(-|\d)\d+\s~\s(-|\d)\d+/
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should return weather condition in daytime' do
|
24
|
+
con = weather.condition_day
|
25
|
+
con.must_be_instance_of String
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should return weather condition in nighttime' do
|
29
|
+
con = weather.condition_night
|
30
|
+
con.must_be_instance_of String
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should return rain probaliity' do
|
34
|
+
prob = weather.rain_probability
|
35
|
+
prob.must_match /\d+\s%/
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should return weather tips' do
|
39
|
+
tips = weather.tips
|
40
|
+
tips.must_be_instance_of String
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'weatherdesc/version'
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'weatherscout'
|
5
|
+
s.version = WeatherDesc::VERSION
|
6
|
+
s.date = WeatherDesc::DATE
|
7
|
+
s.executables << 'weatherscout'
|
8
|
+
s.summary = 'Tell you the weather of Taiwan'
|
9
|
+
s.description = 'You can find the weather of the major city in Taiwan'
|
10
|
+
s.authors = ['Vicky Lee']
|
11
|
+
s.email = ['lch82327@gmail.com']
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files spec/*`.split("\n")
|
14
|
+
s.homepage = 'https://github.com/vicky-sunshine/weather-scout-scraper'
|
15
|
+
s.license = 'MIT'
|
16
|
+
|
17
|
+
# depend gem
|
18
|
+
s.add_development_dependency 'minitest'
|
19
|
+
s.add_runtime_dependency 'oga'
|
20
|
+
s.add_runtime_dependency 'thor'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weatherscout
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vicky Lee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oga
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thor
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: You can find the weather of the major city in Taiwan
|
56
|
+
email:
|
57
|
+
- lch82327@gmail.com
|
58
|
+
executables:
|
59
|
+
- weatherscout
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/weatherscout
|
70
|
+
- lib/weatherdesc.rb
|
71
|
+
- lib/weatherdesc/daily_weather.rb
|
72
|
+
- lib/weatherdesc/version.rb
|
73
|
+
- spec/weatherdesc_spec.rb
|
74
|
+
- weatherscout.gemspec
|
75
|
+
homepage: https://github.com/vicky-sunshine/weather-scout-scraper
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.5.0
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Tell you the weather of Taiwan
|
99
|
+
test_files:
|
100
|
+
- spec/weatherdesc_spec.rb
|