8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

slack hubotを使って遅延情報を取得する

Last updated at Posted at 2018-02-24

slack hubotを使ってみる
の続編な感じです。

毎朝決まった時間に、使う電車の運行情報をSlackに投稿するBot
これを参考にさせていただいて、通勤に使う電車の遅延情報を取得したいと思います!

完全に引用させてもらいます!

#事前準備
まずは、今回使用するモジュールをインストールします。

# スクレイピングを行う
sudo npm install cheerio-httpcli --save
# 定期タスクを動かす
sudo npm install cron --save

package.json に、インストールしたモジュール情報が登録されていれば準備完了です。

package.json
 "dependencies": {
   "cheerio-httpcli": "^0.6.9",
   "cron": "^1.0.5",
 }

こんな感じ。

$ cat package.json 
{
  "name": "slackhubot",
  "version": "0.0.0",
  "private": true,
  "author": "<author> <mailaddress>",
  "description": "slackhubot",
  "dependencies": {
    "cheerio-httpcli": "^0.7.2",
    "cron": "^1.3.0",
    "hubot": "^2.19.0",
    "hubot-diagnostics": "0.0.2",
    "hubot-google-images": "^0.2.7",
    "hubot-google-translate": "^0.2.1",
    "hubot-help": "^0.2.2",
    "hubot-heroku-keepalive": "^1.0.3",
    "hubot-maps": "0.0.3",
    "hubot-pugme": "^0.1.1",
    "hubot-redis-brain": "0.0.4",
    "hubot-rules": "^0.1.2",
    "hubot-scripts": "^2.17.2",
    "hubot-shipit": "^0.2.1",
    "hubot-slack": "^4.4.0"
  },
  "engines": {
    "node": "0.10.x"
  }
}

さらにさらにほぼこのまま使用させてもらいました。
https://github.com/hotatekaoru/myHubotScripts/blob/master/train-info.coffee
ちょっと改造して、、京浜東北線と京急本線を取得するのと、定期実行のあたりを変えました。

cheerio = require 'cheerio-httpcli'
cronJob = require('cron').CronJob

module.exports = (robot) ->
  
  searchAllTrain = (msg) ->
    # send HTTP request
    baseUrl = 'https://transit.yahoo.co.jp/traininfo/gc/13/'
    cheerio.fetch baseUrl, (err, $, res) ->
      if $('.elmTblLstLine.trouble').find('a').length == 0
        msg.send "事故や遅延情報はありません"
        return
      $('.elmTblLstLine.trouble a').each ->
        url = $(this).attr('href')
        cheerio.fetch url, (err, $, res) ->
          title = ":warning: #{$('h1').text()} #{$('.subText').text()}"
          result = ""
          $('.trouble').each ->
            trouble = $(this).text().trim()
            result += "- " + trouble + "\r\n"
          msg.send "#{title}\r\n#{result}"

  robot.respond /train (.+)/i, (msg) ->
    target = msg.match[1]
    # 京浜東北線
    jr_kt = 'http://transit.yahoo.co.jp/traininfo/detail/22/0/'
    # 京急本線
    kq = 'https://transit.yahoo.co.jp/traininfo/detail/120/0/'
    if target == "kq"
      searchTrain(kq, msg)
    else if target == "jr_kt"
      searchTrain(jr_kt, msg)
    else if target == "all"
      searchAllTrain(msg)
    else
      msg.send "#{target}は検索できなし( ˘ω˘ ) usage: @world_conquistador [kq | jr_kt | all]"

  searchTrain = (url, msg) ->
    cheerio.fetch url, (err, $, res) ->
      title = "#{$('h1').text()}"
      if $('.icnNormalLarge').length
        msg.send ":ok_woman: #{title}は遅延してないので安心しろ。"
      else
        info = $('.trouble p').text()
        msg.send ":warning: #{title}は遅延しとる。フザケンナ。\n#{info}"
        
  # cronJobの引数は時間曜日の順番
  new cronJob('0 20,30,40,50 8 * * 1-5', () ->
    # 京急本線(Yahoo!運行情報から選択したURLを設定する)
    kq = 'https://transit.yahoo.co.jp/traininfo/detail/120/0/'
    # 京浜東北線
    jr_kt = 'http://transit.yahoo.co.jp/traininfo/detail/22/0/'
    searchTrainCron(kq)
    searchTrainCron(jr_kt)
  ).start()
  
  new cronJob('0 30,59 18 * * 1-5', () ->
    # 京急本線(Yahoo!運行情報から選択したURLを設定する)
    kq = 'https://transit.yahoo.co.jp/traininfo/detail/120/0/'
    # 京浜東北線
    jr_kt = 'http://transit.yahoo.co.jp/traininfo/detail/22/0/'
    searchTrainCron(kq)
    searchTrainCron(jr_kt)
  ).start()
  
  searchTrainCron = (url) ->
    cheerio.fetch url, (err, $, res) ->
      #路線名(Yahoo!運行情報から正式名称を取得)
      title = "#{$('h1').text()}"
      if $('.icnNormalLarge').length
        # 通常運転の場合
        #robot.send {room: "#random"}, "#{title}は遅延してないので安心しろ。"
      else
        # 通常運転以外の場合
        info = $('.trouble p').text()
        robot.send {room: "#train_info"}, ":warning: #{title}は遅延しとる。フザケンナ。\n#{info}"

でこれを動かすと
スクリーンショット 2018-02-24 23.29.59.png

きました。

ありがとうございました。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?