Monday, January 23, 2012

A little joke for your day ...






I have written this shell script to get some jokes in my daily routine. If you are like me, you probably open several terminal windows during the day.

If that is the case, how about a little joke to keep it fun.

This script does a few things:

  1. Gets jokes from the good folks at http://www.randomjoke.com/ (Please support them if you can.)
  2. It formats the returned joke so it can be printed on the terminal screen
  3. Since some of the jokes are pretty funny, I decided to add functionally to update the status on Adium/Skype with the returned "oneliners" joke

I've tested this with Snow Leopard and Leopard on the Mac, also Ubuntu 11.10 and Ubuntu 11.04 (The Ubuntu part is only for printing on the terminal not Adium/Skype).

In Linux, with the sed command, line 69 and 73 on the script, remove the set of empty "" double quotes.

If you are not going to use the Adium/Skype functionally, feel free to use the other "Joke Categories" which are pretty funny.

Shout me an email if you have any questions yaxzone@yaxmail.com




#!/bin/sh
# luis yax - 20120123 - yaxzone@yaxmail.com
###############################################################################
# I have written this shell script to get some jokes in my daily routine
# If you are like me, you probably open several terminal windows during the day
# If that is the case, how about a little joke to keep it fun.
# This script does a few things:
# 1. Gets jokes from the good foks at http://randomjoke.com/ (Please support them if you can.)
# 2. It formats the returned joke so it can be printed on the terminal screen
# 3. Since some of the jokes are pretty funny, I decided to add functionally so
#    so it can update the status of Adium/Skype with the returned "oneliners" joke
# 
# I've tested this with Snow Leopard and Leopard on the Mac, also Ubuntu 11.10 and Ubuntu 11.04 
# (The Ubuntu part is only for printing on the terminal not Adium/Skype)
# In Linux, with the sed command, line 69 and 73, remove the set of empty "".
# If you are not going to use the Adium/Skype functionally, feel free to use the other
# Joke Categories which are pretty funny.
# Shout me an email if you have any questions yaxzone@yaxmail.com


clear

#### Jokes Category ####
# Uncomment ONE of the following:

#JokeCategory='haha'  # - http://www.randomjoke.com/topic/haha.php
JokeCategory='oneliners'  # - http://www.randomjoke.com/topic/oneliners.php
#JokeCategory='news'  # - http://www.randomjoke.com/topic/news.php
#JokeCategory='signs'  # - http://www.randomjoke.com/topic/signs.php
#JokeCategory='nerd'  # - http://www.randomjoke.com/topic/nerd.php
#JokeCategory='professional'  # - http://www.randomjoke.com/topic/professional.php
#JokeCategory='quotes'  # - http://www.randomjoke.com/topic/quotes.php
#JokeCategory='lightbulb'  # - http://www.randomjoke.com/topic/lightbulb.php
#JokeCategory='couples'  # - http://www.randomjoke.com/topic/couples.php
#JokeCategory='riddles'  # - http://www.randomjoke.com/topic/riddles.php
#JokeCategory='religion'  # - http://www.randomjoke.com/topic/religion.php
#JokeCategory='gross'  # - http://www.randomjoke.com/topic/gross.php
#JokeCategory='blonde'  # - http://www.randomjoke.com/topic/blonde.php
#JokeCategory='politics'  # - http://www.randomjoke.com/topic/politics.php
#JokeCategory='doit'  # - http://www.randomjoke.com/topic/doit.php
#JokeCategory='laws'  # - http://www.randomjoke.com/topic/laws.php
#JokeCategory='defs'  # - http://www.randomjoke.com/topic/defs.php
#JokeCategory='dirty'  # - http://www.randomjoke.com/topic/dirty.php
#JokeCategory='ethnic'  # - http://www.randomjoke.com/topic/ethnic.php
#JokeCategory='zippergate'  # - http://www.randomjoke.com/topic/zippergate.php


### End of Jokes Category ####


recordid='/tmp/'$JokeCategory'id'
category=$JokeCategory
file1="/tmp/jokes_full_page.txt"
file2="/tmp/jokes_only.txt"
file3="/tmp/jokes_.txt"

if [ -f "$recordid"  ];
then
id=`cat $recordid`
fi

lynx -dump "http://www.randomjoke.com/topic/$category.php?$id" > $file1 2>&1

grep "$category.php?" $file1 | grep -v '#' | cut -d'?' -f2 | head -1 > $recordid

sed -n -e '/next joke|/,/Jokes served./p' $file1 > $file2
#sed -i '/next joke/d' $file2; sed -i '/Jokes served./d' $file2
### For printing on terminal only, better formatted
#sed -i "" '/next joke/d;/Jokes served./d' $file2

### For using with Adium/Skype
### Comment this line if you decide not to use it with Adium/Skype
sed -i "" '/next joke/d;/Jokes served./d;/^$/d' $file2

## Uncoment the 3 lines below if you want to print jokes on your terminal only

#echo "JokeId Number: $id" > $file3
#cat $file2 >> $file3
#cat $file3

## End of printing on terminal only

######## For Adium/Skype ################ 
## Code below updates your status on Adium and/or Skype ##
# uncomment this option if you don't want to use these apps.
# This one is recommended to use with the JokeCategory='oneliners'
# As the content will be larger and will not go well with Adium/Skype

adiumStatus=`cat $file2`

osascript -e "tell application \"System Events\"

if exists process \"Adium\" then
   tell Application \"Adium\"
      set the status message of every account whose status type is available to \"$adiumStatus\"
   end tell
end if

if exists process \"Skype\" then
   tell Application \"Skype\"
      send command \"SET PROFILE MOOD_TEXT\" & \"$adiumStatus\" script name \"updater\" 
   end tell
end if

end tell"

###### End of code for updating status on Adium/Skype ##


rm /tmp/jokes_*
exit

No comments: