#!/bin/bash
# A shell script that checks a given user's
# link and comment karma. Also checks
# whether or not the user has new messages
# and displays an image when they do
# http://www.redditstatic.com/cake.png
# http://www.redditstatic.com/mail.png
# initialize karma to zero
L_KARMA=0
C_KARMA=0
# set username to null
U_NAME="null"
# set mail to false
MAIL="false"
# sets the day created to 0 (don't know
# why. Just cause.
CREATED=0
getInfo() {
# just so the output isn't shown
tempStore=`curl -s -b $HOME/.redditCookie http://www.reddit.com/api/me.json | grep '"has_mail": true'`
# checks to see if grep exited with an error code. If it didn't
# then the user has no mail.
if [ $? -eq 0 ] ; then
MAIL="true"
else
MAIL="false"
fi
# gets the json file, then gets the username and then takes out
# all of the non usable characters that exist (quotes, etc)
U_NAME=` curl -s -b $HOME/.redditCookie http://www.reddit.com/api/me.json | sed 's/:/\n/g' | grep -A 1 '"name"' | tail -n 1 | sed 's/created//g' | sed 's/[^0-9a-zA-Z]*//g'`
# gets the json file, and gets the values for karma, then sets
# them to the vars.
L_KARMA=`curl -s -b $HOME/.redditCookie http://www.reddit.com/api/me.json | sed 's/:/\n/g' | grep -A 1 '"link_karma"' | sed 's/[^0-9]*//g' | tail -n 1`
C_KARMA=`curl -s -b $HOME/.redditCookie http://www.reddit.com/api/me.json | sed 's/:/\n/g' | grep -A 1 '"comment_karma"' | sed 's/[^0-9]*//g' | tail -n 1`
# gets the time your account was created, and converts it from
# Unix time to 'real' time and sets it up to compare to the
# current time
CREATED=`curl -s -b $HOME/.redditCookie http://www.reddit.com/api/me.json | sed 's/:/\n/g' | grep -A 1 '"created"' | sed 's/,//g' | tail -n 1 | sed 's/[^0-9.]//g'`
CREATED_MONTH=`date -d @$CREATED | sed 's/ /\n/g' | sed 's/ /\n/g' | head -n 2 | tail -n 1`
CREATED_MONTH="$CREATED_MONTH `date -d @$CREATED | sed 's/ /\n/g' | sed 's/ /\n/g' | head -n 3 | tail -n 1`"
CREATED_YEAR=`date -d @$CREATED | sed 's/ /\n/g' | sed 's/ /\n/g' | tail -n 1 | bc`
# gets the current month and year, converting them into the right
# formats to compare against the created values
cakeDay_month=`date | sed 's/ /\n/g' | sed 's/ /\n/g' | head -n 3 | tail -n 2`
cakeDay_year=`date | sed 's/ /\n/g' | sed 's/ /\n/g' | tail -n 1 | bc`
case $1 in
# prints the console version of the output (for terminal)
--con)
# if it has been a year since the creation of your reddit account
# it wishes you a happy cake day
if [ "$CREATED_MONTH" == "$cakeDay_month" ] ; then
if [ "`echo "scale=2; $cakeDay_year.00 - $CREATED_YEAR.00" | bc | sed 's/[^.00]//g'`" == ".00" ] ; then
echo "Happy CakeDay!"
fi
fi
# prints out the user's link and comment karma
echo "Link Karma: $L_KARMA"
echo "Comment Karma: $C_KARMA"
# if you have new mail, a message is printed out saying
# you have mail
if [ "$MAIL" != "false" ] ; then
echo "You have unread messages."
fi;;
# prints the XML version of the output (for the panel)
*)
# if it has been a year since the creation of your reddit account
# a cake image is shown next to your account name in the panel.
if [ "$CREATED_MONTH" == "$cakeDay_month" ] ; then
if [ "`echo "scale=2; $cakeDay_year.00 - $CREATED_YEAR.00" | bc | sed 's/[^.00]//g'`" == ".00" ] ; then
echo "<img>$HOME/.redditCakeDay.png</img>"
fi
fi
# logged in user, tooltip is karma values. Karma formatting in code
# is bad, but makes the newline appear in the tooltip (which is
# what I wanted)
echo "<txt>$U_NAME</txt>"
echo "<tool>Link Karma: $L_KARMA
Comment Karma: $C_KARMA</tool>"
# if mail is not false, also display the new mail image and makes
# the image clickable. Once clicked, the image takes the user to
# the url of unread messages (assumes the user is logged in).
if [ "$MAIL" != "false" ] ; then
if [ "$MAIL" != "" ] ; then
echo "<img>$HOME/.redditNewMail.png</img>"
echo "<click>/usr/bin/exo-open http://www.reddit.com/message/unread</click>"
fi
fi;;
esac
} # getInfo func
# script to get the login cookie for reddit
scriptSetup() {
# checks to see if the cookie exists
if [ ! -f $HOME/.redditCookie ] ; then
# gets the user's username and password
echo "What is your reddit username?"
read user
echo "What is your account password?"
read pass
# no errors yet
ERRORS="false"
# uses the user inputted name and password to
# retrieve a login cookie. Output is piped to
# a temp file
curl -s -d user=$user -d passwd=$pass -c $HOME/.redditCookie http://www.reddit.com/api/login > /tmp/redditScript
# checks the temp file for any error messages
temp=`grep error /tmp/redditScript`
# if there were any, then the temp file is moved
# to the home directory for the user to read.
# If there were none then the temp file is
# removed
if [ $? -eq 0 ] ; then
rm $HOME/.redditCookie
ERRORS="true"
mv /tmp/redditScript $HOME/RedditScriptErrors.log
else
rm /tmp/redditScript
fi
else
echo "Set up has already been run!"
fi
# if the .png images for cakeday and mail are there
# then don't downoad them, otherwise get them.
if [ ! -f $HOME/.redditNewMail.png ] ; then
curl -s http://www.redditstatic.com/mail.png > $HOME/.redditNewMail.png
fi
if [ ! -f $HOME/.redditCakeDay.png ] ; then
curl -s http://www.redditstatic.com/cake.png > $HOME/.redditCakeDay.png
fi
# if there were any errors then make sure the user knows
# otherwise tell them how to add it to an XFCE gen-mod panel
if [ "$ERRORS" == "true" ] ; then
echo ""
echo "Setup finished with errors. Check the error log in your home directory for more details, then re-run this script with --setup option."
else
echo ""
echo "Setup finished! To run as a XFCE Panel General module plug-in, run with --genmod option."
fi
} # scriptSetup
# to test if there is an internet connection
ping -c 1 google.com > /dev/null
if [ $? -eq 0 ] ; then
case $1 in
--genmon)
getInfo;;
--setup)
scriptSetup;;
*)
getInfo --con;;
esac
else
case $1 in
--genmon)
echo "<txt>NETWORK ERROR</txt>";;
--setup)
echo "Network Connection";;
*)
echo "Network Error";;
esac
fi