Skip to content

Instantly share code, notes, and snippets.

@MasWag
Last active August 29, 2015 14:13
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 MasWag/4eadd5aa7c414dd415ad to your computer and use it in GitHub Desktop.
Save MasWag/4eadd5aa7c414dd415ad to your computer and use it in GitHub Desktop.
ISの休講補講情報を取得してGoogle Calendarに格納するGoogle Apps Script。
/*
The MIT License (MIT)
Copyright (c) 2015 Masaki Waga
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
使い方
========
1. cache用とkey用のfusion tableと休講情報を入れるcalendarを作り、IDを持ってくる。
2. それぞれのIDをCacheTableID,KeyTableID,CancelCalendarIDというPropertyでスクリプトのプロパティで保存する。
3. key用のtableにkeyを入れる。授業番号を使う。
4. triggerとかでrunを回す。
*/
function fetch(uri,index,message) {
var response = UrlFetchApp.fetch(uri,{"contentType": 'text/html;charset=euc-jp'});
var myRegexp = /<tbody>([\s\S]*?)<\/tbody>/;
var str = response.getBlob().getDataAsString("euc-jp")
var match = myRegexp.exec(str)[index];
if (typeof match === 'undefined') {
return [];
}
var array = match.split(/<\/tr><tr>/).map(function(str){
return str.split(/<\/td>\n<td>/g);}).map(function(array){
return array.map(function(str){
return str.replace(/<(\/)?t[dr]>/g,"").replace(/\s/g,"");
});
});
var keys = getRows(PropertiesService.getScriptProperties().getProperty("KeyTableID"));
Logger.log(typeof keys);
return array.filter(function (array){
// keyにないものを消す
return keys.reduce(function(cond,key) {
return cond || key[0] == array[1];
},false);
}).map(function(array){
// 調度良い形式に変換する
var ret = new Array;
ret[0] = array[1];
ret[1] = array[2]+message;
ret[2] = array[3];
ret[3] = array[4];
var date = /(\d+)月(\d+)日\D*(\d+):(\d+)-(\d+):(\d+)/.exec(array[0]);
var newDate = new Date();
// 一ヶ月以上後じゃないと一年後ということにしない
if (newDate.getMonth() > date[1]) {
year = newDate.getFullYear() + 1;
} else {
year = newDate.getFullYear();
}
ret[4] = new Date(year,date[1] - 1,date[2],date[3],date[4]);
ret[5] = new Date(year,date[1] - 1,date[2],date[5],date[6]);
return ret;
});
}
//レコード読み出し
function getRows(tableId) {
var sql = 'SELECT * FROM ' + tableId + ' LIMIT 100';
var result = FusionTables.Query.sqlGet(sql, {
hdrs: false
});
if (result.rows) {
return result.rows;
} else {
Logger.log('No rows returned.');
}
}
function run () {
var CacheTableID = PropertiesService.getScriptProperties().getProperty("CacheTableID");
var cancels = [].concat(fetch("http://www.is.s.u-tokyo.ac.jp/no-lec.php",1,"休講"),
fetch("http://www.is.s.u-tokyo.ac.jp/no-lec.php",2,"補講"),
fetch("http://www.is.s.u-tokyo.ac.jp/report.php",1,"試験"),
fetch("http://www.is.s.u-tokyo.ac.jp/report.php",2,"レポート"));
Logger.log(cancels);
var rows = getRows(CacheTableID);
Logger.log(rows);
if (typeof rows === 'undefined') {
cancels.map(addEvent);
} else {
cancels.map(function(cancel){
var str = cancel.toString();
if (rows.reduce(function(cond,row) {
Logger.log(row.toString());
Logger.log(str);
return cond && row.toString() != str;
},true)) {
addEvent (cancel);
}
});
}
}
// レコード追加
function addEvent(array) {
Logger.log(array);
var sql = "INSERT INTO " + PropertiesService.getScriptProperties().getProperty("CacheTableID")
+ " ('Class ID','Name','Teacher','Location','StartDate','EndDate')"
+ " VALUES ('" + array[0] + "','" + array[1] + "','" + array[2] + "','" + array[3] + "','" + array[4] + "','" + array[5] + "')";
FusionTables.Query.sql(sql);
Logger.log(new Date(array[4]));
var event = CalendarApp.getCalendarById(PropertiesService.getScriptProperties().getProperty("CancelCalendarID")).createEvent(array[1],
array[4],
array[5],
{location: array[3]});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment