Skip to content

Instantly share code, notes, and snippets.

@mrc
Created February 2, 2014 12:43
Show Gist options
  • Save mrc/8767862 to your computer and use it in GitHub Desktop.
Save mrc/8767862 to your computer and use it in GitHub Desktop.
Emacs: Retrieve a page from cricinfo and extract the score from the title
(require 'deferred)
(require 'request)
(require 'request-deferred)
(defun extract-title ()
(goto-char (point-min))
(let* ((start-string "<title>")
(end-string "</title>")
(start (search-forward start-string))
(end (- (search-forward end-string) (length end-string))))
(buffer-substring start end)))
(defun parse-title (title)
(cl-labels ((m (n) (match-string-no-properties n title))
(c (s n) (cons s (m n)))
(fields (syms) (cl-mapcar #'c syms (number-sequence 1 100))))
(cond
((string-match "\\(.+\\) \\([[:digit:]]+\\)/?\\([[:digit:]]+\\)? (\\([[:digit:]]+\\)\\.\\([[:digit:]]+\\) ov\\(?:, \\([^,]+\\) \\([[:digit:]]+\\)\\(\\*\\)\\)\\(?:, \\([^,]+\\) \\([[:digit:]]+\\)\\(\\*\\)\\)?, \\([^)]+\\) \\([[:digit:]]+\\)/\\([[:digit:]]+\\))" title)
(fields
'(team runs wickets overs in-over
bats1 bats1-runs bats1-no
bats2 bats2-runs bats2-no
bowler bowler-wickets bowler-runs))))))
(defun fetch-match-score (url)
(deferred:$
(request-deferred url
:parser #'extract-title)
(deferred:nextc it #'request-response-data)
(deferred:nextc it #'parse-title))))
(setq u "http://www.espncricinfo.com/the-ashes-2013-14/engine/current/match/636166.html")
(deferred:sync! (fetch-match-score u))
"Eng 25/3 (5.0 ov, JE Root 1*, GJ Maxwell 1/6) | Live Scorecard | ESPN Cricinfo"
"Eng 75/4 (11.0 ov, JC Buttler 6*, EJG Morgan 31*, GJ Maxwell 2/31) | Live Scorecard | ESPN Cricinfo"
"Eng 100/8 (15.1 ov, CJ Jordan 0*, SCJ Broad 2*, NM Coulter-Nile 2/17) | Live Scorecard | ESPN Cricinfo"
"Eng 104/8 (16.0 ov, SCJ Broad 2*, CJ Jordan 4*, NM Coulter-Nile 2/21) | Live Scorecard | ESPN Cricinfo"
"Eng 110/9 (16.6 ov, JW Dernbach 1*, CJ Jordan 9*, JM Muirhead 2/13) | Live Scorecard | ESPN Cricinfo"
"Eng 111 (17.2 ov, CJ Jordan 10*, MA Starc 1/8) - Match over | Live Scorecard | ESPN Cricinfo"
"Ind 41/0 (14.0 ov, M Vijay 19*, S Dhawan 16*, SET Friday 0/17) - Stumps | Live Scorecard | ESPN Cricinfo"
((team . "Ind") (runs . "41") (wickets . "0") (overs . "14") (in-over . "0") (bats1 . "M Vijay") (bats1-runs . "19") (bats1-no . "*") (bats2 . "S Dhawan") (bats2-runs . "16") (bats2-no . "*") (bowler . "SET Friday") (bowler-wickets . "0") (bowler-runs . "17"))
((team . "AusWn") (runs . "102") (wickets . "3") (overs . "18") (in-over . "3") (bats1 . "AJ Blackwell") (bats1-runs . "10") (bats1-no . "*") (bats2 . "EJ Villani") (bats2-runs . "36") (bats2-no . "*") (bowler . "CM Edwards") (bowler-wickets . "0") (bowler-runs . "10"))
"AusWn 102/3 (18.3 ov, AJ Blackwell 10*, EJ Villani 36*, CM Edwards 0/10) - Match over | Live Scorecard | ESPN Cricinfo"
@mrc
Copy link
Author

mrc commented Feb 2, 2014

Works with live scorecard pages I've tried, not so great on e.g. domestic matches in Bangladesh where the title doesn't contain the score...

@mrc
Copy link
Author

mrc commented Feb 2, 2014

@mrc
Copy link
Author

mrc commented Feb 4, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment