JavaCricbuzz – Cricket API for Java

I have already written an API for python which can be found at pycricbuzz . Many people asked me as how can they use this library in python. I am not very good at python but I gave it a try and converted the python code to java.

Cricbuzz provides the live scores through its XML feed. We can parse this XML file to gather a lot of information. One of the easiest way to parse a XML file in java is through Jsoup library. But you don’t need to worry about parsing it  since I have done it. You can directly access the live matches, commentary and scorecards by using this library in java.

Importing the library

    1. Include the source code of library in your code. This will give you more control over the library.
      https://github.com/codophobia/JavaCricbuzz
      “Cricbuzz.java” should be included in a package “cricbuzz” and you also need to add jsoup library.
    2. Include the compiled jar file in your package library
      cricbuzz.jar
import cricbuzz.Cricbuzz;

Create a cricbuzz object

Cricbuzz c = new Cricbuzz();

We can use this object to work with all the functions provided bylibrary.

Fetch all the matches provided by cricbuzz XML feed

Vector<HashMap<String,String>> matches = c.matches();
 Gson gson = new GsonBuilder().setPrettyPrinting().create();
 String json = gson.toJson(matches);
 System.out.println(json);

Output:


[
  {
    "mchstate": "stump",
    "srs": "The Ashes, 2017-18",
    "mnum": "5th Test",
    "id": "3",
    "mchdesc": "AUS vs ENG",
    "type": "TEST",
    "status": "Aus lead by 133 runs"
  },
  {
    "mchstate": "stump",
    "srs": "India tour of South Africa, 2017-18",
    "mnum": "1st Test",
    "id": "4",
    "mchdesc": "RSA vs IND",
    "type": "TEST",
    "status": "RSA lead by 142 runs"
  },
  {
    "mchstate": "complete",
    "srs": "Pakistan tour of New Zealand, 2018",
    "mnum": "1st ODI",
    "id": "5",
    "mchdesc": "NZ vs PAK",
    "type": "ODI",
    "status": "NZ won by 61 runs (DLS method)"
  },
  {
    "mchstate": "Result",
    "srs": "Big Bash League, 2017-18",
    "mnum": "19th Match",
    "id": "18510",
    "mchdesc": "MLS vs MLR",
    "type": "T20",
    "status": "MLR won by 6 wkts"
  },
  {
    "mchstate": "Result",
    "srs": "Super Smash, 2017-18",
    "mnum": "22nd Match",
    "id": "18642",
    "mchdesc": "CD vs OTG",
    "type": "T20",
    "status": "OTG won by 3 wkts"
  }
]
    

We get a list of matches with each match having its details. The ‘id’ attribute of a match is important as it will be used to fetch commentary and scorecard for that match.Note: Commentary, scorecard and live score won’t be available for “nextlive” matches.

Fetching the live score for a match

String id = matches.get(0).get("id");
Map<String,Map> score = c.livescore(id);
json = gson.toJson(score);
System.out.println(json);

Output:


{
  "matchinfo": {
    "mchstate": "stump",
    "srs": "The Ashes, 2017-18",
    "mnum": "5th Test",
    "id": "3",
    "mchdesc": "AUS vs ENG",
    "type": "TEST",
    "status": "Aus lead by 133 runs"
  },
  "batting": {
    "score": [
      {
        "overs": "157",
        "runs": "479",
        "wickets": "4",
        "desc": "1st Inns"
      }
    ],
    "team": [
      {
        "team": "AUS"
      }
    ],
    "batsman": [
      {
        "six": "0",
        "balls": "207",
        "name": "Shaun Marsh",
        "fours": "10",
        "runs": "98"
      },
      {
        "six": "2",
        "balls": "87",
        "name": "Mitchell Marsh",
        "fours": "9",
        "runs": "63"
      }
    ]
  },
  "bowling": {
    "bowler": [
      {
        "name": "James Anderson",
        "maidens": "11",
        "overs": "30",
        "runs": "52",
        "wickets": "1"
      },
      {
        "name": "Tom Curran",
        "maidens": "2",
        "overs": "20",
        "runs": "71",
        "wickets": "0"
      }
    ],
    "score": [
      {
        "overs": "112.3",
        "runs": "346",
        "wickets": "10",
        "desc": "1st Inns"
      }
    ],
    "team": [
      {
        "team": "ENG"
      }
    ]
  }
}

It gives us the information about the match and details of the batting and bowling team. The batsman and bowlers are the current two batsman batting and current two bowlers bowling.In case match has not started,you will get an empty dictionary as output.

Fetch commentary of the match

Map<String,Map> comm = c.commentary(id);
json = gson.toJson(comm);
System.out.println(json);

Output:


"matchinfo": {
    "mchstate": "stump",
    "srs": "The Ashes, 2017-18",
    "mnum": "5th Test",
    "id": "3",
    "mchdesc": "AUS vs ENG",
    "type": "TEST",
    "status": "Aus lead by 133 runs"
  },
  "commentary": {
    "text": [
      "This is what Tristan, our correspondent, has to say, \"A flagging England was battered by Australia and it appears a matter of time before the hosts claim victory and a 4-0 series triumph. Australia\u0027s maligned batting order methodically turned the screws against a tiring England who probably can\u0027t wait for their gruelling Ashes campaign to end. England need to conjure the miraculous on day four to turn this into a contest otherwise Australia may sniff a victory within four days.\"",
      "Crane bamboozled Khawaja in the fourth over after tea and that brought in Mitchell Marsh, who was struggling against the 20-year-old leggie. But, the younger Marsh battled it out for an hour and later decided to play his shots. From 5 off 33 balls, Mitchell Marsh raced away to bring up his fifty by scoring the next 45 runs in just 31 deliveries. Shaun Marsh, on the other hand, is two runs short of his second Ashes century. The Marsh brothers have added 104 runs and the lead is now 133. 4-0, 3-0 or 3-1? The probability decreases in the same order. With the pitch expected to deteriorate further, Lyon must be eager to bowl on this track. Don\u0027t forget to join us tomorrow.",
      "1, 0 and 1 - wickets taken by England today session-wise. The overnight batsmen continued from where they left yesterday. Khawaja brought up his maiden Ashes ton early in the morning session and it seemed as if the duo would go another session unscathed. But, Smith got out to Moeen Ali before lunch and England should have had Khawaja as well. The debutant - Mason Crane - had overstepped by a centimeter and missed out on his maiden Test wicket. He eventually got it by dismissing the same batsman, but in between that was a wicketless session, where Khawaja and Shaun Marsh ensured that their team got into the lead.",
      "156.6 Anderson to Mitchell Marsh, no run, back of a length outside off, Marsh is happy to wave it through to Bairstow. The brothers shake hands and walk off to a standing ovation. They have the whole of tomorrow to get their respective milestones",
      "156.5 Anderson to Mitchell Marsh, no run, Shaun Marsh will remain unbeaten on 98. Nothing easy from Anderson, keeps it tight close to off, Marsh gets behind the line and blocks it to the left of the bowler",
      "156.4 Anderson to Mitchell Marsh, no run, short and outside off, Marsh chops the cut to backward point",
      "156.3 Anderson to Mitchell Marsh, no run, fuller and teasing the batsman to drive, Marsh lets it pass",
      "156.2 Anderson to Mitchell Marsh, no run, short of length around off, Marsh taps it towards cover-point",
      "156.1 Anderson to Shaun Marsh, 1 run.. and Marsh takes a single first ball. Angled in from back of a length and Marsh knocks it to mid-on, calls Mitch through for a run",
      "All his eyes on Shaun. The entire Australia team is out in the balcony. The crowd are anticipating and getting vocal..",
      "155.6 Tom Curran to Mitchell Marsh, no run, blocks the last delivery and leaves his brother six deliveries to reach his hundred. Back of a length and outside off, Marsh shuffles across and defends it to mid-off",
      "155.5 Tom Curran to Mitchell Marsh, FOUR, 100-run stand comes up. First for the Marsh brothers. They shake hands and get on with it. Short and width provided outside off, Marsh needs no second invitation, hangs back and cuts it fiercely behind point"
    ]
  }
}

“Commentary” is a list containing all the commentary texts.

Here is my sample code which I used for testing the library:

package test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.*;
import cricbuzz.Cricbuzz;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException{
        Cricbuzz c = new Cricbuzz();
        Vector<HashMap<String,String>> matches = c.matches();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(matches);
        System.out.println(json);
        
        Integer i = 0;
        for(i = 0; i < matches.size(); i++) {
            String id = matches.get(i).get("id");
            Map<String,Map> score = c.livescore(id);
            json = gson.toJson(score);
            System.out.println(json);
            
            Map<String,Map> comm = c.commentary(id);
            json = gson.toJson(comm);
            System.out.println(json);
            
            break;
        }
    }
}

 

7 thoughts on “JavaCricbuzz – Cricket API for Java

Add yours

  1. thank u bro …Can we get commentary section of a particular match(using this api i am getting info of all matches).. if so how can i get that because i tried specifying id of a match but i got an error so can you plaese send me a basic python code to extract commentary section of a match
    My mail id:veeramanikannan1165@gmail.com
    Please do respond ..i need that for my work

    Like

  2. Bro i got the code executed but still i was able to get only information for some 2 overs from commentary section but is there a way to get entire match information

    Like

    1. There is no direct way to get the full commentary. The only thing you can do is to store the commentary at some place after each over. At the match end, you will have the full commentary.
      Other way of getting the full commentary is to scrape cricbuzz or cricinfo which I do not recommend.
      Do leave a star on my github project in case you liked it 🙂

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: