Skip to content

Instantly share code, notes, and snippets.

@hasegawayosuke
Last active July 22, 2020 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasegawayosuke/84ce69034e16b6403f08a4c666d69d42 to your computer and use it in GitHub Desktop.
Save hasegawayosuke/84ce69034e16b6403f08a4c666d69d42 to your computer and use it in GitHub Desktop.
東京ドームのイベント情報を通知
var slack_token = "xoxp-xxxxxxxxxxx-....";
var slack_channel = "#channnel";
function scraping(day /* 1..31 */){
var url = "https://www.tokyo-dome.co.jp/dome/event/schedule.html";
var res, pattern, html, pos1, pos2, text;
res = UrlFetchApp.fetch(url);
if (typeof day === "number" && day < 10) {
day = "0" + day;
}
if (res.getResponseCode() === 200) {
html = res.getContentText();
pattern = "<span class=\"c-mod-calender__day\">" + day + "</span>";
pos1 = html.indexOf(pattern);
pos2 = html.indexOf('</td>', pos1 + pattern.length);
if (pos1 < 0 || pos2 < 0) {
text = "イベント情報の取得に失敗しました"
} else {
html = html.substr(pos1, pos2 - pos1);
html = html.replace(/<[^>]+>/g, "");
text = html.replace(/[\s]+/g, " ");
if (/^[\d]+\s\(.\)\s*$/.test(text)) {
text += "イベント情報はありません"
}
}
} else {
text = "イベント情報の取得に失敗しました。HTTP Status=" + res.getResponseCode();
}
postToSlack(text);
}
//Slackに送信
function postToSlack(text){
var escape = function(s){
if (s === undefined || s === null) s = "";
return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
};
var url = 'https://slack.com/api/chat.postMessage';
var payload = {
token : slack_token,
channel : slack_channel,
username : '東京ドーム イベント情報',
attachments : []
};
payload.text = escape(text);
payload.attachments = JSON.stringify(payload.attachments);
var res = UrlFetchApp.fetch(url, { method : 'post', payload : payload });
}
function trigger(){
var d = new Date();
scraping(d.getDate());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment