Created
October 4, 2015 09:19
-
-
Save hiiamyes/8b0bab16876b9156d7cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async = require 'async' | |
moment = require 'moment' | |
request = require 'request' | |
cheerio = require 'cheerio' | |
# task | |
startTime = moment() | |
async.parallel({ | |
forecastHour: (callback) -> | |
request 'http://www.cwb.gov.tw/V7/forecast/entertainment/3Hr/D106.htm', (err, res, body) -> | |
if err then cb 'Hour crawling fail.', null | |
else | |
$ = cheerio.load body | |
forecast = { | |
time: [], | |
weather: [], | |
temperature: [], | |
probabilityOfPrecipitation: [] | |
} | |
# time parser | |
hourPre = 0 | |
dayAdd = 0 | |
$('tr:nth-child(2) td span').each (i) -> | |
hour = Number.parseInt $(this).text().split(':')[0] | |
if hourPre > hour then dayAdd++ | |
hourPre = hour | |
forecast.time.push moment($(this).text(), 'HH:mm').add(dayAdd, 'd').format() | |
# weather parser | |
$('tr:nth-child(3) td').each (i) -> if i > 0 then forecast.weather.push $(this).find('img').attr('title') | |
# temperature parser | |
$('tr:nth-child(4) td').each (i) -> if i > 0 then forecast.temperature.push $(this).text() | |
# probability of precipitation parser | |
$('tr:nth-child(8) td').each (i) -> | |
if i > 0 | |
if $(this).attr('colspan') | |
for [0...$(this).attr('colspan')] | |
forecast.probabilityOfPrecipitation.push $(this).text() | |
else | |
forecast.probabilityOfPrecipitation.push $(this).text() | |
callback null, forecast | |
, | |
forecastDay: (callback) -> | |
request 'http://www.cwb.gov.tw/V7/forecast/entertainment/7Day/D106.htm', (err, res, body) -> | |
if err then callback 'Day crawling fail.', null | |
else | |
$ = cheerio.load body | |
forecast = { | |
time: [], | |
weather: [], | |
probabilityOfPrecipitation: [] | |
} | |
for i in [0..6] | |
forecast.time.push moment().add(i, 'd').format() | |
forecast.time.push moment().add(i, 'd').format() | |
# weather parser | |
$('tr:nth-child(3) td').each (i) -> if i > 0 then forecast.weather.push $(this).find('img').attr('title') | |
# probability of precipitation parser | |
$('tr:nth-child(9) td').each (i) -> | |
if i > 0 then forecast.probabilityOfPrecipitation.push $(this).text() | |
# return | |
callback null, forecast | |
}, | |
(err, results) -> | |
if err then console.log 'Crawling fail: ' + err | |
else | |
console.log '\n\nCrawling success in ' + moment().diff(startTime) + ' ms.' | |
console.log '\n逐三小時預報:' | |
console.log '( 日期 / 時間 / 天氣型態 / 氣溫 / 降雨機率 )' | |
for time, i in results.forecastHour.time | |
console.log moment(time).format('MM/DD HH:00 ') + results.forecastHour.weather[i] + ' ' + results.forecastHour.temperature[i] + ' ' + results.forecastHour.probabilityOfPrecipitation[i] | |
console.log '\n一周預報:' | |
console.log '( 日期 / 早晚 / 天氣型態 / 降雨機率 )' | |
for time, i in results.forecastDay.time | |
daytimeOrNight = if i % 2 then '晚上 ' else '白天 ' | |
console.log moment(time).format('MM/DD ') + daytimeOrNight + results.forecastDay.weather[i] + ' ' + results.forecastDay.probabilityOfPrecipitation[i] | |
console.log '\n\n' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment