Classified ResultsΒΆ

The Results script collates full-time results for matches corresponding to a matchday or a range of matchdays. It’s not a very analytical script but it does show how data can be retrieved from the API.

Language:

This script is written in Python 2.7 using the official API client for Python. The client simplifies the process of making HTTP requests to the Soccermetrics API and unpacking the responses.

The total length of the script is approximately 25 lines.

Concept

This script calls match.information.get(). All of the data we need are in the response. We’ll also make use of the sorting functionality so that the response data are in the order we want.

Walkthrough

At the very top of the script we do our imports. Import the sys package (for reasons that will become clear later on) and import the SoccermetricsRestClient package that will allow us to communicate with the API.

import sys
from soccermetrics.rest import SoccermetricsRestClient

In the main routine we create a SoccermetricsRestClient object. You can instantiate it by passing the authentication tokens through the account and api_key variables, but we recommend storing those tokens in environmental variables.

if __name__ == "__main__":
    client = SoccermetricsRestClient()

We’re going to get starting and ending matchdays from the command-line arguments (this is why we needed to import the sys module). The user must enter both ranges so we’ll check for that. We’ll also make sure to convert the argument values to integers.

if len(sys.argv) != 3:
    sys.stderr.write("Usage: python %s <matchday_start> <matchday_end>\n" % sys.argv[0])
    raise SystemExit(1)
matchday_start = int(sys.argv[1])
matchday_end = int(sys.argv[2])

We now iterate between the range of matchdays, and we retrieve match information data from all matches associated with each matchday. (Response data from the API are split into pages, but we can get the full dataset at once with the all() method.) Let’s sort on the match date, and then the kickoff time.

for day in range(matchday_start,matchday_end+1):
    matches = client.match.information.get(matchday=day,
                sort='match_date,kickoff_time').all()

We step into the list of matches, and we retrieve goal and penalty kick events for both teams (described by home_team_name and away_team_name). We use the hyperlinks in the match representation to retrieve goal and penalty kick events under certain conditions. We want the goals credited to each team (this includes own goals) and we only want the penalty kick events that resulted in a goal (outcome_type = Goal). Finally we pretty-print the results.

for match in matches:
    match_goals = []
    for team in [match.home_team_name,match.away_team_name]:
        goals = client.link.get(match.link.events.goals,scoring_team_name=team).all()
        pens = client.link.get(match.link.events.penalties,player_team_name=team,
                               outcome_type="Goal").all()
        match_goals.append(len(goals)+len(pens))

        print "Matchday %02s %s %s %30s %d-%d %-30s" % (match.matchday,
            match.match_date, match.kickoff_time, match.home_team_name,
            match_goals[0],match_goals[1],match.away_team_name)
    print

Here’s how it looks when we run it:

$ python examples_results.py 2 4

And the result:

Matchday  2 2011-08-20 13:00:00                     Sunderland 0-1 Newcastle United
Matchday  2 2011-08-20 13:45:00                        Arsenal 0-2 Liverpool
Matchday  2 2011-08-20 16:00:00                   Swansea City 0-0 Wigan Athletic
Matchday  2 2011-08-20 16:00:00                        Everton 0-1 Queens Park Rangers
Matchday  2 2011-08-20 16:00:00                    Aston Villa 3-1 Blackburn Rovers
Matchday  2 2011-08-20 18:30:00                        Chelsea 2-1 West Bromwich Albion
Matchday  2 2011-08-21 14:30:00                   Norwich City 1-1 Stoke City
Matchday  2 2011-08-21 15:00:00        Wolverhampton Wanderers 2-0 Fulham
Matchday  2 2011-08-21 17:00:00               Bolton Wanderers 2-3 Manchester City
Matchday  2 2011-08-22 21:00:00              Manchester United 3-0 Tottenham Hotspur

Matchday  3 2011-08-27 13:05:00                    Aston Villa 0-0 Wolverhampton Wanderers
Matchday  3 2011-08-27 13:30:00                 Wigan Athletic 2-0 Queens Park Rangers
Matchday  3 2011-08-27 16:00:00                        Chelsea 3-1 Norwich City
Matchday  3 2011-08-27 16:00:00               Blackburn Rovers 0-1 Everton
Matchday  3 2011-08-27 16:00:00                   Swansea City 0-0 Sunderland
Matchday  3 2011-08-27 18:30:00                      Liverpool 3-1 Bolton Wanderers
Matchday  3 2011-08-28 14:00:00               Newcastle United 2-1 Fulham
Matchday  3 2011-08-28 14:30:00              Tottenham Hotspur 1-5 Manchester City
Matchday  3 2011-08-28 16:00:00           West Bromwich Albion 0-1 Stoke City
Matchday  3 2011-08-28 17:00:00              Manchester United 8-2 Arsenal

Matchday  4 2011-09-10 16:00:00                        Everton 2-2 Aston Villa
Matchday  4 2011-09-10 16:00:00                     Stoke City 1-0 Liverpool
Matchday  4 2011-09-10 16:00:00                        Arsenal 1-0 Swansea City
Matchday  4 2011-09-10 16:00:00        Wolverhampton Wanderers 0-2 Tottenham Hotspur
Matchday  4 2011-09-10 16:00:00                Manchester City 3-0 Wigan Athletic
Matchday  4 2011-09-10 16:00:00                     Sunderland 1-2 Chelsea
Matchday  4 2011-09-10 18:30:00               Bolton Wanderers 0-5 Manchester United
Matchday  4 2011-09-11 14:30:00                   Norwich City 0-1 West Bromwich Albion
Matchday  4 2011-09-11 17:00:00                         Fulham 1-1 Blackburn Rovers
Matchday  4 2011-09-12 21:00:00            Queens Park Rangers 0-0 Newcastle United

Full Script:

#!/usr/bin/env python

import sys
from soccermetrics.rest import SoccermetricsRestClient

if __name__ == "__main__":

    client = SoccermetricsRestClient()

    if len(sys.argv) != 3:
        sys.stderr.write("Usage: python %s <matchday_start> <matchday_end>\n" % sys.argv[0])
        raise SystemExit(1)
    matchday_start = int(sys.argv[1])
    matchday_end = int(sys.argv[2])

    for day in range(matchday_start,matchday_end+1):

        matches = client.match.information.get(matchday=day,
                    sort='match_date,kickoff_time').all()

        for match in matches:
            match_goals = []
            for team in [match.home_team_name,match.away_team_name]:
                goals = client.link.get(match.link.events.goals,scoring_team_name=team).all()
                pens = client.link.get(match.link.events.penalties,player_team_name=team,
                                       outcome_type="Goal").all()

                match_goals.append(len(goals)+len(pens))

            print "Matchday %02s %s %s %30s %d-%d %-30s" % (match.matchday,
                match.match_date, match.kickoff_time, match.home_team_name,
                match_goals[0],match_goals[1],match.away_team_name)
        print