The Match Condition History script collates time and weather data over all of a team’s matches in a season (in this case, Everton’s Premier League matches in the 2011-12 season). These results could be used to provide context for other statistics, but even as an observation into changing weather conditions over a nine-month season they are interesting.
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 15 lines.
Concept
This script calls match.information.get() and match.link.get(), making use of the hypertext provided with the match.information response.
Walkthrough
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 prefer not to repeat ourselves in our code, so we create a club_name variable and assign “Everton” to it. If we want to repeat this analysis for another team, just change the team name.
club_name = "Everton"
Next, we retrieve match information data from all of Everton’s matches. We do this by first querying for all of Everton’s home matches, then all of Everton’s away matches. The result from both queries is a list, which we can join into a bigger list by concatenation.
Results from the API are unsorted, so we sort the results by match date. (You can also sort by matchday but not all matches are played in order.)
Note
Data payloads from the API are accessed using dot notation, in the same manner that JSON objects are accessed in Javascript.
matches = []
for key in ['home_team_name','away_team_name']:
param = {key: club_name}
matches.extend(client.match.information.get(**param).all())
sorted_matches = sorted(matches, key=lambda k: k.match_date)
Now we can iterate over the sorted match list. Make sure you use the correct variable! For every match record, retrieve its match condition record through the conditions hyperlink associated with the match record. Finally we print the match date, kickoff time, and kickoff temperature for each match.
print "Match Date,Kickoff Time,Kickoff Temp"
for match in sorted_matches:
condition = client.link.get(match.link.conditions).data[0]
print "%s,%s,%2.1f" % (match.match_date, match.kickoff_time, condition.kickoff_temp)
The results look something like this:
Match Date | Kickoff Time | Kickoff Temp |
---|---|---|
2011-08-20 | 16:00:00 | 19.0 |
2011-08-27 | 16:00:00 | 16.0 |
2011-09-10 | 16:00:00 | 22.0 |
2011-09-17 | 16:00:00 | 14.0 |
2011-09-24 | 13:45:00 | 17.0 |
2011-10-01 | 13:45:00 | 26.0 |
2011-10-15 | 18:30:00 | 14.4 |
2011-10-23 | 14:30:00 | 19.0 |
2011-10-29 | 13:00:00 | 11.0 |
2011-11-05 | 13:45:00 | 11.0 |
2011-11-19 | 16:00:00 | 10.0 |
2011-11-26 | 16:00:00 | 12.0 |
2011-12-04 | 16:00:00 | 6.0 |
2011-12-10 | 16:00:00 | 4.2 |
2011-12-17 | 16:00:00 | 5.0 |
2011-12-21 | 21:00:00 | 13.0 |
2011-12-26 | 16:00:00 | 12.0 |
2012-01-01 | 13:30:00 | 10.0 |
2012-01-04 | 21:00:00 | 10.0 |
2012-01-11 | 20:45:00 | 10.0 |
2012-01-14 | 16:00:00 | 3.0 |
2012-01-21 | 16:00:00 | 7.0 |
2012-01-31 | 21:00:00 | 0.0 |
2012-02-04 | 16:00:00 | -1.0 |
2012-02-11 | 16:00:00 | 2.0 |
2012-03-03 | 16:00:00 | 14.0 |
2012-03-10 | 18:30:00 | 11.0 |
2012-03-13 | 21:00:00 | 8.0 |
2012-03-21 | 21:00:00 | 8.0 |
2012-03-24 | 16:00:00 | 19.0 |
2012-03-31 | 16:00:00 | 11.0 |
2012-04-07 | 16:00:00 | 7.0 |
2012-04-09 | 16:00:00 | 10.0 |
2012-04-22 | 13:30:00 | 9.0 |
2012-04-28 | 16:00:00 | 10.0 |
2012-05-01 | 20:45:00 | 12.0 |
2012-05-06 | 15:00:00 | 12.0 |
2012-05-13 | 16:00:00 | 15.0 |
So there’s the data in comma-delimited format. You can use the csv module to create a CSV file in cleaner way, or you can save the data to a list or dictionary.
You can visualize the raw data using a good plotting library like matplotlib, but that’s beyond the scope of this example.
Full Script
#!/usr/bin/env python
from soccermetrics.rest import SoccermetricsRestClient
if __name__ == "__main__":
client = SoccermetricsRestClient()
club_name = "Everton"
matches = []
for key in ['home_team_name','away_team_name']:
param = {key: club_name}
matches.extend(client.match.information.get(**param).all())
sorted_matches = sorted(matches, key=lambda k: k.match_date)
print "Match Date,Kickoff Time,Kickoff Temp"
for match in sorted_matches:
condition = client.link.get(match.link.conditions).data[0]
print "%s,%s,%2.1f" % (match.match_date, match.kickoff_time,
condition.kickoff_temp)