Last active
September 10, 2021 01:09
-
-
Save chengkangzai/4b536c6c5d9b7bd98b7e2e9d4d5fa300 to your computer and use it in GitHub Desktop.
Website Health check with google Sheet
This file contains hidden or 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 spreadSheet = SpreadsheetApp.openById('####') | |
/** | |
* Helper Section | |
*/ | |
function ping(url) { | |
try { | |
return UrlFetchApp.fetch(url).getResponseCode(); | |
} catch (err) { | |
return 500; | |
} | |
} | |
function log(url, resCode) { | |
var timestamp = new Date().toLocaleString(); | |
var logSheet = spreadSheet.getSheetByName('Log'); | |
for (let i = 1; i < 1000000000000; i++) { | |
if (logSheet.getRange('A' + i).getValue() == "") { | |
logSheet.getRange('A' + i).setValue(timestamp); | |
logSheet.getRange('B' + i).setValue(url); | |
logSheet.getRange('C' + i).setValue(resCode); | |
return; | |
} | |
} | |
} | |
function report(url, resCode) { | |
var discordUrl = '####'; | |
var payload = JSON.stringify({ | |
content: `URL: ${url} \nStatus Code: ${resCode} \nOn : ${new Date().toLocaleString()}` | |
}); | |
var params = { | |
headers: { 'Content-Type': 'application/json' }, | |
method: "POST", | |
payload: payload, | |
muteHttpExceptions: true | |
}; | |
UrlFetchApp.fetch(discordUrl, params); | |
} | |
/** | |
* Main Thread | |
*/ | |
function main() { | |
for (let i = 2; i < 5; i++) { | |
var urlSheet = spreadSheet.getSheetByName('Url'); | |
var url = urlSheet.getRange('A' + i).getValue(); | |
var resCode = ping(url); | |
log(url, resCode); | |
if (resCode >= 400) { | |
report(url, resCode); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment