Created
February 13, 2020 01:32
-
-
Save tirtawr/1167863c14cbe87736a8eb9b2da551c6 to your computer and use it in GitHub Desktop.
Extract YouTube video watch count from json file given by google takout
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
'use strict' | |
const fs = require('fs') | |
let rawdata = fs.readFileSync('watch-history.json') | |
let history = JSON.parse(rawdata) | |
let timestamps = {} | |
for (const video of history) { | |
const time = new Date(video.time) | |
const key = `${time.getFullYear()}-${time.getMonth()+1}-${time.getDate()}` | |
if (timestamps[key]) { | |
timestamps[key].push(time) | |
} else { | |
timestamps[key] = [ time ] | |
} | |
} | |
let timestampCounts = {} | |
const keys = Object.keys(timestamps) | |
for (const key of keys) { | |
timestampCounts[key] = timestamps[key].length | |
} | |
// Find out max videos watched | |
let maxWatched = 0 | |
for (const key of keys) { | |
timestampCounts[key] = timestamps[key].length | |
if (timestamps[key].length > maxWatched) { | |
maxWatched = timestamps[key].length | |
} | |
} | |
// Write data to JSON | |
let data = JSON.stringify(timestampCounts) | |
fs.writeFileSync('watch-time-count.json', data) | |
// Write data to CSV | |
let csvData = 'date,videos\n' | |
for (const key of keys) { | |
csvData += `${key},${timestampCounts[key]}\n` | |
} | |
fs.writeFileSync('watch-time-count.csv', csvData) | |
console.log(`Export done, ${history.length} records processed`) | |
console.log(`Maximum videos watched at one day = ${maxWatched}`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment