Cricket PowerGadget

January 3, 2007

I was thinking about PowerShell and how you can get it to do fantastic things. And I wondered how easily it could be used for scraping cricket scores.

So I threw together four lines of code to grab the cricket scoreboard from cricinfo and rip out the title.

$ret = (new-object Net.WebClient).DownloadString(“http://content-aus.cricinfo.com/ausveng/engine/current/match/249226.html?view=live;wrappertype=mainframe”)
$titlestart = [Regex]::Matches($ret,”<title>”,”IgnoreCase”)[0].Index
$titleend = [Regex]::Matches($ret,”</title>”,”IgnoreCase”)[0].Index
$ret.Substring($titlestart+7,$titleend-$titlestart-7)

Edited: This can be done easily in one line – Lars pointed out the use of Regex to grab the section between the title tags, which then means we don’t need to store $ret at all. It can now be:

[Regex]::Match((new-object Net.WebClient).DownloadString(“http://content-aus.cricinfo.com/ausveng/engine/current/match/249226.html?view=live;wrappertype=mainframe”),”<title>(.*)</title>”,”IgnoreCase”).Groups[1].Value

It’s not particularly elegant, but it works nicely. I would’ve liked to have handled the HTML as XML instead, and just gone straight to the Title tag, but there’s stuff in there that won’t convert to XML, so I guess that option wasn’t available.

And the really nice thing about this is that I can put these four lines into PowerGadgets, and in all of 10 seconds have a floating gadget which I can use in XP as well as Vista, and (in Vista) put in the sidebar if I want. I’ve told it to refresh every minute, which won’t refresh as quick as some, but hopefully won’t stop working too quickly. It’s not quite as nifty as Darren Neimke’s gadget, but then again, this was really really quick to throw together.

And of course, I’ve left the advert for Cricinfo in there. I wouldn’t want to hide the source of the information. And if they ask me not to do this, then of course I’ll stop. Cricinfo have a great site, and I really don’t want to upset them.

cricinfo

This Post Has 6 Comments

  1. Abhishek225

    Very cool! Congatulations on 5-0 sweep in the ashes. I will use your script as a basis to get India-SA scores!

    -Abhishek

  2. robfarley

    You’ll have to change the match number, but that’s not hard to work out. 🙂 Just replace the 249226 with whatever applies to the match you’re interested in.

  3. Lars Haupt-Hansen

    Using RegEx to then use substring is like crossing the river for water (not sure if thats a saying anywhere else than Denmark):

    $ret = (new-object Net.WebClient).DownloadString(“http://content-aus.cricinfo.com/ausveng/engine/current/match/249226.html?view=live;wrappertype=mainframe”)
    [Regex]::Match($ret,”(.*)“,”IgnoreCase”).Groups[1].Value

  4. robfarley

    Ah, so I guess then you don’t even need a second line…

    [Regex]::Match((new-object Net.WebClient).DownloadString(“http://content-aus.cricinfo.com/ausveng/engine/current/match/249226.html?view=live;wrappertype=mainframe”),”(.*)“,”IgnoreCase”).Groups[1].Value

  5. Jahangir Moin

    i think u done a great job for junior programmer ,but i can’t understand these lines of code for grabing score board .please provide me these lines of code in vb.net b/c i also want to display live cricket score board in my website so i need your help or you can provide resources in this regard
    thanks

  6. robfarley

    This isn’t VB.Net code. This is PowerShell to put into PowerGadgets. In VB.Net you could use a WebClient object to download the string from that location (altered to the right match), and then use a regular expression to get the score.

    But I’m not going to write the code for you – this was an exercise in PowerGadgets, not VB.Net

Leave a Reply

LobsterPot Blogs

Blog posts by Rob Farley and other LobsterPot Solutions team members.

Search