Skip to content

Instantly share code, notes, and snippets.

@janzikan
Created April 21, 2017 08:18
Show Gist options
  • Save janzikan/711e383668123dde7cde6a7417fa219e to your computer and use it in GitHub Desktop.
Save janzikan/711e383668123dde7cde6a7417fa219e to your computer and use it in GitHub Desktop.
Google Apps Script: Mastodon stats scraping
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