LoginSignup
4
9

More than 5 years have passed since last update.

Google Apps Script で京急線と京浜東北線の遅延情報を【無料】でSlack 通知の巻

Last updated at Posted at 2018-05-23

目的

最近、電車遅延が多く、遅延情報を知りたい

目標

遅延があれば、アクティブに通知して欲しい
無料で!!!
土日・祝日は除く

やったこと

Google Apps Script に京急線と京浜東北線の情報を定時で読み込ませ、トラブルあれば、Slack へ通知する。
 GAS -> 土日じゃないこと
-> 祝日じゃないこと(日本の祝日情報をgoogle カレンダーAPI から取得)
->yahoo 運行情報
-> "平常運転"以外
        -> 遅延理由の抜き出し
->slck へpush

GAS のコード

your web hook 部分にslack のweb hook 情報を入れて下さい。

trainlate
function trainlate() {

  var currentDate = new Date();
  var weekday = currentDate.getDay();
  var date = Utilities.formatDate( currentDate, 'Asia/Tokyo', 'M月d日 HH時mm分');

  if (weekday == 0 || weekday == 6) {
    return;
  }
  var calendar = CalendarApp.getCalendarById('ja.japanese#holiday@group.v.calendar.google.com');
  if (calendar.getEventsForDay(currentDate, {max: 1}).length > 0) {
    return;
  }

  //keihintohoku line info
  var yahoodata = UrlFetchApp.fetch("https://transit.yahoo.co.jp/traininfo/detail/22/0/").getContentText();
  if(yahoodata.indexOf('現在、事故・遅延に関する情報はありません') > -1){
    // match the word
  }else{    
    //get error info
    var yahoodatastart = yahoodata.indexOf('og:description" content="');
    //4 japanese word only
    yahoodatastart += 25;
    var yahoodataend = yahoodata.indexOf('の情報です。');
    var yahoodataoutput = yahoodata.substring(yahoodatastart, yahoodataend);

    postMessage("\n" + "◆京浜東北線 " + date + yahoodataoutput + "\n",'your web hook');
  }


  //keikyuuhonsen line info
  var yahoodata2 = UrlFetchApp.fetch("https://transit.yahoo.co.jp/traininfo/detail/120/0/").getContentText();
  if(yahoodata2.indexOf('現在、事故・遅延に関する情報はありません') > -1){
    // match the word
  }else{    
    //get error info
    var yahoodatastart2 = yahoodata2.indexOf('og:description" content="');
    //4 japanese word only
    yahoodatastart2 += 25;
    var yahoodataend2 = yahoodata2.indexOf('の情報です。');
    var yahoodataoutput2 = yahoodata2.substring(yahoodatastart2, yahoodataend2);

    postMessage("\n" + "◆京急本線 " + date + yahoodataoutput2 + "\n",'your web hook');

  }


}


function postMessage(message, hookPoint){
  var payload = {
    "text": message,
  }
  var options = {
    "method": "POST",
    "payload": JSON.stringify(payload),
    "headers": {
      "Content-type": "application/json",
    }
  }
    var response = UrlFetchApp.fetch(hookPoint, options);

  if (response.getResponseCode() == 200) {
    return response;
  }
  return false;
} 

github
https://github.com/HaHatake/gas_trainlate

スケジュールに7-8時で、trainlate を実行すれば、ok!
これで、遅刻とはおさらばだぜ!

4
9
6

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
4
9