Skip to content

Instantly share code, notes, and snippets.

@CodySchrank
Last active May 14, 2019 15:59
Show Gist options
  • Save CodySchrank/a2fa2f29aebf3549f372fbb3550ed911 to your computer and use it in GitHub Desktop.
Save CodySchrank/a2fa2f29aebf3549f372fbb3550ed911 to your computer and use it in GitHub Desktop.
// A script that looks through your Netflix viewing activity and
// tallys up how much time you've spent watching Netflix.
// And then it exports to a json file.
//
// INSTRUCTIONS TO USE:
// Open https://www.netflix.com/WiViewingActivity and the developer console
// Copy and paste this script into the developer console and press enter
//
(function () {
var ex = function (data, filename) {
if (typeof data === "object") {
data = JSON.stringify(data, undefined, 4)
}
var blob = new Blob([data], {
type: 'text/json'
}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
var fetchAllViewedItems = function () {
var deferred = jQuery.Deferred();
var viewedItems = [];
(function fetchPage(page) {
data = netflix.reactContext.models.serverDefs.data;
url = data.SHAKTI_API_ROOT + '/' + data.BUILD_IDENTIFIER;
jQuery.getJSON(url + '/viewingactivity?pg=' + page).done(function (json) {
viewedItems = viewedItems.concat(json.viewedItems);
console.log('Fetched %s viewed items', viewedItems.length);
if (json.viewedItems.length == json.size) {
fetchPage(++page);
} else {
deferred.resolve(viewedItems);
}
}).fail(deferred.reject);
})(0);
return deferred.promise();
};
fetchAllViewedItems().then(function (viewedItems) {
var totalTime = viewedItems.reduce(function (runningTotal, viewedItem) {
return runningTotal + viewedItem.bookmark;
}, 0);
var days = Math.floor(totalTime / 60 / 60 / 24);
var hours = Math.floor((totalTime / 60 / 60) % 24);
var minutes = Math.round((totalTime / 60) % 60);
console.log('According to your viewing history, you have cumulatively watched %i days, %i hours and %i minutes of Netflix', days, hours, minutes);
ex(viewedItems, "netflix-history.json");
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment