0
0

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 3 years have passed since last update.

Google App Scriptでベイスターズの試合後に借金を数える

Last updated at Posted at 2021-09-01

動機

  • なぜGAS?
    • いろいろ簡単だった
      • 環境構築不要
      • JavaScript
      • 日時で動かせる
      • 無料
  • なぜベイスターズ?
    • 弱かった
      • 新監督
      • 戦力の流出
    • 今年はなぜかオリックスが強かった

流れ

ベイスターズの試合の日、かつ敗北した日に開幕から現在までの勝敗をカウントする

試合スケジュールはGoogle Calenderから取得

正規表現で勝ち、負け、引き分け、試合中止を出すカウントする

結果をdiscordで教える

ベイスターズの試合日

Googleカレンダーがあるのでそれを拝借
https://calendar.google.com/calendar/u/0/embed?src=bnBiXzNfJTU5b2tvaGFtYSslNDRlJTRlJTQxKyU0MmF5JTUzdGFycyNzcG9ydHNAZ3JvdXAudi5jYWxlbmRhci5nb29nbGUuY29t

カレンダーを読み込む

CalendarApp.getCalendarById(id)で読み込み、getEvents(開幕日から今日)で回す

const bsCalId = "npb_3_%59okohama+%44e%4e%41+%42ay%53tars#sports@group.v.calendar.google.com"
const bsCal = CalendarApp.getCalendarById(id)
const today = new Date()

bsCal.getEvents(new Date("2021-03-26 00:00:00"), today).map(l => {
      if (gameResult(l.getTitle()) === "W") {
        countWin++
      } else if (gameResult(l.getTitle()) === "L") {
        countLose++
      }
    })

試合結果を正規表現でごにょごにょする

試合前
BayStars @ Swallows
試合後
BayStars (33) @ Swallows (4)
で記載されるので、
WDLNの4つのどれかを返す

// in : BayStars (33) @ Swallows (4)
// out : W D L N
function gameResult (game) {
  const bsSocre = game.replace(/.*BayStars \(([0-9]*)\).*/g,"$1")
  const otherSocre = game.replace(/BayStars \([0-9]*\)/g,"").replace(/.*\(([0-9]*)\).*/g,"$1")

  // 試合中止
  if (game === bsSocre || game === otherSocre) {
    return "N"
  } else if (bsSocre - otherSocre > 0) {
    return "W"
  } else if (bsSocre - otherSocre < 0) {
    return "L"
  }
  return "D"
}

GASからdiscordにpost

qiitaにいっぱい載ってるので略
割と簡単にできた

function post(url, message) {
  const payload = {
    'content' : message,
    'parse' : 'full',
  }

  const params = {
    'method' : 'post',
    'payload' : payload,
    'muteHttpExceptions': true,
  }
  
  UrlFetchApp.fetch(url, params)
}

GASを日時で動かす

トリガー

トリガーを追加

実行する関数を選択、時間ベースのトリガーのタイプを選択(時間を選択)

結果

リーゼントがとても伸びた(正式にはポンパドールらしい)
スクリーンショット 2021-08-30 2.08.20.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?