Python and football

So, I haven’t been playing too much with Python recently but the opportunity came up today so I jumped right in!

To provide some context, we are using Octopus to deploy our stuff to Azure, and in Octopus I wrote a Powershell hack so that Octopus reported to Slack the name of the person doing the deploy. In our build chain we are using TeamCity to push our stuff to Octopus. In true CI/CD spirit some of our things are automatically deployed to Azure when a TC build is finished. When that happens, the person doing the deploy is some generic service account user in Octopus.

My colleague thought this was a boring thing: “When this happens, what about printing the name of a random footballer?” Said and done, one Google search later I found, and signed up for, Football-Data.org. They offered a couple of leagues free of charge with a rate limit of 10 requests per minute. Great! Let’s do some Python:

import random as r
import requests as req
import json as j

def Maximum(obj):
    return len(obj) - 1

def prettyPrintJson(obj):
    text = j.dumps(obj, indent = 4)
    print(text)

Minimum = 0
TokenKey = "X-Auth-Token"
TokenValue = ""
compUrlPrefix = "https://api.football-data.org/v2/competitions/"
compUrlSuffix = "/teams"
teamUrl = "https://api.football-data.org/v2/teams/"
competitions = [2000, 2001, 2002, 2003, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2021]
T = "teams"
I = "id"
S = "squad"
N = "name"

compResponseJson = req.get(compUrlPrefix + str(competitions[r.randint(Minimum, Maximum(competitions))]) + compUrlSuffix, headers={TokenKey: TokenValue}).json()
#prettyPrintJson(compResponseJson)

randomTeam = compResponseJson[T][r.randint(Minimum, Maximum(compResponseJson[T]))][I]

teamReponseJson = req.get(teamUrl + str(randomTeam), headers={TokenKey: TokenValue}).json()
#prettyPrintJson(teamReponseJson)

randomName = teamReponseJson[S][r.randint(Minimum, Maximum(teamReponseJson[S]))][N] + " (" + teamReponseJson[N] + ")"

print(randomName)

So basically, I pick a random league from the free ones, pull all the teams, pull a random team and then pull a random player for that team.

The output looks like this:

Matteo Raspa (UC Sampdoria)

I figured it was a nice touch to add the name of the team as well since I sometimes had no idea what player it was. Suddenly, Emre Can showed up in the result. Seems like the coaches are in there as well!