Guest User

Untitled

a guest
May 1st, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.71 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # stalk your vrfrens
  3. # more features soon™
  4. set -euo pipefail
  5.  
  6. USER=$1
  7.  
  8. KEYFILE=/tmp/vrfrens-apikey.txt
  9. # cache key so we don't have to grab it every time
  10. if [ -f $KEYFILE ]; then
  11.   KEY=$(<$KEYFILE)
  12. else
  13.   KEY=$(curl -s https://api.vrchat.cloud/api/1/config | jq .apiKey -r)
  14.   echo $KEY > $KEYFILE
  15. fi
  16.  
  17. TOKFILE=/tmp/vrfrens-tok-$USER.txt
  18. # also cache TOK for some time to avoid logging in if necessary. Dunno how long
  19. # the tokens actually last.
  20. if [ -f $TOKFILE ] && test $(find $TOKFILE -not -mmin +30); then
  21.   TOK=$(<$TOKFILE)
  22. else
  23.   TOK=$(curl -su $USER "https://api.vrchat.cloud/api/1/auth/user?apiKey=$KEY" | jq .authToken -r)
  24.   echo $TOK > $TOKFILE
  25. fi
  26.  
  27. FRENS=$(curl -H "Cookie: auth=$TOK" -s "https://api.vrchat.cloud/api/1/auth/user/friends?apiKey=$KEY")
  28. echo You have $(echo $FRENS | jq -r 'length') friends online
  29.  
  30. # grab all unique non-private worlds
  31. # || true to prevent grep's nonzero status from exiting the script with set -e
  32. WRLDS=$(echo $FRENS | jq -r 'map(.location) | unique | join ("\n")' | grep -v private || true)
  33. declare -A WLDINFO
  34. for w in $WRLDS; do
  35.   if [[ "$w" =~ ([^:]+):.+ ]]; then
  36.     wid="${BASH_REMATCH[1]}"
  37.     WLDINFO[$w]=$(curl -H "Cookie: auth=$TOK" -s "https://api.vrchat.cloud/api/1/worlds/$wid?apiKey=$KEY")
  38.   fi
  39. done
  40.  
  41. echo $FRENS | jq -r 'map("\(.displayName) \(.location)") | join("\n")' | while read fren loc; do
  42.   if [[ -z "$fren" ]]; then continue; fi
  43.   fren="REDACTED"
  44.   if [[ "$loc" = "private" ]] ; then
  45.     echo $fren is in a private world.
  46.   elif [[ "$loc" = "offline" ]] ; then
  47.     echo $fren is actually offline, strange. # got this response once, weird
  48.   else
  49.     info=${WLDINFO[$loc]}
  50.     if [[ "$w" =~ [^:]+:(.+) ]]; then
  51.       instid="${BASH_REMATCH[1]}"
  52.       population=$(echo $info | jq ".instances[] | select(.[0] == \"$instid\") | .[1]")
  53.       if [[ -z "$population" ]]; then
  54.         # XXX sometimes the the world info doesn't include the instance in the list
  55.         # but usually it does (even if hidden), so we can get the poopulation.
  56.         # echo $fren claims to be in $loc
  57.         # echo something happened: $instid
  58.         # echo $info
  59.         # echo $info | jq '.instances[]'
  60.         # well we can assume it's something less than the total occupants of the world
  61.         population="unknown, <$(echo $info | jq -r .occupants)"
  62.       fi
  63.     fi
  64.     echo $fren is in $(echo $info | jq -r '"\"\(.name)\" [\(.description)]"') \($population people including them\)
  65.   fi
  66. done
  67.  
  68. OFFRENS=$(curl -H "Cookie: auth=$TOK" -s "https://api.vrchat.cloud/api/1/auth/user/friends?apiKey=$KEY&offline=true")
  69. echo You have $(echo $OFFRENS | jq -r 'length') friends offline:
  70. #echo $OFFRENS | jq -r 'map(.displayName) | join("\n")'
Advertisement
Add Comment
Please, Sign In to add comment