ESPN cricket score OSD

I like to keep up with the cricket match scores when Sri Lanka is playing, but don't want to sit in-front of a TV or don't have the time for it. I use ESPN Crickinfo to check the score, but it is tedious to go to the site and click around to locate the match each time I need to know the score.

I wrote this python script to pull the score from match page and display on screen via libnotify.

Warning: I strongly discourage using this method for anything other than personal use, as it is prohibited by the ESPN Crickinfo terms of use and on may other web sites on the Internet.

cricinfo-score.py
  1. #!/usr/bin/python -tt
  2. #
  3. #  cricinfo-score.py: Fetch match scores from espncricinfo.com and display
  4. #  Copyright (C) 2012 Sudaraka Wijesinghe
  5. #
  6. #  This program is free software: you can redistribute it and/or modify
  7. #  it under the terms of the GNU General Public License as published by
  8. #  the Free Software Foundation, either version 3 of the License, or
  9. #  (at your option) any later version.
  10. #
  11. #  This program is distributed in the hope that it will be useful,
  12. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #  GNU General Public License for more details.
  15. #
  16. #  You should have received a copy of the GNU General Public License
  17. #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. #
  20. #  USAGE: ./cricinfo-score.py <URL>
  21. #
  22.  
  23. from gtk import gdk
  24. from os.path import *
  25. import sys
  26. import re
  27. import urllib
  28. import time
  29. import pynotify
  30.  
  31.  
  32. # Output subroutine
  33. def out(text, title = None, delay = 10000):
  34.   if None == title or 0 == len(title):
  35.     title = 'cricinfo-score.py'
  36.  
  37.   #print title
  38.   #print text
  39.  
  40.   notify = pynotify.Notification(title, text)
  41.   # 10s time out
  42.   notify.set_timeout(delay)
  43.  
  44.   icon = gdk.pixbuf_new_from_file_at_size(abspath(dirname(__file__)) + '/cricket_ball.png', 50, 50)
  45.   notify.set_icon_from_pixbuf(icon)
  46.  
  47.   try:
  48.     notify.show()
  49.   except:
  50.     sys.exit(3)
  51.  
  52.  
  53. # Start main program
  54. if '__main__' == __name__:
  55.  
  56.   # Initialize notification
  57.   pynotify.init('cricinfo-score.py')
  58.  
  59.   # Get URL from command line
  60.   if 2 > len(sys.argv):
  61.     out('''
  62. USAGE: cricinfo-score.py <URL>
  63. ''')
  64.     sys.exit(1)
  65.  
  66.   url = sys.argv[1].split('?')[0]
  67.   if not re.match('^http:\/\/www\.espncricinfo\.com\/', url):
  68.     out('Please enter a valid score card URL')
  69.     sys.exit(2)
  70.  
  71.   url = url + '?template=iframe_desktop'
  72.  
  73.   try:
  74.     while(1):
  75.       # Fetch score card HTML
  76.       try:
  77.         html = urllib.urlopen(url).read()
  78.       except:
  79.         out('Failed to get HTML from score card page')
  80.         time.sleep(60)
  81.         continue
  82.  
  83.       # Process HTML
  84.       html = html.split('<!--score top starts-->')[1]
  85.       html = html.split('<!--score top end-->')[0]
  86.  
  87.       title = re.findall('<p\s+class="desktopHeading">.+:\s+(.+)</p>', html)
  88.       if None != title and 0 < len(title):
  89.         title = title[0].split(',')
  90.         if 0 < len(title):
  91.           title = title[0]
  92.  
  93.       msg = ''
  94.  
  95.       team = re.findall('<td\s+class="desktopTextSeparator">(.+)</td>', html)
  96.       if 0 < len(team):
  97.         msg = msg + '<b>' + team[0] + ':</b> '
  98.         if 1 < len(team):
  99.           msg = msg + team[1]
  100.  
  101.       team = re.findall('<td\s+class="desktopText">(.+)</td>', html)
  102.       if 0 < len(team):
  103.         msg = msg + '\n<b>' + team[0] + ':</b> '
  104.         if 1 < len(team):
  105.           msg = msg + team[1]
  106.  
  107.       status = re.findall('<td\s+class="desktopStatus".*>(.+)</td>', html)
  108.       if 0 < len(status):
  109.         msg = msg + '\n\n<i>' + status[0] + '</i> '
  110.  
  111.         # If status reads "Stumps", end of the day
  112.         if 'Stumps' == status[0]:
  113.           out(msg, title, 5 * 60 * 1000)
  114.           break
  115.  
  116.       result = re.findall('<p\s+class="lveStText">(.+)</p>', html)
  117.       if None != result or 0 < len(result):
  118.         msg = msg + '\n\n<b>' + result[0] + '</b>'
  119.  
  120.         # If result reads "won by", match is over
  121.         if re.search('won\s+by', result[0]):
  122.           out(msg, title, 5 * 60 * 1000)
  123.           break
  124.  
  125.       out(msg, title)
  126.  
  127.       time.sleep(60*5)
  128.  
  129.   except KeyboardInterrupt, SystemExit:
  130.     sys.exit(0)
  131.  
  132.   except:
  133.     out('cricinfo-score.py terminated\n' + str(sys.exc_info()))