ptanov

get-location.sh

Jun 5th, 2020
123
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. set -e
  4.  
  5. # while :; do ./get-location.sh 'https://www.google.com/maps/rpc/locationsharing/read?authuser=0&hl=en&gl=bg&pb=....' 'out.gpx' ; ./update-map.sh 'out.gpx' 'map.html'; echo "[[[--- $(echo "$(cat "out2.gpx" | wc -l) - 7" | bc) ---]]] on $(date)" ; sleep 60; done
  6.  
  7.  
  8. latLonTimestamp="$(curl "${1}" -s | tail +2 | jq '(.[0][0][1][1][1]),(.[0][0][1][1][2]),(.[0][0][1][2])' )"
  9.  
  10. lon="$(head -n 1 <<< "${latLonTimestamp}" | tail -n 1)"
  11. lat="$(head -n 2 <<< "${latLonTimestamp}" | tail -n 1)"
  12. timestamp="$(head -n 3 <<< "${latLonTimestamp}" | tail -n 1)"
  13.  
  14. unixTimestamp="$(( "${timestamp}" / 1000 ))"
  15.  
  16. time="$(date --utc +%FT%T.%3NZ -d @${unixTimestamp})"
  17.  
  18. gpxLine="            <trkpt lon=\"${lon}\" lat=\"${lat}\"><time>${time}</time></trkpt>"
  19.  
  20. filename="${2:-out.gpx}"
  21. if ! [ -f "${filename}" ]; then
  22.     echo '<?xml version="1.0" encoding="UTF-8"?>' > "${filename}"
  23.     echo '<gpx xmlns="http://www.topografix.com/GPX/1/1">' >> "${filename}"
  24.     echo '    <trk>' >> "${filename}"
  25.     echo '        <trkseg>' >> "${filename}"
  26.     echo '        </trkseg>' >> "${filename}"
  27.     echo '    </trk>' >> "${filename}"
  28.     echo '</gpx>' >> "${filename}"
  29. fi
  30.  
  31. matches="$(grep "${gpxLine}" "${filename}" || [[ ${?} == 1 ]])"
  32.  
  33. if [ -z "${matches}" ]; then
  34.     # remove </trkseg></trk></gpx>:
  35.     content="$(head -n -3 "${filename}")"
  36.     echo "${content}" > "${filename}"
  37.     echo "${gpxLine}" >> "${filename}"
  38.     echo '        </trkseg>' >> "${filename}"
  39.     echo '    </trk>' >> "${filename}"
  40.     echo '</gpx>' >> "${filename}"
  41. fi
RAW Paste Data