From dae42443069cef72fb8898d407aaa9b12104d11a Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 11 Aug 2013 13:47:54 +0100 Subject: [PATCH] Added command to cricket-scores plugin --- plugins/cricket-score.py | 49 +++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/plugins/cricket-score.py b/plugins/cricket-score.py index 6a0c41d0..3ea97d5b 100644 --- a/plugins/cricket-score.py +++ b/plugins/cricket-score.py @@ -3,8 +3,8 @@ import urllib2 import json import time -#score_url = "http://api.scorescard.com/?type=score&teamone=Australia&teamtwo=England" -score_url = None +score_url = "http://api.scorescard.com/?type=score&teamone=Australia&teamtwo=England" +#score_url = None summary = None @@ -12,25 +12,51 @@ summary = None def prof_init(version, status): if score_url: prof.register_timed(get_scores, 60) + prof.register_command("/cricket", 0, 0, "/cricket", "Get latest cricket score.", "Get latest cricket score.", cmd_cricket) def prof_on_start(): if score_url: get_scores() +# commands +def cmd_cricket(): + global score_url + global summary + new_summary = None + + result_json = retrieve_scores_json() + + if 'ms' in result_json.keys(): + new_summary = result_json['ms'] + + prof.cons_show("") + prof.cons_show("Cricket score:") + if 't1FI' in result_json.keys(): + prof.cons_show(" " + result_json['t1FI']) + + if 't2FI' in result_json.keys(): + prof.cons_show(" " + result_json['t2FI']) + + if 't1SI' in result_json.keys(): + prof.cons_show(" " + result_json['t1SI']) + + if 't2SI' in result_json.keys(): + prof.cons_show(" " + result_json['t2SI']) + + summary = new_summary + prof.cons_show("") + prof.cons_show(" " + summary) + prof.cons_alert() + # local functions def get_scores(): global score_url global summary - notify = None new_summary = None change = False - req = urllib2.Request(score_url, None, {'Content-Type': 'application/json'}) - f = urllib2.urlopen(req) - response = f.read() - f.close() - result_json = json.loads(response); + result_json = retrieve_scores_json() if 'ms' in result_json.keys(): new_summary = result_json['ms'] @@ -62,3 +88,10 @@ def get_scores(): prof.cons_show(" " + summary) prof.cons_alert() prof.notify(notify, 5000, "Cricket score") + +def retrieve_scores_json(): + req = urllib2.Request(score_url, None, {'Content-Type': 'application/json'}) + f = urllib2.urlopen(req) + response = f.read() + f.close() + return json.loads(response);