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!

Playing with Python

At work we have some dedicated time each week to learning new things or improve on old things, and earlier this week I had randomly read some stuff about Python. Since I have never gotten my hands dirty with Python I decided that it was time to change that.

I started out by following this guide to install Python in my VS Code environment and played a bit with the examples provided.

Then I decided to write a function to base64 encode username and password to be used for basic auth, just to see if I could:

import base64

EncodingAsString = "utf-8"
BasicPrefix = "Basic"

def GetBasicAuth(username, password):

    encodedString = str(base64.b64encode(str(f"{username}:{password}").encode(EncodingAsString)), EncodingAsString)

    return str(f"{BasicPrefix} {encodedString}")

print(GetBasicAuth("ebolakid", "blog"))

I guess it turned out alright for being written 15 minutes after a Python installation.

EDIT: Please note that the intendation didn’t quite fit the blog…
But copy-paste into your favorite editor should work just fine.