Skip to content

Instantly share code, notes, and snippets.

@toofusan
Created October 23, 2016 02:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toofusan/c5a8a0152349a8ccd9dcc59bf21720c9 to your computer and use it in GitHub Desktop.
Save toofusan/c5a8a0152349a8ccd9dcc59bf21720c9 to your computer and use it in GitHub Desktop.
Navitimeをスクレイピングして電車時刻検索を行う
# ---------------------
# Description
# ---------------------
# hubot用の電車時刻検索スクリプト(Navitimeをスクレイピング)
# 'train 銀座 渋谷 1730' -> 銀座を出て17:30に渋谷に到着する電車を検索
# 'train now 銀座 渋谷' -> いまから銀座を出て渋谷に到着する電車を検索
module.exports = (robot) ->
# 到着時刻を指定して電車検索
robot.hear /train (\S*) (\S*) (\d{4})/i, (response) ->
nowTime = new Date()
startStation = response.match[1]
goalStation = response.match[2]
month = nowTime.getFullYear() + '/' + (nowTime.getMonth() + 1)
day = nowTime.getDate()
hour = response.match[3].slice(0,2)
minute = response.match[3].slice(2)
if(nowTime.getHours() > Number(hour))
day += 1; # 現在時刻より前の時刻を記載した場合、次の日の検索を行う
basis = 0 # 到着時刻で検索
response.send month + '/' + day + ' ' + hour + ':' + minute + ' に' + goalStation + '駅に到着する'
searchTrain(startStation, goalStation, month, day, hour, minute, basis, response)
# 現在時刻から出発する電車検索
robot.hear /train now (\S*) (\S*)/i, (response) ->
nowTime = new Date()
startStation = response.match[1]
goalStation = response.match[2]
month = nowTime.getFullYear() + '/' + (nowTime.getMonth() + 1)
day = nowTime.getDate()
hour = nowTime.getHours()
minute = nowTime.getMinutes()
basis = 1 # 出発時刻で検索
response.send 'いまから' + goalStation + '駅に向かう'
searchTrain(startStation, goalStation, month, day, hour, minute, basis, response)
# Navitimeスクレイピング関数
searchTrain = (startStation, goalStation, month, day, hour, minute, basis, response) ->
searchInfo = {
orvStationName: startStation,
dnvStationName: goalStation,
month : month,
day : day,
hour : hour,
minute : minute,
basis : basis,
}
client.fetch 'https://www.navitime.co.jp/', (err, $, res, body) ->
$('#transfer_form').submit searchInfo, (err, $, res, body) ->
response.send startStation + ' ' + $('.summary_list li:first-child dt').text() + ' ' + goalStation
response.send res.request.uri.href
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment