Skip to content

Instantly share code, notes, and snippets.

@tokland
Last active March 7, 2024 12:38
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tokland/1bfbbdf495576cf6253d8153d7168de4 to your computer and use it in GitHub Desktop.
Save tokland/1bfbbdf495576cf6253d8153d7168de4 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 logged-in in timeline.google.com with some
# add-on/extension (i.e. 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.
#
# There exist free online sites tht render KML files into a map. For example: https://kmlviewer.nsspot.net
debug() {
echo "$@" >&2
}
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)) # 0-index month
local day0=$((10#$day)) # The day arguments must not contain leading zeros
local pb_ary=(
"!1m8"
"!1m3"
"!1i$year!2i$month0!3i$day0"
"!2m3"
"!1i$year!2i$month0!3i$day0"
)
local pb=$(echo "${pb_ary[@]}" | tr -d " ")
local url="https://timeline.google.com/maps/timeline/kml?authuser=0&pb=${pb}"
local kmlfile="$year-$month-$day.kml"
debug "GET $url -> $kmlfile"
download_kml "$cookies" "$url" >"$kmlfile"
echo "$kmlfile"
done
}
download_kml() {
local cookies=$1 url=$2
local output
output=$(curl -f -sS -L -b "$cookies" "$url")
if grep -q "accounts.google.com/v3/signin" <<<"$output"; then
debug "Authentication error. Check the cookies file: $cookies"
return 1
else
echo "$output"
fi
}
merge_kmls() {
xargs -r xml_grep --wrap "Document" --cond "Placemark"
}
main() {
if test $# -ne 4; then
echo "Usage: $(basename "$0") COOKIES.txt FROM(YYYY-MM-DD) TO(YYYY-MM-DD) OUT.kml"
exit 2
else
local outfile=$4
download_kml_files "$@" | merge_kmls >"$outfile"
fi
}
main "$@"
@disconnect5852
Copy link

disconnect5852 commented Sep 6, 2022

lol! Cant download anything after july, as the month above 07 causes "line 37: 08: value too great for base (error token is "08")". It is interpreted as octal number.
adding line before the error line solves it: local month0=$(($((10#$month)) - 1))

@tokland
Copy link
Author

tokland commented Sep 10, 2022

@disconnect5852 Thanks for the report! Fixed. Also, added a 4th argument for the output KML file, and a check on the session.

@Fireblade954
Copy link

Should this still work? cant get any usefull responses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment