Created
April 21, 2017 08:18
-
-
Save janzikan/711e383668123dde7cde6a7417fa219e to your computer and use it in GitHub Desktop.
Google Apps Script: Mastodon stats scraping
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
function addData() { | |
var values = fetchStats(); | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheets()[0]; | |
sheet.appendRow([currentDateTime(), values[0], values[1]]); | |
} | |
function fetchStats() { | |
var url = 'https://instances.mastodon.xyz/list'; | |
var content = UrlFetchApp.fetch(url).getContentText(); | |
return getValues(content); | |
} | |
function getValues(content) { | |
return content.match(/<strong>(.*?)<\/strong>/g).slice(0, 2).map(function(val) { | |
return val.replace(/<\/?strong>/g, ''); | |
}); | |
} | |
function currentDateTime() { | |
var date = new Date(); | |
var day = date.getUTCDate(); | |
var month = date.getUTCMonth() + 1; | |
var year = date.getUTCFullYear(); | |
var hours = pad(date.getUTCHours(), 2); | |
var minutes = pad(date.getUTCMinutes()); | |
return day + "." + month + "." + year + " " + hours + ":" + minutes; | |
} | |
function pad(num, size) { | |
var s = num.toString(); | |
while (s.length < size) s = "0" + s; | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment