Friday, April 30, 2010

Desktop Scorecard for Cricket

Most of the cricket lovers will be using their favorite site cricinfo.com for the match updates. As a huge fan of cricket I do the same. But sometimes when you are doing some important work, you may not be interested in switching the current working window to watch the score. As an example When I am watching a movie, I am not interested in pausing the movie and see the score card. This led me to think of how can I know the score without switching my window. Desktop scorecard is the solution that I came up with.

I have kubuntu running on machine. Kubuntu shows a pop-up notification message at the bottom right corner when I plug in a pendrive. I thought of using the similar kind of notification message for the cricket score card. When I searched on net, I found a cool solution with kdialog tool. It has various GUI elements like input textbox, alert windows etc. For the score card notification message it has an option called passivepopup. It takes 2 parameters. One is the text to be shown in the notification message and the other one is the time duration for which the notification message should be active. If the user does not close that message with in that time, It automatically gets closed. For example the following command produces the below passivepopup

kdialog --title "This is a passive popup" --passivepopup "It will disappear in about 60 seconds" 60


Having known how to show pop-up notification messages, my task of showing score card has become more easy. All I need to do now is that, I have to get the current score and then send that to the popup message. For this task, I have used the cricinfo.com as the source for my score card. My score card shows very little information, the current team's score and both the batsman's score. I think that is enough but if you want more you can add it.

For reading the webpage and getting the score, I have used the perl language. Finally a mix of both perl and shell scripting completed my task.

#!/usr/bin/perl
use LWP::Simple;
$numargs=$#ARGV + 1;
if($numargs < 1)
{
  print "Enter the URL as argument";
  exit;
}
$pageURL=$ARGV[0];
$filename="cricket.txt";
$scorecardfname="score.txt";
#$pageURL="http://www.cricinfo.com/world-twenty20-2010/engine/current/match/412678.html?view=live";#"http://www.google.com";
$simplePage=get($pageURL); 
if( $simplePage == /^\s*$/)
{
   print "page can not be fetched\n";
   open FHSC1,">$scorecardfname" or die $!;
   print FHSC1 "";
   close FHSC1;
   exit;
}
open FH,"> $filename" or die $!;
print FH $simplePage;
close FH;
open FH,$filename or die $!;
open FHSC,">$scorecardfname" or die $!;
@fc=<fh>;
$NOL=@fc;
$line=0;
$noofplayers=0;
$teamcount=0;
while($line < $NOL)
{
  $e = $fc[$line];
  if( $e =~ /<div class="teamText">
(.*)<\/p>/ )
  {
    if($teamcount == 0 )
    {
      $firstbattingteam = $1;
      print FHSC $firstbattingteam."\n";
      $teamcount++;
    } 
    elsif($teamcount==1)
    {
      $secondbattingteam = $1;
      $teamcount++;
    }
  }
  elsif( $e =~ /showHwkTooltip/ )
  {
    if( $e =~ /<a.*>(.*)<\/a>/)
    {
      #print $1."\n";
      $name = $1;
      $line = $line + 2;
      if( $fc[$line] =~ /<b>(.*)<\/b><\/td>/ )
      {
          $score = $1;
      }
      $line++;
      if( $fc[$line] =~ /(.*)<\/td>/ )
      {
          $balls = $1;
      }
      $line++;
      if( $fc[$line] =~ /(.*)<\/td>/ )
      {
          $fours = $1;
      }
      $line++;
      if( $fc[$line] =~ /(.*)<\/td>/ )
      {
          $sixes = $1;
      }
      $line++;
      if( $fc[$line] =~ /(.*)<\/td>/ )
      {
          $strikerate = $1;
      }
      print FHSC $name."      ".$score."(".$balls.")\n"; 
      $noofplayers++;
      if($noofplayers==2) 
      {
          last;
      }
    }
  }
  $line++;
}
print FHSC $secondbattingteam."\n";
#print FHSC $firstbattingteam."\n";
close FH;
close FHSC;

save the above code as readwebpage.pl. This takes the URL of the cricinfo webpage as the command line argument. This is perl script is executed in the below mentioned shell script. It reads the webpage and searches for the teams information and current batting players. It writes this information in to score.txt file. This file is read by the below shell script.

i=1;
url=`kdialog --title "Cricinfo Url" --inputbox "Enter the Cricinfo URL"`;
echo "Entered URL: $url";
while [ $i -ne 0 ]
do
  ./readwebpage.pl $url;
  sha=`cat score.txt`;
  if [[ $sha == "" ]]
  then
    exit;
  fi
  kdialog --title "scorecard" --passivepopup "$sha" 10;
  sleep 11;
done

Save the above snippet as showscore.sh. When you run this shell script, it opens a window containing a input text box asking for the URL. Go to cricinfo.com in your browser and go the score card page for which you want updates, copy and then paste it in that text box.



One important thing to note here is that always add "?view=live" at the end of the URL. For example if the page URL is http://www.cricinfo.com/world-twenty20-2010/engine/current/match/412678.html then the URL you should enter is http://www.cricinfo.com/world-twenty20-2010/engine/current/match/412678.html?view=live. After taking this URL as input it calls the above perl script with the URL as the command line argument. The perl script writes data into score.txt file. Now the shell script reads the data from score.txt file and calls kdialog's passivepop message. I have kept the 10sec time for the popup to stay alive. You can configure this as you wish. This is all fine but we want to update the score card on regular time intervals. For this purpose make the above functionality in an infinite loop but there should be some sleep time between each update. That regular interval time can be put as the parameter for the sleep command in the script. Suppose if you want to update the score for every 30 sec. then make that as sleep 30 instead of sleep 11.

This works only in Linux machines and you should have Kdialog and perl installed. Ubuntu users can get kdialog using sudo apt-get install kdebase-bin or get it through synaptic manager. Most of the Linux distributions already have perl installed. If you quickly want to try this, download the file DesktopScoreCard. Untar the downloaded file. Open a terminal and go to the untarred DesktopScoreCard folder. Then run the showscore.sh file. If all goes well you can see the popup message with the score card from the URL you have entered.
You can see the screen shot showing the score card

Wednesday, April 28, 2010

Suppress specific characters in a GWT Textbox

Some times we need to suppress specific characters in our GWT text box. Suppose you want only numbers to be allowed in your text box then you need to suppress all the alphabets in that text box. This you can do by attaching a key board listener to the text box and the method onKeyPress should do the required processing for suppressing. Below is the code for a text box which allows only numbers.

TextBox t = new TextBox();
t.addKeyPressHandler(new KeyPressHandler(){
public void onKeyPress(KeyPressEvent event) {
char keyCode = event.getCharCode();
if(!(keyCode > '0' && keyCode <'9') ) {
t.cancelKey();
}
}
});


Similarly, you can limit the number of characters a text box can take


TextBox t = new TextBox();
t.addKeyPressHandler(new KeyPressHandler(){
public void onKeyPress(KeyPressEvent event) {
if(t.getText().length() >= 5 ) {
t.cancelKey();
}
}
});


PS: For the syntax highlight of your code in a post, follow this link http://pleasemakeanote.blogspot.com/2008/06/posting-source-code-in-blogger.html

Friday, February 19, 2010

iptux - an intranet communication tool for linux


People Using a windows machine and connected to a LAN might be familiar with an intranet communication manager called IP Messenger. This is useful for messaging and transferring files from one machine to other machine over LAN. Once installed in your machine, it will automatically detect and show you a list of all the other machines which have this messenger running. You can choose any one on the list and start messaging or file transfer. You can google for IP Messenger and download it for Windows machines.





For Linux users also we have similar application called Gnome IP messenger. I have a previous post here which tells you how to install it. But it has some problem with transfering folders. This is a serious concern. To overcome this problem we have one more similar application called iptux . This is really very cool application. You have lot of features with this application and it has very rich UI. Ubuntu Users can directly get it through Synaptic manger. Just searc for iptux in your Synaptic manager. Alternatively you can download it from Ubuntu Repository and install it. There is different pakage file for different version of ubuntu. Please keep this in mind while downloading. For other flavours of linux users, you can download the tar file from google code page and follow these standard instructions for installing
  1. Untar the file and in terminal go to that folder
  2. run ./configure and if it fails, it means that something is missing in your OS for compiling the source. You can google with the error it gives and find the solution. Once you are successfull with this step move to next step.
  3. run make
  4. run make install -- after this you are ready to use your newly installed iptux
  5. run make clean -- this step is optional.
  6. To start iptux, open and termial and type iptux and enjoy intranet messaging and file transfers.

Friday, February 12, 2010

Sunday, January 10, 2010

ఎవడి గోల వాడిది........ తెలంగాణా పై...........

గత నెల రోజులకు పైగా ఆంధ్రప్రదేశ్ రాష్ట్ర రాజకీయాలు ఎటు వైపు వెళ్తున్నాయో అర్థం కావడం లేదు. ఏ వార్తా పత్రిక చుసిన, ఏ టీవీ ఛానల్ పెట్టిన కనిపించే అంశం ఒక్కటే. అది ప్రత్యేక తెలంగాణా రాష్ట్ర ఏర్పాటు. ఈ తెలంగాణా అంశం చర్చకు రాకముందు మీడియా లో నలుగుతూన్న అంశం ఓబులాపురం గనుల అక్రమాలు. ఇప్పుడు దాని గురించి పట్టించుకునే పత్రిక గానీ, న్యూస్ ఛానల్ గాని లేదు. బహుశ ఈ కోట్లు గడించే అక్రమాలను మీడియా నుండి తప్పించడానికే చాలా చిన్నదిగా సాగిన తెలంగాణా అంశాన్ని బూతద్దంలో పెట్టి పెద్దదిగా కనిపించేలా చేసారేమో. తెలంగాణా అంశం చర్చకు వచ్చిన తరువాత వచ్చిన N. D. తివారి రాసలీలలు, రాజశేకర్ రెడ్డి మరణ సంగటనలో అంబానీల హస్తం లాంటివి ఏవి తెలంగాణా అంశాన్ని పక్క తోవ పట్టించలేకపోయాయి. ఇదిలా పక్కన పెడితే........
అసలు ఈ రాజకీయనాయకులకు తెలంగాణాకు గానీ, సమైక్యాంధ్రకు గానీ మద్దతు పలికే అధికారం ఎవరిచ్చారు?? అవును ప్రజలే వారిని ఎన్నుకొని వారికి ఆ అధికారాన్ని ఇచ్చారు. కాని ఆ అధికారాన్ని ఉపయోగించి, రాష్ట్ర విభజన లాంటి అతి ప్రాముక్యమైన అంశం పై వారు స్వతంత్రంగా నిర్ణయం తీసుకోవచ్చా ?? ఒక్కసారి ఆలోచించండి?? ప్రతి రాజకీయ నాయకుడు తన స్వలాభం కోసమో గానీ, తమ పార్టీ ప్రయోజనలకో గానీ మద్దతించడం లేదా వ్యతిరేకించడం జరుగుతుంది. నిజంగా ఎంత మంది రాజకీయ నాయకులు తమ నియోజకవర్గ ప్రజల అభిప్రాయాలను సేకరించి నిర్ణయం తీసుకున్నారు?? ప్రజాస్వామ్యం అంటే ప్రజలు తమను తామే పరిపలించుకునే ఒక వ్యవస్థ అని ఎంత మంది రాజకీయ నాయకులకు గుర్తుందంటారు..........