Created
July 16, 2020 11:06
-
-
Save ColinEberhardt/28e1d47a0228c9b355fc94d979b9c415 to your computer and use it in GitHub Desktop.
Google TImeline Data Scrape
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
const fetch = require("node-fetch"); | |
const xpath = require("xpath"); | |
const dom = require("xmldom").DOMParser; | |
const moment = require("moment"); | |
const namespace = `xmlns="http://www.opengis.net/kml/2.2"`; | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
const fetchData = (year, month, day) => | |
fetch( | |
/* REDACTED! */ | |
).then(r => r.text()); | |
(async () => { | |
const start = moment("2020-01-01"); | |
const end = moment("2020-07-15"); | |
for (var m = moment(start); m.isBefore(end); m.add(1, "days")) { | |
const xml = await fetchData(m.year(), m.month(), m.date()); | |
const xmlStripped = xml.replace(namespace, ""); | |
const doc = new dom().parseFromString(xmlStripped); | |
const nodes = xpath.select( | |
".//Placemark[ExtendedData/Data[@name='Distance']/value > 0]", | |
doc | |
); | |
nodes.forEach(node => { | |
const type = node.childNodes[0].childNodes[0].toString(); | |
const startTime = node.childNodes[5].childNodes[0].childNodes[0].toString(); | |
const endTime = node.childNodes[5].childNodes[1].childNodes[0].toString(); | |
const duration = moment.duration(moment(endTime).diff(moment(startTime))).asMinutes(); | |
console.log(`${m.year()}-${m.month()+1}-${m.date()}, ${type}, ${duration}`); | |
}); | |
await sleep(1000); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment