stargazer's profile picture

Published by

published
updated

Category: Web, HTML, Tech

Check user status with a single command

Tired of browsing the web to see when your SpaceHey friends were last online?
Now you can do it with a single command.


What you'll need

  • a Linux OS
  • the shell script I'll provide
  • 2-3 minutes of freedom

Installation

1. Copy the code below and save it into a file titled status.sh

#!/bin/bash

#handle flags
if [ "$1" = "-i" ]
then
URL="https://spacehey.com/profile?id="$2
elif [ "$1" = "-u" ]
then
URL="https://spacehey.com/"$2
elif [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
echo -e "\nUsage: $ status <flags>\n\nDisplays the status of SpaceHey users.\n\n Flags:\n\n -i <id> Search by user id\n\n -u <name> Search by username\n\n -h --help Display this message"
else
echo -e "E: invalid flags or arguments"
fi

#perform curl request and greps
if [ "$1" = "-i" ] || [ "$1" = "-u" ]
then
DATA=`curl -s $URL`
ONLINE=`echo $DATA | grep -m 1 '<p class="online">'`
EPOCHE=`echo $DATA | grep -oP '(?<=Last active: <br> <time class="ago">)\w+(?=</time>)'`

#set display name
NAME=`echo $DATA | grep -oP -m 1 '(?<=<h1>)\w+(?=</h1>)'`
if [ "$NAME" = "" ]
then
NAME=`echo $DATA | grep -oP -m 1 '(?<=<h1>)\w+(?= <)'`
if [ "$NAME" = "" ]
then
NAME="User"
fi
fi

#handle edge cases
if [ "$EPOCHE" != "" ]
then
echo -e "\n$NAME is offline"
DATE=`date -d @$EPOCHE`
echo "Last seen: "$DATE

else
if [ "$ONLINE" != "" ]
then
echo -e "\n$NAME is online"
else
echo -e "\nUser not found"
fi
fi
fi



2. Edit your shell configuration file

If you're using Bash you can do so using the following command

$ sudo nano ~/.bashrc

Now add an alias to the config file

alias status='./file-path'

Note: replace file-path with the file path to status.sh you created earlier.



3. Lastly set the created file to an executable

$ chmod 755 status.sh

If all went well you should be able to get the following output when running status -h

Usage: $ status <flags>

Displays the status of SpaceHey users.

Flags:
-i <id>Search by user id
-u <name>Search by username
-h --helpDisplay this message



I hope you found this useful. If you have any questions or need help trouble shooting, just comment on this post.


15 Kudos

Comments

Displaying 1 of 1 comments ( View all | Add Comment )

scientist_axolotl

scientist_axolotl's profile picture
Pinned

thanks ill def use that (never in a billion years would i be able understand/use that)


Report Comment



You still gave me free kudos tho, get rekt

by stargazer; ; Report

its like doin charity work

by scientist_axolotl; ; Report

I don't have a comeback to that so I'll just say yes

by stargazer; ; Report