Last active
May 25, 2017 08:22
-
-
Save dolpen/c9790f51ca679c5c78203fc7f2e3f183 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Discord = (function(){ | |
var w = function(id, token){ | |
this.id = id; | |
this.token = token; | |
}; | |
w.prototype = { | |
_endpoint: function(){ | |
return 'https://discordapp.com/api/webhooks/' + this.id + '/' + this.token; | |
}, | |
send: function(content){ | |
UrlFetchApp.fetch(this._endpoint(), { 'method': 'post', 'payload': { 'content': content } }); | |
} | |
}; | |
return w; | |
})(); | |
var SpreadSheet = (function () { // 完成した内部クラスの提供 | |
var c = function (sid, sname) { | |
this.inner = SpreadsheetApp.openById(sid).getSheetByName(sname); | |
}; | |
c.prototype = { | |
addRows: function (rows) { | |
var height = rows.length; | |
if (height <= 0)return; | |
var width = rows[0].length; | |
if (width <= 0)return; | |
var headRow = this.inner.getLastRow() + 1; | |
Logger.log([headRow, 1, headRow + height - 1, width]); | |
this.inner.getRange(headRow, 1, height, width).setValues(rows); | |
}, | |
addRow: function (row) { | |
this.addRows([row]); | |
}, | |
setRow: function (row, toRow) { | |
var width = row.length; | |
if (width <= 0)return; | |
this.inner.getRange(toRow, 1, 1, width).setValues([row]); | |
}, | |
addCell: function (cell) { | |
this.addRows([[cell]]); | |
}, | |
getDataFromRow: function (targetRow) { | |
return this.inner.getRange(targetRow, 1, 1, this.inner.getLastColumn()).getDisplayValues()[0]; | |
}, | |
getAllRows: function () { | |
return this.inner.getRange(1, 1, this.inner.getLastRow(), this.inner.getLastColumn()).getDisplayValues(); | |
} | |
}; | |
return c; | |
})(); | |
var KVS = (function () { | |
var e = function (k, v, r, s) { | |
this.r = r; | |
this.k = k; | |
this.v = v; | |
this.s = s; | |
}; | |
e.prototype = { | |
commit: function () { | |
this.s.setRow([this.k, '' + this.v], this.r); | |
}, | |
update: function (v) { | |
this.v = v; | |
}, | |
updateWithCommit: function (v) { | |
this.v = v; | |
this.s.setRow([this.k, '' + this.v], this.r); | |
}, | |
get: function () { | |
return this.v; | |
}, | |
}; | |
var u = function (ss) { | |
this.inner = ss; | |
this.entities = null; | |
this.next = 0; | |
this._readEntities(); | |
}; | |
u.prototype = { | |
_readEntities: function () { | |
var rows = this.inner.getAllRows(); | |
var indexes = {}; | |
for (var i = 1; i < rows.length; i++) { | |
indexes[rows[i][0]] = new e( | |
rows[i][0], | |
rows[i][1], | |
i + 1, | |
this.inner | |
); | |
} | |
this.entities = indexes; | |
this.next = rows.length; | |
}, | |
getEntity: function (key) { | |
var r = this.entities[key]; | |
if (!r) r = new e( | |
key, | |
'', | |
++this.next, | |
this.inner | |
); | |
return r; | |
} | |
}; | |
return u; | |
})(); | |
var settings = { | |
spreadsheet_id: '***************************************', // スプレッドシートのURLに付いてる長い奴 | |
sheet_meta: 'meta', // (データ管理用)↑にシートを追加し、付けたシート名を書いておく | |
sheet_logs: 'logs', // (ログ収集先)↑にシートを追加し、付けたシート名を書いておく | |
store: 'https://store.nintendo.co.jp/category/NINTENDOSWITCH/HAC_S_KAYAA.html', | |
// https://discordapp.com/api/webhooks/' + discord_wh_id + '/' + discord_wh_token | |
discord_wh_id : '000000000000000000', | |
discord_wh_token : '********************************************************************' | |
}; | |
function myFunction() { | |
var discord = new Discord( | |
settings.discord_wh_id, | |
settings.discord_wh_token | |
); | |
var meta = new KVS(new SpreadSheet(settings.spreadsheet_id, settings.sheet_meta)); | |
var data = new SpreadSheet(settings.spreadsheet_id, settings.sheet_logs); | |
var prev_state_cache = meta.getEntity('state'); | |
var prev_state_value = prev_state_cache.get().toLowerCase(); | |
var prev_state = prev_state_value == 'true'; | |
var resp = UrlFetchApp.fetch(settings.store, {'method': 'get'}); | |
var doc = resp.getContentText(); | |
var btn_txt = doc.split('<div class="add_area">')[1].split('</div>')[0].trim().replace(/<[^>]*>/g,''); | |
var state = btn_txt != 'SOLD OUT'; // button message | |
if(state != prev_state){ | |
data.addRow([''+(new Date()), state]); | |
discord.send(state ? ('再販 at '+settings.store) : '完売'); | |
prev_state_cache.updateWithCommit(state ? 'true' : 'false'); | |
} | |
meta.getEntity('updated').updateWithCommit((''+new Date())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment