Match Segments

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.

Concept

In the script, we call match.information.get() to retrieve the specific match representation and link.get() to make use of the hypertext linked from the match representations.

The total length of the script is about 20 lines.

Walkthrough

At the very top of the script we import the SoccermetricsRestClient package that will allow us to communicate with the API.

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 define the names of the home and away teams and retrieve match information from the API.

home_club = "Liverpool"
away_club = "Everton"

match = client.match.information.get(home_team_name=home_club,
                                     away_team_name=away_club).all()

Collect all of the players in the match lineup and create a dictionary that links unique player IDs to their full names.

lineup = client.link.get(match[0].link.lineups).all()
players = {x.player:x.player_name for x in lineup}

We obtain all of the segments in the match – all of the periods of play where both sides’ on-field lineup is unchanged.

segments = client.link.get(match[0].link.analytics.segments).all()

Before we loop over the segments, we define a lambda function that creates a list of players separated by commas. A conventional function would have done the job, too.

platoon = lambda rec: ', '.join([players[_id] for _id in rec])

Now we’re ready to loop over the match segments. We output the time at which the segment started, the on-field makeup of the two teams, and the duration of the segment.

for segment in segments:
    if segment.start_stoppage_mins > 0:
        match_time = "%d+%d" % (segment.start_time_mins,
                                segment.start_stoppage_mins)
    else:
        match_time = "%d" % segment.start_time_mins
    print "Start segment: %s" % match_time
    print "Home Players: %s" % platoon(segment.home_players_on)
    print "Away Players: %s" % platoon(segment.away_players_on)
    print "Duration: %s mins" % segment.duration

Results

When we run the script, here are the results:

Start segment: 0
Home Players: Martin Škrtel, Stewart Downing, Jay Spearing, José Enrique, Jamie Carragher, Andy Carroll, José Reina, Luis Suárez, Martin Kelly, Jordan Henderson, Steven Gerrard
Away Players: Steven Pienaar, Tony Hibbert, Tim Howard, Leighton Baines, Denis Stracqualursi, Victor Anichebe, Phil Jagielka, Séamus Coleman, Sylvain Distin, Jack Rodwell, Marouane Fellaini
Duration: 60 mins
Start segment: 61
Home Players: Martin Škrtel, Stewart Downing, Jay Spearing, José Enrique, Jamie Carragher, Andy Carroll, José Reina, Luis Suárez, Martin Kelly, Jordan Henderson, Steven Gerrard
Away Players: Steven Pienaar, Tony Hibbert, Royston Drenthe, Tim Howard, Leighton Baines, Leon Osman, Phil Jagielka, Nikica Jelavić, Sylvain Distin, Jack Rodwell, Marouane Fellaini
Duration: 11 mins
Start segment: 72
Home Players: Martin Škrtel, Stewart Downing, Dirk Kuyt, Jay Spearing, José Enrique, Jamie Carragher, Andy Carroll, José Reina, Luis Suárez, Martin Kelly, Steven Gerrard
Away Players: Steven Pienaar, Tony Hibbert, Royston Drenthe, Tim Howard, Leighton Baines, Leon Osman, Phil Jagielka, Nikica Jelavić, Sylvain Distin, Jack Rodwell, Marouane Fellaini
Duration: 21 mins

Full Script

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from soccermetrics.rest import SoccermetricsRestClient

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

    home_club = "Liverpool"
    away_club = "Everton"

    match = client.match.information.get(home_team_name=home_club,
                                         away_team_name=away_club).all()

    lineup = client.link.get(match[0].link.lineups).all()
    players = {x.player:x.player_name for x in lineup}

    segments = client.link.get(match[0].link.analytics.segments).all()

    platoon = lambda rec: ', '.join([players[_id] for _id in rec])

    for segment in segments:
        if segment.start_stoppage_mins > 0:
            match_time = "%d+%d" % (segment.start_time_mins,
                                    segment.start_stoppage_mins)
        else:
            match_time = "%d" % segment.start_time_mins
        print "Start segment: %s" % match_time
        print "Home Players: %s" % platoon(segment.home_players_on)
        print "Away Players: %s" % platoon(segment.away_players_on)
        print "Duration: %s mins" % segment.duration