Updating my FPL bot for Reddit

in #fpl6 years ago






https://github.com/amosbastian/FPLbot

In 2015, around October, I was really getting into playing https://fantasy.premierleague.com/ (although I had played it before, I'd normally stop caring about halfway through the season). It was around that time that I found the subreddit r/FantasyPL, which contained a lot of useful information. One of the daily posts was the daily price changes of players, and I noticed this was being done manually by someone. I decided to message him and ask if it would be okay for me to create a Reddit bot that would do this automatically, as I was looking to learn something new and wanted to be helpful. Of course he replied that it was completely fine, and so I set out creating the first version of the bot, written in Python 2.7.

I didn't know as much about programming back then, and so it took me quite a while to finish it. I also didn't know much about FPL's API, and learnt most of the basics (just retrieving players) from an article I can't seem to find anymore. I was also using http://fplstatistics.co.uk/ to get the price changes every day, since I didn't realise you could easily find this information using FPL's API either. Obviously this sometimes caused problems, since everything was hardcoded and so when they changed something, it would mess up the bot.

There were also plenty of other bugs or funny things, that in the end sort of turned into memes in the FPL community. Stuff like "Player Price Changes (13rd March 2018)" as a title, or the thumbnail always being an advertisement for a "Cumbrian bungalow", because it linked the fplstatistics website in its body.

Anyway, after a couple of years, and with me updating fpl to an asynchronous version, I've finally decided to rework the bot and add some new features! The first version is basically a rework of the current version, without any additional features, so it's nothing special, but I think it will be a good example for others on how to use fpl as well.

https://github.com/amosbastian/FPLbot/pull/1

The old bot

I thought it would be funny to show you guys what the old bot looks like. I never made it open source, because I was embarrassed about how bad it was (and I made it before I had even heard of "open source"). I haven't really touched it much since I wrote it all those years ago, except for last year when I moved it to a droplet and rewrote part of it so it was compatible with Python 3 (I was hosting it on an Amazon server before, which cost me a lot once the free year ran out...). For some reason I would align the assignment of variables, had never heard of PEP 8 and in general didn't know much.



Saving players to a pickle file

Instead of using a database I would create a pickle file and save the players in there. Pretty weird way of doing things, but it worked!


old_bot.png
The old bot

Very readable, right?! I can remember how long it took me to figure out which headers to use in the request - great memories.

The new bot

Instead of using a pickle file, I decided to use MongoDB, since all the players are JSON anyway. Now the function for updating the players simply looks like this:



Updating players and storing them in a database

As for the rest - I mentioned before that the functionality is the same, but it has been rewritten, and is of (hopefully) better quality. Instead of creating the table in multiple nested for loops and if statements it has been reduced to this:



Creating the price change table

Instead of being reliant on , it instead simply retrieves the players and compares each player's price to find out who changed in price:



Getting the players whose price changed

There is obviously more to compare, but you get the point. It also now includes a configuration file, so it can easily be used on any other subreddit, or with another account. Also, the post now has its own template which can easily be changed, instead of being partly defined in the code shown above, or the long string you can see in the screenshot below:



The configuration file of the old bot

I was heavily inspired by @emrebeyler's tagbot while adding a configuration file and template, so I'd like to thank him for that!

Roadmap

On another subreddit for Manchester United supporters there is a bot called goalbot, which can be called with the command !goalbot <player>, <opponent>. In this case <player> is obviously a player, like Cristiano Ronaldo, and <opponent> is a football team - it then lists videos and/or GIFs of all the goals the player has scored against that particular team.

I want to do something similar, but instead of showing GIFs it will show xG (expected goals) and xA (expected assists) against that particular opponent. I've found a website called https://understat.com/ where this information is available - I'll need to find out a way to scrape it, since I emailed them and they said they don't have an API.

I would also like to add something that allows people to compare players against each other, over the last N gameweeks. Basically, something similar to this, but where people can just type "!fplbot aguero vs. sane, 5" to get a nicely formatted table as a reply.

Usage & installation

FPLbot uses MongoDB to store players in a database, and so it is required to have MongoDB installed. Other than that, it uses fpl to retrieve information from Fantasy Premier League's API, and thus requires Python 3.6+.

git clone [email protected]:amosbastian/FPLbot.git
cd FPLbot
pip install -r requirements.txt



Once installed you should create a config.json file like the above example, but with the correct values. Once you have done this, then you can schedule a cron job to run the bot whenever you want!

Contributing



I've created a Discord server for people interested in programming FPL related things, so if you are interested in helping out, or simply want to know more, then don't hesitate to join!

Sort:  

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @helo! Keep up the good work!

Hi @amosbastian!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @amosbastian!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Hi, @amosbastian!

You just got a 0.07% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Congratulations @amosbastian! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 11000 upvotes. Your next target is to reach 12000 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.026
BTC 57383.94
ETH 2450.29
USDT 1.00
SBD 2.40