Skip to content

Instantly share code, notes, and snippets.

@floresce
Forked from tokland/google-timeline-download.sh
Last active July 12, 2022 03:40
Show Gist options
  • Select an option

  • Save floresce/cb8a9e5eb73b8e5aa529749746d3be3d to your computer and use it in GitHub Desktop.

Select an option

Save floresce/cb8a9e5eb73b8e5aa529749746d3be3d to your computer and use it in GitHub Desktop.
Get KML of Google Maps Timeline for a date range
#!/bin/bash
set -e -u -o pipefail
# Usage:
#
# Export a cookies.txt file from a browser session that is logged in timeline.google.com.
# Use an add-on/extension (Export cookies, Get cookies.txt, ...). Now run the script for the
# desired period:
#
# $ bash google-timeline-download.sh cookies.txt 2022-01-01 2022-02-20 > out.kml
#
# Dependencies: curl, perl-xml-twig
debug() {
echo "$@" >&2
}
join_by() {
local IFS="$1"
shift
echo "$*"
}
date_range() {
local from=$1 to=$2
local days=$((($(date '+%s' -d "$to") - $(date '+%s' -d "$from")) / (60 * 60 * 24)))
seq 0 $days | while read days_offset; do
date -d "$from + $days_offset days" "+%Y-%m-%d"
done
}
download_kml_files() {
local cookies=$1 from=$2 to=$3
date_range "$from" "$to" | while IFS="-" read year month day; do
local month0=$((10#$month - 1))
local pb_ary=(
"!1m8!1m3"
"!1i$year!2i$month0!3i$day"
"!2m3"
"!1i$year!2i$month0!3i$day"
)
local pb=$(join_by "" "${pb_ary[@]}")
local url="https://timeline.google.com/maps/timeline/kml?authuser=0&pb=${pb}"
local kmlfile="$year-$month-$day.kml"
debug "GET $url -> $kmlfile"
curl -f -sS -L -b "$cookies" "$url" -o "$kmlfile"
echo "$kmlfile"
done
}
join_kmls() {
xargs xml_grep --wrap "Document" --cond "Placemark"
}
main() {
if test $# -ne 3; then
echo "Usage: $(basename "$0") COOKIES.TXT FROM_YYYY-MM-DD TO-YYYY-MM-DD"
exit 2
else
download_kml_files "$@" | join_kmls
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment