Last active
August 29, 2015 14:20
-
-
Save hiiamyes/84b04abb5a7b592da6b0 to your computer and use it in GitHub Desktop.
This file contains 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
request = require 'request' # https://github.com/request/request | |
cheerio = require 'cheerio' # https://github.com/cheeriojs/cheerio | |
async = require 'async' | |
async.waterfall([ | |
(cb) -> | |
request 'http://recreation.forest.gov.tw/index.aspx', (err, res, body) -> | |
$ = cheerio.load body | |
# console.log 'open homepage: ' + res.statusCode | |
# console.log res.headers | |
cb null, $('#__VIEWSTATE').val(), $('#__EVENTVALIDATION').val() | |
,(viewstate, eventvalidation, cb) -> | |
request( | |
{ | |
'method': 'POST' | |
'url': 'http://recreation.forest.gov.tw/index.aspx' | |
'headers': | |
'Connection': 'keep-alive' | |
'form': | |
'__VIEWSTATE': viewstate | |
'__EVENTVALIDATION': eventvalidation | |
'ctl00$ContentPlaceHolder1$ID_TextBox': '[email protected]' | |
'ctl00$ContentPlaceHolder1$Pass_TextBox': 'iamahutcrawler1' | |
'ctl00$ContentPlaceHolder1$btn_login': '登入' | |
}, (err, res, body) -> | |
# console.log 'login: ' + res.statusCode | |
# console.log res.headers | |
# cookie = res.headers['set-cookie'][0].split(';')[0] + ';' + res.headers['set-cookie'][1].split(';')[0] | |
cookie = res.headers['set-cookie'][0].split(';')[0] | |
cb null, cookie | |
) | |
,(cookie, cb) -> | |
request( | |
{ | |
'url': 'http://recreation.forest.gov.tw/askformonhouse/AskForPaperAddNew.aspx?mode=0&AskSID=&houseid=C' | |
'headers': | |
'Connection': 'keep-alive' | |
'Cookie': cookie | |
}, (err, res, body) -> | |
# console.log 'get hut: ' + res.statusCode | |
# console.log res.headers | |
$ThisMonth = cheerio.load body | |
# parser $1 | |
# console.log $('#eventscalendar_ctl00_label2').text().split(' ')[0] | |
cb null, cookie, $ThisMonth, $ThisMonth('#__VIEWSTATE').val(), $ThisMonth('#__EVENTVALIDATION').val() | |
) | |
,(cookie, $ThisMonth, viewstate, eventvalidation, cb) -> | |
request( | |
{ | |
'method': 'POST' | |
'url': 'http://recreation.forest.gov.tw/askformonhouse/AskForPaperAddNew.aspx?mode=0&AskSID=&houseid=C' | |
'headers': | |
'Cookie': cookie | |
'form': | |
'__VIEWSTATE': viewstate | |
'__EVENTVALIDATION': eventvalidation | |
'__EVENTTARGET': 'eventscalendar$ctl00$NextMonth' | |
'__VIEWSTATEENCRYPTED': '' | |
}, (err, res, body) -> | |
# console.log 'post hut: ' + res.statusCode | |
# console.log res.headers | |
$NextMonth = cheerio.load body | |
parser $ThisMonth, $NextMonth | |
# console.log $('#eventscalendar_ctl00_label2').text().split(' ')[0] | |
cb null, 'done' | |
) | |
], (err, result) -> | |
console.log result | |
) | |
parser = ($ThisMonth, $NextMonth) -> | |
# Check the month | |
monthZHThisMonth = $ThisMonth('#eventscalendar_ctl00_label2').text().split(' ')[0] | |
monthThisMonth=0 | |
switch monthZHThisMonth | |
when '一月' then monthThisMonth = 1 | |
when '二月' then monthThisMonth = 2 | |
when '三月' then monthThisMonth = 3 | |
when '四月' then monthThisMonth = 4 | |
when '五月' then monthThisMonth = 5 | |
when '六月' then monthThisMonth = 6 | |
when '七月' then monthThisMonth = 7 | |
when '八月' then monthThisMonth = 8 | |
when '九月' then monthThisMonth = 9 | |
when '十月' then monthThisMonth = 10 | |
when '十一月' then monthThisMonth = 11 | |
when '十二月' then monthThisMonth = 12 | |
# Check days one by one | |
capacityStatus = [] | |
dayPre = 0 | |
month = monthThisMonth | |
$ThisMonth('.dayNumber').each (i) -> | |
# Calculate the date | |
day = parseInt $ThisMonth(this).text() | |
if i is 0 and day isnt 1 then month-- else # ex: 4/29 4/30 5/1 5/2 | |
if day < dayPre then month++ # ex: 5/31 6/1 6/2 6/3 | |
dayPre = day | |
push capacityStatus, $ThisMonth(this), month, day | |
monthCheck = month | |
dayCheck = dayPre | |
month = monthThisMonth + 1 | |
pass = false | |
$NextMonth('.dayNumber').each (i) -> | |
# Calculate the date | |
day = parseInt $NextMonth(this).text() | |
if i is 0 and day isnt 1 then month-- else | |
if day < dayPre then month++ | |
dayPre = day | |
if not pass and month is monthCheck and day is dayCheck then pass = true | |
else if pass | |
push capacityStatus, $NextMonth(this), month, day | |
console.log capacityStatus | |
console.log capacityStatus.length | |
push = (capacityStatus, ele, month, day) -> | |
if ele.parent().children().length > 1 | |
date = new Date() | |
date.setMonth(month - 1) | |
date.setDate(day) | |
remaining = ele.parent().find('#eventscalendar_Label1_0').html().split('<br>')[1].split(':')[1] | |
applying = ele.parent().find('#eventscalendar_Label1_0').html().split('<br>')[2].split(':')[1] | |
capacityStatus.push | |
'date': date, | |
'remaining': remaining, | |
'applying': applying, | |
'waiting': null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment