Skip to content

Instantly share code, notes, and snippets.

@hiiamyes
Created September 13, 2016 17:36
Show Gist options
  • Save hiiamyes/65439c45897a85f5c90f677b85a0d17b to your computer and use it in GitHub Desktop.
Save hiiamyes/65439c45897a85f5c90f677b85a0d17b to your computer and use it in GitHub Desktop.
bed crawler
let moment = require('moment');
let cheerio = require('cheerio');
let async = require('asyncawait/async');
let await = require('asyncawait/await');
let rp = require('request-promise');
const URL = 'https://npm.cpami.gov.tw/bed_1.aspx';
const getASPState = () => {
return new Promise( (resolve, reject) => {
rp({
url: URL,
transform: body => cheerio.load(body)
})
.then( $ => {
resolve({
__EVENTVALIDATION: $('#__EVENTVALIDATION').val(),
__VIEWSTATE: $('#__VIEWSTATE').val()
})
});
})
}
const getBedStatus = ({ASPState, room, monthOffset}) => {
return new Promise( (resolve, reject) => {
let date = monthOffset ? {
ctl00$ContentPlaceHolder1$ddlMonth: moment().add(monthOffset, 'months').month() + 1,
ctl00$ContentPlaceHolder1$ddlYear: moment().add(monthOffset, 'months').year(),
ctl00$ContentPlaceHolder1$hidMonth: moment().add(monthOffset, 'months').month() + 1,
ctl00$ContentPlaceHolder1$hidYear: moment().add(monthOffset, 'months').year(),
} : {};
rp.post({
url: URL,
form: Object.assign({}, ASPState, date, {
ctl00$ContentPlaceHolder1$rooms: room,
ctl00$ScriptManager1: 'ctl00$ScriptManager1|ctl00$ContentPlaceHolder1$btnsearch',
ctl00$ContentPlaceHolder1$btnsearch: '查詢'
}),
transform: body => cheerio.load(body)
})
.then( $ => {
resolve({
$,
ASPState: {
__EVENTVALIDATION: $('#__EVENTVALIDATION').val(),
__VIEWSTATE: $('#__VIEWSTATE').val()
}
})
});
})
}
const parser = ($) => {
return new Promise( (resolve, reject) => {
let capacityStatus = [];
for (var i = 1; i <= 42; i++) {
const indexString = `0${i}`.substr(-2);
const date = $(`#ContentPlaceHolder1_cc_${indexString} a`)
if (date.length) {
const [year, month, day] = date.attr('href').split('sdate=')[1].split('-');
const remaining = parseInt($(`#ContentPlaceHolder1_cc_${indexString} a span:nth-of-type(1)`).text());
const waiting = parseInt($(`#ContentPlaceHolder1_cc_${indexString} a span:nth-of-type(2)`).text());
const applying = parseInt($(`#ContentPlaceHolder1_cc_${indexString} a span:nth-of-type(3)`).text());
capacityStatus.push({
date: moment.utc(`${parseInt(year) + 1911}-${month}-${day}`).format(),
remaining,
applying: waiting + applying,
isDrawn: true,
});
}
}
resolve(capacityStatus);
})
}
const getBeds = async ((room) => {
let ASPState = await (getASPState());
let thisMonth = await (getBedStatus({ASPState, room, monthOffset: 0}));
let nextMonth = await (getBedStatus({ASPState: thisMonth.ASPState, room, monthOffset: 1}))
thisMonth = await (parser(thisMonth.$));
nextMonth = await (parser(nextMonth.$));
return thisMonth.concat(nextMonth);
})
exports.get = getBeds;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment