Created
April 4, 2023 07:20
-
-
Save suhankins/5e965bd1481545065de5a4d066d27760 to your computer and use it in GitHub Desktop.
This script parses Google Maps timeline data and outputs how many hours you've spent at a specific location.
This file contains hidden or 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
/** | |
* This script parses Google Maps timeline data and outputs how many hours you've spent at a specific location. | |
* I used it to calculate how many hours I've spent at work. | |
*/ | |
// Replace these with your own latitude and longitude | |
// You can put here only first X digits of your coordinates, the rest will be rounded | |
// For example, if your coordinates are 12.345678, 12.345678, you can put here 12345, 12345 | |
const latitude = /* Latitude goes here */; | |
const longitude = /* Longitude goes here */; | |
const latitudePrecision = 10 ** (9 - latitude.toString().length); | |
const longitudePrecision = 10 ** (9 - longitude.toString().length); | |
const fs = require('fs'); | |
const path = process.argv[2]; | |
if (!path) { | |
console.log('Please provide a path to Google Maps timeline data'); | |
process.exit(1); | |
} | |
const rawFile = fs.readFileSync(path, { encoding: 'utf8' }); | |
const data = JSON.parse(rawFile); | |
const workdays = data.timelineObjects.flatMap((item) => { | |
if (!item.placeVisit) return []; | |
const location = item.placeVisit.location; | |
if ( | |
Math.round(location.latitudeE7 / latitudePrecision) !== latitude || | |
Math.round(location.longitudeE7 / longitudePrecision) !== longitude | |
) | |
return []; | |
const duration = item.placeVisit.duration; | |
return { | |
date: new Date(duration.startTimestamp).toISOString().split('T')[0], | |
duration: | |
(new Date(duration.endTimestamp) - | |
new Date(duration.startTimestamp)) / | |
1000 / | |
60 / | |
60, | |
}; | |
}); | |
console.log(workdays); | |
console.log(`Total: ${workdays.reduce((acc, item) => acc + item.duration, 0)} hours`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment