Advertisement
howtophil

getspot.sh Spot/SpotX/Etc console tracker

Jan 27th, 2023 (edited)
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.63 KB | None | 0 0
  1. #!/bin/bash
  2. #---------------------------------------------------#
  3. # getspot.sh                                        #
  4. # Phillip J Rhoades                                 #
  5. # 2023-01-26                                        #
  6. #---------------------------------------------------#
  7. # Requires curl and jq                              #
  8. #---------------------------------------------------#
  9. # This script will check a public SPot XML/JSON     #
  10. # feed every XXX number of seconds. If there is     #
  11. # a new message (Tracking/Custom/OK), it will       #
  12. # will print out the message, coordinates, and      #
  13. # a link to a google map of the message location.   #
  14. # Monitor a Spot device from the console!           #
  15. #---------------------------------------------------#
  16.  
  17.  
  18. #----------------------------------------------
  19. # Make ctrl-c exit nicer
  20. #----------------------------------------------
  21. trap "echo Exited!; exit;" SIGINT SIGTERM
  22.  
  23. #----------------------------------------------
  24. # Sleep between refreshes at least 150 seconds
  25. # or Spot will ban your IP and shut down your
  26. # xml/json feed. You can make it refresh less
  27. # often just fine though.
  28. #----------------------------------------------
  29.  
  30. refreshTime=150
  31.  
  32. #----------------------------------------------
  33. # You'll need to create an XML/JSON feed
  34. # on the spot website using your account
  35. # https://www.findmespot.com/en-us/support/spot-x/get-help/general/spot-api-support
  36. #----------------------------------------------
  37. #
  38. # Then put your feedID here
  39. #----------------------------------------------
  40.  
  41. feedID="YOUR-SPOT-feedID-GOES-HERE"
  42.  
  43. #----------------------------------------------#
  44. # The work begins here.                        #
  45. #----------------------------------------------#
  46.  
  47. while :
  48. do
  49.     #----------------------------------------------
  50.     # Save the previous message ID
  51.     # and don't refresh display if there
  52.     # is no new message.
  53.     prevID=$id
  54.  
  55.     #----------------------------------------------
  56.     # For testing use a downloaded file
  57.     # so you don't flood the feed with requests
  58.     #data="`cat spottest.json |jq -r .response.feedMessageResponse.messages.message`"
  59.     #----------------------------------------------
  60.  
  61.     #----------------------------------------------
  62.     #For real use this link
  63.     data="`curl -s https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/$feedID/latest.json |jq -r .response.feedMessageResponse.messages.message`"
  64.     #----------------------------------------------
  65.  
  66.     if  [ -z "$data" ]; then
  67.         echo "Could Not Connect To Spot Server"
  68.     else
  69.         latitude=`echo $data|jq -r .latitude`
  70.         longitude=`echo $data|jq -r .longitude`
  71.         dateTime=$(date -d `echo $data|jq -r .dateTime`)
  72.         messengerName=`echo $data|jq -r .messengerName`
  73.         messageType=`echo $data|jq -r .messageType`
  74.         messageContent=`echo $data|jq -r .messageContent`
  75.         batteryState=`echo $data|jq -r .batteryState`
  76.         unixTime=`echo $data|jq -r .unixTime`
  77.         messengerId=`echo $data|jq -r .messengerId`
  78.         id=`echo $data|jq -r .id`
  79.     fi
  80.  
  81.     if  [[ "$prevID" == "$id" ]]; then
  82.         # Message has not updated so wait
  83.         # and try again on next cycle
  84.         true
  85.     else
  86.         echo "-------------------------------"
  87.         echo "Message Type: $messageType"
  88.         echo "Date/Time: $dateTime"
  89.         echo "From: $messengerName"
  90.         echo "Battery: $batteryState"
  91.         if  [[ "$messageType" != "TRACK" ]]; then
  92.             echo "Message:"
  93.             echo "****************"
  94.             echo "$messageContent"
  95.             echo "****************"
  96.         fi
  97.         echo "Coordinates: $latitude,$longitude"
  98.         # Most xterminals let you right click and open links
  99.         # so this should work that way for opening a google map
  100.         # of the checked-in location.
  101.         echo "Map: http://maps.google.com/maps?q=$latitude,$longitude+(SPOT)&z=14&ll=$latitude,$longitude"
  102.     fi
  103.  
  104.     #----------------------------------------------
  105.     # Waiting the number of seconds
  106.     # before trying the feed again.
  107.     #----------------------------------------------
  108.     # Reset thesleep to refreshTime every cycle
  109.     # for new countdown.
  110.  
  111.     thesleep=$refreshTime
  112.  
  113.     #----------------------------------------------
  114.     # There's a progress spinner here
  115.     # for friendly UI purposes.
  116.     # The four .25 seconds equal one second
  117.     # per "turn" of the spinner.
  118.     #----------------------------------------------
  119.     while [ $thesleep -gt 0 ]
  120.     do
  121.         echo -n "| Updating in $thesleep seconds"
  122.         sleep .25
  123.         echo -n -e "\033[2K\r"
  124.         echo -n "/ Updating in $thesleep seconds"
  125.         sleep .25
  126.         echo -n -e "\033[2K\r"
  127.         echo -n "— Updating in $thesleep seconds"
  128.         sleep .25
  129.         echo -n -e "\033[2K\r"
  130.         echo -n "\\ Updating in $thesleep seconds"
  131.         sleep .25
  132.         echo -n -e "\033[2K\r"
  133.         thesleep=$(($thesleep - 1))
  134.     done
  135. done
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement