Skip to content

Instantly share code, notes, and snippets.

@weiland
Last active November 11, 2022 14:33
Show Gist options
  • Save weiland/c7a5111eb394257ae73b39e1636fe01e to your computer and use it in GitHub Desktop.
Save weiland/c7a5111eb394257ae73b39e1636fe01e to your computer and use it in GitHub Desktop.
Total Netflix Viewing History in Hours and CO2

Total Netflix Viewing History in Hours and CO2

Before you start, never click untrusted links and never paste unknown code from unknown sources, so better read the source code before using it.

Make sure to be logged in at netflix.com.
Visit the Viewing Activity page: https://www.netflix.com/viewingactivity.

Paste the script (main.js) below in the JavaScript Console.

When the main function is invoked the script will start to fetch the entire history and accumulate the total amount of hours watched.

// first page of content
const START_PAGE = 0;
// fixed size, increasing this number has no effect
const ITEMS_PER_PAGE = 20;
const fetchPage = (page) => fetch(`https://www.netflix.com/api/shakti/vb850f007/viewingactivity?pg=${page}&pgSize=${ITEMS_PER_PAGE}`)
.then(response => response.json());
// reducer
const addDuration = (accumulator, item) => accumulator + item.duration;
async function main() {
const json = await fetchPage(START_PAGE);
// total items
const totalItemsCount = json.vhSize;
let items = json.viewedItems;
// first date we obtain, oldest item
const lastDate = items[0].date;
console.debug(`${totalItemsCount} items has been watched.`);
// calculate the amount of requests to be made
const maxRequests = Math.ceil(totalItemsCount / ITEMS_PER_PAGE);
let totalSeconds = items.reduce(addDuration, 0);
let page;
// since we already fetched the first page (page 0), we start at one.
for (let i = 1; i < maxRequests; i++) {
page = await fetchPage(i);
items = page.viewedItems;
totalSeconds = items.reduce(addDuration, totalSeconds);
}
// fallback
let firstDate = lastDate;
if (items.length > 0) {
firstDate = items[items.length - 1].date;
}
const days = Math.round((lastDate - firstDate) / 1000 / 60 / 60 / 24);
console.debug(`from ${new Date(firstDate)} to ${new Date(lastDate)} (${days}) Tage`);
console.debug(`${totalSeconds}s (ca ${Math.round(totalSeconds/60/60)}h) has been streamed`);
// in grams (according to Lean-ICT-Report) alternatively 53,3grams (other sources suggest)
const co2perMinute = 3.8;
console.debug(`${totalSeconds}s ${Math.round(totalSeconds/60 * co2perMinute / 1000)}kg co_2 was produced.`);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment