Skip to content

Instantly share code, notes, and snippets.

@leonsilicon
Last active March 18, 2020 01:59
Show Gist options
  • Save leonsilicon/0cc3c691918ebda0ddbe677a3d3163ee to your computer and use it in GitHub Desktop.
Save leonsilicon/0cc3c691918ebda0ddbe677a3d3163ee to your computer and use it in GitHub Desktop.
// View the application in action at the following link: https://docs.google.com/spreadsheets/d/1N69GtA6hKS8vg77rJm-12OJihQihVyvWPkg6ZFk827U
function rescueTime() {
// Returns the date formatted in the form YYYY-MM-DD
function formatDate(d) {
return d.getFullYear() + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2);
}
// Returns the time (in seconds) formatted in the form [hours]h [minutes]m
function formatTime(t) {
var f = Math.floor;
return (t > 3600 ? "" + f(t / 3600) + "h " : "") + f(t % 3600 / 60) + "m";
}
var apiKey = ""; // Insert your RescueTime API Key Here
var options = {dateStyle: 'short'};
var yesterdayStart = new Date(new Date().toDateString());
yesterdayStart.setDate(yesterdayStart.getDate() - 1);
yesterdayStart = formatDate(yesterdayStart);
var yesterdayEnd = formatDate(new Date(new Date().toDateString()));
var adURL = 'https://www.rescuetime.com/anapi/data?key=' + apiKey +
'&restrict_begin=' + yesterdayStart + '&restrict_end=' + yesterdayEnd + '&format=json';
var adjson = UrlFetchApp.fetch(adURL).getContentText();
var ad = JSON.parse(adjson);
var dsfResponse = UrlFetchApp.fetch('https://www.rescuetime.com/anapi/daily_summary_feed?key='
+ apiKey).getContentText();
var dsf = JSON.parse(dsfResponse);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var TIME_SPENT = 1;
var ONE_DAY = 1000*60*60*24;
var startDate = new Date(2019, 7, 25);
// getLastRow() returns last row *with* content, 1 row after is the divider, 2 rows after is empty
var nextRow = sheet.getLastRow() + 2;
var summaryRange = sheet.getRange(nextRow, 1, 1, 6);
var cur = dsf[0];
// Setting RescueTime Summary Stats
var values = [
[cur.date,
cur.total_duration_formatted,
cur.productivity_pulse,
cur.all_productive_duration_formatted,
cur.all_distracting_duration_formatted,
cur.neutral_duration_formatted
]
];
summaryRange.setValues(values);
// Logging RescueTime Activities
var rankingNames = ["Very Distracting", "Distracting", "Neutral", "Productive", "Very Productive"];
var rows = ad.rows;
for (var i = 0; i < rows.length && rows[i][1] >= 300; ++i) {
var name = rows[i][3], s = rows[i][1],
// formatting seconds into [hours]h [minutes]m format
time = formatTime(s);
ranking = rankingNames[rows[i][5]+2];
var curValues = [
[name, time, ranking]
];
sheet.getRange(nextRow + i, 7, 1, 3).setValues(curValues);
}
sheet.setRowHeight(nextRow + i, 2);
sheet.getRange(nextRow + i, 1, 1, 9).setBackground("black");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment