Automating Fantasy Premier League Analysis with PowerShell and OpenAI

Automating Fantasy Premier League Analysis with PowerShell and OpenAI

The Premier League is back! Not only do we get non-stop football from now till the end of the season, it heralds the start of a brand-new season of Fantasy Premier League. There is nothing quite like celebrating a Chelsea clean sheet (as a Leicester fan) as Sanchez seals a strong 8 pointer with a clean sheet, a save point and 1 bonus point!

And when our workplace league grew to 20+ people, I thought it might be good to write weekly analysis summaries to keep the conversation flowing over the virtual water cooler. Except manually trawling through everyone's teams, checking transfers, calculating rank changes, and working out who played which chip was going to eat up hours of my week. Hours I could be spending on more important things, like agonizing over next week's captain choice.

So, I did what any sensible techie would do - I automated the hell out of it.

The Lightbulb Moment


After about 45 minutes of looking through everyone's teams to find who made the best captain choice or went with a tactical Gameweek 1 bench boost, I'm thinking there has to be a better way. That's when I remembered the FPL API exists, and I know how to write PowerShell scripts. A perfect combination.

The plan was simple: build a script that pulls all the data I was going to manually collect, make sense of it, and spit out a proper analysis. No more manually calculating who gained how many places, no more missing that someone played their Bench Boost, and definitely no more forgetting to mention a jammy 22-pointer from Ekitike.

Writing the Script

The FPL API is actually pretty decent once you figure out the endpoints. The main ones you need are:

- League standings (who's where and how many points)

- Individual team picks (who they've got and who's captain)

- Live gameweek data (the actual points)

- Player database (so you know who these people actually are)

Getting the data out is straightforward enough with Invoke-RestMethod, but the real work happens when you start making sense of it all.

# This gets you the league table
$leagueUrl = "https://fantasy.premierleague.com/api/leagues-classic/$leagueId/standings/"
$leagueData = Invoke-RestMethod -Uri $leagueUrl

# And this gets the juicy details for each team
foreach ($team in $leagueData.standings.results) {
    $teamUrl = "https://fantasy.premierleague.com/api/entry/$($team.entry)/event/$gameweek/picks/"
    $picksData = Invoke-RestMethod -Uri $teamUrl
    # Now we've got everything...
}

The tricky bit is handling all the FPL quirks. Triple Captain means your captain gets 3x points instead of 2x. Bench Boost means those bench players actually count. Free Hit means the team reverts next week. So, with a helping hand from GitHub Copilot, I put together a script that could pull all of that data together, which you can find here: https://github.com/joshban99/fpl-automation

But here's where it gets interesting - once I had all this data structured properly, I could feed it to an OpenAI model and let it write the actual analysis.

Using AI for the better good...

Once I've got all the data nicely structured - who scored what, transfers made, who played which chip - I dump the whole lot into a JSON file and send it off to Azure OpenAI. This is where the magic happens.

Instead of me writing "Bench boost was good for a few players," the AI turns it into "Bench Boost was notably utilized by Player A with outstanding results, clearly paying off handsomely. Player B and Player C also dipped into their chips, but they only brought minor boosts!"

The prompt engineering took a bit of tweaking. I wanted the tone to be workplace-friendly but still capture that FPL drama we all love. Nothing too harsh when someone's had a mare, but definitely calling out the brilliant moves and the questionable decisions.

$messages = @(
    @{
        role = 'system'
        content = "You're analyzing Fantasy Premier League data for a workplace league. Be engaging, slightly competitive, but keep it friendly..."
    },
    @{
        role = 'user' 
        content = "Here's this week's data: $jsonData"
    }
)

$response = Invoke-RestMethod -Uri $azureOpenAIUrl -Headers $headers -Body ($body | ConvertTo-Json) -Method Post

The Results

What could have taken me the better part of an hour now takes about 30 seconds. The script runs, pulls all the data, crunches the numbers, gets the AI to write it up, and copies the result straight to my clipboard. I paste it into our league chat, and suddenly I look like I've spent ages crafting the perfect analysis.

What does that mean? We've got proper banter going about transfer strategies and captain choices. It's turned a casual workplace league into something genuinely competitive and fun.

Conclusion

This started as a way to increase engagement for my work league, but it's become something much more useful. It's shown me how powerful the combination of good data and AI analysis can be, even for something as simple as Fantasy Football.

The pattern is there for anything really - if you've got a repetitive analysis task, chances are you can automate the data collection and get AI to handle the insights. Whether it's sales reports, infrastructure monitoring, or yes, Fantasy Premier League analysis.

Would I recommend building your own FPL analysis tool? Absolutely, if you're that way inclined. Will it improve your actual FPL performance? Probably not - I'm still terrible at making transfers. But it might just make your league a whole lot more fun.

And if you're wondering, yes, I'm sharing this after a disappointing Gameweek 1 where my front line blanked spectacularly. Some things never change, even with automation.

*The script is up on GitHub (https://github.com/joshban99/fpl-automation) if anyone wants to adapt it for their own league. Just don't blame me when you spend more time tweaking the analysis than you saved by automating it in the first place.*