Skip to content

Instantly share code, notes, and snippets.

@moonblade
Last active December 19, 2016 14:50
Show Gist options
  • Save moonblade/962be9f54c9e7fbaffdf8cd1845322b7 to your computer and use it in GitHub Desktop.
Save moonblade/962be9f54c9e7fbaffdf8cd1845322b7 to your computer and use it in GitHub Desktop.
# In google chrome, install cookies.txt addon, download browser cookies with it to download folder
activity="Motorcycling"
# create csv file if not exist
if [ ! -f ~/mileage.csv ]; then
/usr/bin/echo "date,distance,=sum(b:b)/1000" > ~/mileage.csv
startDate=`date +%Y-%m-%d -d "1 day ago"`
else
startDate=$(/usr/bin/cat ~/mileage.csv | /usr/bin/tail -n1 | /usr/bin/cut -f1 -d,)
startDate=$(date -I -d "$startDate +1 day")
fi
endDate=`date +%Y-%m-%d`
d=$startDate
while [ "$d" != $endDate ]; do
# Get year month-1 and day for kml history date
year=`echo $d | cut -f1 -d-`
month=`echo $d | cut -f2 -d-`
let month=month-1
day=`echo $d | cut -f3 -d-`
# Make kml link out of the given date
dateLink="https://www.google.com/maps/timeline/kml?authuser=0&pb=!1m8!1m3!1i${year}!2i${month}!3i${day}!2m3!1i${year}!2i${month}!3i${day}"
randomNumber=$RANDOM
/usr/bin/curl -s -o today$randomNumber --cookie ~/Downloads/cookies.txt $dateLink
# find all distances travelled
distances=$(/usr/bin/cat today$randomNumber | /usr/bin/grep -hoP "<description> $activity .*?</description>" | /usr/bin/grep -ho "[0-9][0-9]*m" | /usr/bin/cut -dm -f1)
# find the date
todayDate=$(/usr/bin/cat today$randomNumber | /usr/bin/grep -oh "Location history from [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]" | /usr/bin/grep -oh [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])
# remove the temp file
rm today$randomNumber
# Add up all the distances
totalDistance=0
for f in $distances; do let totalDistance=totalDistance+$f; done
lastDate=$(/usr/bin/cat ~/mileage.csv | /usr/bin/tail -n1 | /usr/bin/cut -f1 -d,)
if [ "$lastDate" != "$todayDate" ]; then
/usr/bin/echo $todayDate,$totalDistance >> ~/mileage.csv
fi
# Update to next date
d=$(date -I -d "$d + 1 day")
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment