Fun Script to Track Your Blog Hit Count…


I got the inspiration for this script from CoolTechie’s blog. He made a script to display cricket match scores from a web site. I decided to have a little fun with it, so made one of my own that periodically checks this blog and displays a popup window if the hit counter has changed, with the current visitor count.

I started with the command, “touch blog-hits”, then edited it in vi. When I was done, I typed “chmod +x blog-hits” to make it executable and ran it by typing “./blog-hits”.

Here is the script…


### Adapted from a soccer score script found on
### http://cooltechie.wordpress.com/2007/12/04/cricket-scoreboard-kde-desktop/
### Thanks out to cooltechie!


#!/bin/sh


####################
# Set variables - interrupt, url, url title, search phrase, and unchanged counter
####################
USER_INTERRUPT=13
url="https://symbolik.wordpress.com"
hits="a"
same="b"
##phrase="hits" ### this is the default, change if you use something else
phrase="bipeds"
title="Linux Free Trade Zone" ### I wanted to come up with a cool way to extract the title of the URL, but it got late...


####################
# Catch Control-C events to break out of the loop and remove the dump file
####################
trap 'echo "Quitting..."; rm -f dump; exit $USER_INTERRUPT' TERM INT


while [ 1 ]
do
lynx -dump $url > dump
hits=`grep "$phrase" dump`
if [ "$hits" != "$same" ]
then
kdialog --title "$title" --passivepopup "$hits" 10
same=$hits
fi
sleep 60
done

It grabs the text of the url, looks for the word “hits” or whatever phrase you tell it to if you have changed that on your site, and compares it to the old value (same). When the current value is different from the “same” value, such as when the script is first run and when people visit your blog, it displays that new hit count in the popup for 10 seconds, then waits one minute before re-downloading a text dump of the site. You end it with CTRL-C, which also tells it to clean up the dump file it makes. You can run it in the background if you want. I am sure there is plenty of stuff you can add to this as well, and it might even be a little buggy (inaccurate). I just thought it would be fun to have a little popup counter, and it was fun to do.

One thing I found is that it also prints the phrase you searched on to display the visitor count line, because that phrase is getting appended to the “hits” variable. Too tired to troubleshoot, however…

Anyway, enjoy!

5 Responses

  1. hi,
    thanks for the above script. i found the exiting with CTRL + C events helpful will implement it in my score board script too..
    Cheers!
    vishal
    http://cooltechie.wordpress.com

  2. Glad it helped. Thanks again!

  3. Cool thanks.

  4. No prob – had forgotten all about this.

  5. to me it looks like a rocket sience

    can you break it down further please
    and tel me what to paste and where in my blog to get hit count
    plz

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: