Guest User

Reddit user information check script

a guest
Nov 8th, 2012
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.63 KB | None | 0 0
  1. #!/bin/bash
  2. # A shell script that checks a given user's
  3. #  link and comment karma. Also checks
  4. #  whether or not the user has new messages
  5. #  and displays an image when they do
  6.  
  7. # http://www.redditstatic.com/cake.png
  8. # http://www.redditstatic.com/mail.png
  9.  
  10. # initialize karma to zero
  11. L_KARMA=0
  12. C_KARMA=0
  13.  
  14. # set username to null
  15. U_NAME="null"
  16.  
  17. # set mail to false
  18. MAIL="false"
  19.  
  20. # sets the day created to 0 (don't know
  21. #  why. Just cause.
  22. CREATED=0
  23.  
  24. getInfo() {
  25.  # just so the output isn't shown
  26.  tempStore=`curl -s -b $HOME/.redditCookie http://www.reddit.com/api/me.json | grep '"has_mail": true'`
  27.  
  28.  # checks to see if grep exited with an error code. If it didn't
  29.  #  then the user has no mail.
  30.  if [ $? -eq 0 ] ; then
  31.      MAIL="true"
  32.  else
  33.      MAIL="false"
  34.  fi
  35.  
  36.  # gets the json file, then gets the username and then takes out
  37.  #  all of the non usable characters that exist (quotes, etc)
  38.  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'`
  39.  
  40.  # gets the json file, and gets the values for karma, then sets
  41.  #  them to the vars.
  42.  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`
  43.  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`
  44.  
  45.  # gets the time your account was created, and converts it from
  46.  #  Unix time to 'real' time and sets it up to compare to the
  47.  #  current time
  48.  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'`
  49.  CREATED_MONTH=`date -d @$CREATED | sed 's/  /\n/g' | sed 's/ /\n/g' | head -n 2 | tail -n 1`
  50.  CREATED_MONTH="$CREATED_MONTH `date -d @$CREATED | sed 's/  /\n/g' | sed 's/ /\n/g' | head -n 3 | tail -n 1`"
  51.  CREATED_YEAR=`date -d @$CREATED | sed 's/  /\n/g' | sed 's/ /\n/g' | tail -n 1 | bc`
  52.  
  53.  # gets the current month and year, converting them into the right
  54.  #  formats to compare against the created values
  55.  cakeDay_month=`date | sed 's/  /\n/g' | sed 's/ /\n/g' | head -n 3 | tail -n 2`
  56.  cakeDay_year=`date | sed 's/  /\n/g' | sed 's/ /\n/g' | tail -n 1 | bc`
  57.  
  58.  
  59.  
  60.  case $1 in
  61.  
  62.  
  63.     # prints the console version of the output (for terminal)
  64.      --con)
  65.       # if it has been a year since the creation of your reddit account
  66.       #  it wishes you a happy cake day
  67.       if [ "$CREATED_MONTH" == "$cakeDay_month" ] ; then
  68.          if [ "`echo "scale=2; $cakeDay_year.00 - $CREATED_YEAR.00" | bc | sed 's/[^.00]//g'`" == ".00" ] ; then
  69.              echo "Happy CakeDay!"
  70.          fi
  71.       fi
  72.      
  73.       # prints out the user's link and comment karma
  74.       echo "Link Karma: $L_KARMA"
  75.       echo "Comment Karma: $C_KARMA"
  76.  
  77.       # if you have new mail, a message is printed out saying
  78.       #  you have mail
  79.       if [ "$MAIL" != "false" ] ; then
  80.           echo "You have unread messages."
  81.       fi;;
  82.  
  83.  
  84.  
  85.  
  86.      # prints the XML version of the output (for the panel)
  87.      *)
  88.           # if it has been a year since the creation of your reddit account
  89.           #  a cake image is shown next to your account name in the panel.
  90.          if [ "$CREATED_MONTH" == "$cakeDay_month" ] ; then
  91.              if [ "`echo "scale=2; $cakeDay_year.00 - $CREATED_YEAR.00" | bc | sed 's/[^.00]//g'`" == ".00" ] ; then
  92.                  echo "<img>$HOME/.redditCakeDay.png</img>"
  93.              fi
  94.          fi
  95.  
  96.          # logged in user, tooltip is karma values. Karma formatting in code
  97.      #  is bad, but makes the newline appear in the tooltip (which is
  98.          #  what I wanted)
  99.          echo "<txt>$U_NAME</txt>"
  100.          echo "<tool>Link Karma: $L_KARMA
  101. Comment Karma: $C_KARMA</tool>"
  102.  
  103.          # if mail is not false, also display the new mail image and makes
  104.          #  the image clickable. Once clicked, the image takes the user to
  105.          #  the url of unread messages (assumes the user is logged in).
  106.          if [ "$MAIL" != "false" ] ; then
  107.  
  108.           if [ "$MAIL" != "" ] ; then
  109.           echo "<img>$HOME/.redditNewMail.png</img>"
  110.           echo "<click>/usr/bin/exo-open http://www.reddit.com/message/unread</click>"
  111.           fi
  112.  
  113.           fi;;
  114.  
  115. esac
  116.  
  117. } # getInfo func
  118.  
  119. # script to get the login cookie for reddit
  120. scriptSetup() {
  121.  
  122. # checks to see if the cookie exists
  123.  if [ ! -f  $HOME/.redditCookie ] ; then
  124.  
  125.      # gets the user's username and password
  126.      echo "What is your reddit username?"
  127.      read user
  128.      echo "What is your account password?"
  129.      read pass
  130.  
  131.      # no errors yet
  132.      ERRORS="false"
  133.  
  134.      # uses the user inputted name and password to
  135.      #  retrieve a login cookie. Output is piped to
  136.      #  a temp file
  137.      curl -s -d user=$user -d passwd=$pass -c $HOME/.redditCookie http://www.reddit.com/api/login > /tmp/redditScript
  138.  
  139.      # checks the temp file for any error messages
  140.      temp=`grep error /tmp/redditScript`
  141.  
  142.      # if there were any, then the temp file is moved
  143.      #  to the home directory for the user to read.
  144.      #  If there were none then the temp file is
  145.      #  removed
  146.      if [ $? -eq 0 ] ; then
  147.          rm $HOME/.redditCookie
  148.          ERRORS="true"
  149.      mv /tmp/redditScript $HOME/RedditScriptErrors.log
  150.      else
  151.      rm /tmp/redditScript
  152.      fi
  153.      
  154.  else
  155.      echo "Set up has already been run!"
  156.  fi
  157.  
  158.  # if the .png images for cakeday and mail are there
  159.  #  then don't downoad them, otherwise get them.
  160.  if [ ! -f $HOME/.redditNewMail.png ] ; then
  161.      curl -s http://www.redditstatic.com/mail.png > $HOME/.redditNewMail.png
  162.  fi
  163.  
  164.  if [ ! -f $HOME/.redditCakeDay.png ] ; then
  165.      curl -s http://www.redditstatic.com/cake.png > $HOME/.redditCakeDay.png
  166.  fi
  167.  
  168.  # if there were any errors then make sure the user knows
  169.  #  otherwise tell them how to add it to an XFCE gen-mod panel
  170.  if [ "$ERRORS" == "true" ] ; then
  171.      echo ""
  172.      echo "Setup finished with errors. Check the error log in your home directory for more details, then re-run this script with --setup option."
  173.  else
  174.      echo ""
  175.      echo "Setup finished! To run as a XFCE Panel General module plug-in, run with --genmod option."
  176.  fi
  177.  
  178. } # scriptSetup
  179.  
  180. # to test if there is an internet connection
  181. ping -c 1 google.com > /dev/null
  182.  
  183.  
  184. if [ $? -eq 0 ] ; then
  185.  
  186.     case $1 in
  187.     --genmon)
  188.         getInfo;;
  189.     --setup)
  190.         scriptSetup;;
  191.     *)
  192.         getInfo --con;;
  193.     esac
  194. else
  195.     case $1 in
  196.     --genmon)
  197.         echo "<txt>NETWORK ERROR</txt>";;
  198.     --setup)
  199.         echo "Network Connection";;
  200.     *)
  201.         echo "Network Error";;
  202.     esac
  203. fi
Add Comment
Please, Sign In to add comment