Releasing fpl 0.5.5 - Adding MongoDB support, fixture difficulty ranking and more

in #fpl6 years ago




fpl


A Python wrapper around the Fantasy Premier League API.

Repository

https://github.com/amosbastian/fpl

Pull request(s)

https://github.com/amosbastian/fpl/pull/14
https://github.com/amosbastian/fpl/pull/15
https://github.com/amosbastian/fpl/pull/16
https://github.com/amosbastian/fpl/pull/17

Installation and upgrading

# installation
pip install fpl

# upgrading
pip install fpl --upgrade

# installing from GitHub
git clone https://github.com/amosbastian/fpl.git
cd fpl
pip install -e .

Over the last few days I've been adding a few features that I will personally need. One of them is MongoDB support, since I like to have a database with all the players in it, as it takes a long time retrieve all players from the API. I've also added a new fixture difficulty ranking, which I have touched on in previous posts (including ones about Nutmega). Lastly, I've added a way to import Fantasy Premier League accounts using the CLI, so you don't need to remember your user ID anymore.

MongoDB

In pull request 14 and 16 I added the update_mongodb() function to the FPL class. What is does is create a new database called fpl with the collections teams and players. It then uses the get_teams() and get_players() functions to get a list of objects, converts them to a dictionary and inserts them in each respective collection.

It also uses the FDR() function (discussed below) to add a new fixture difficulty ranking to each team's fixtures. This is important because it will allow users, if they have MongoDB installed, to quickly create a database for easy accessibility.

One thing I still need to implement is a way to create Player and Team objects directly from the database. Currently if you use the players in a database for example, it's basically the same as working with a dictionary, and doesn't include the functions their respective classes have.

Calculating the FDR and points scored against

I've talked about this before, but the fixture difficulty ranking (FDR) that the Fantasy Premier League uses is really shit. It doesn't take into account what the difficulty is in Fantasy Premier League terms, but it's more a measure of how difficulty it is to win the game. Of course this isn't really that important when the objective is to score the most points (obviously it's not that simple). Because of this I thought of a new way to determine a fixture's difficulty ranking: by simply looking at how many FPL points a team concedes per position.

So basically I created two functions, get_points_against(), which as the name implies gets the points scored against a team in the Fantasy Premier League. After this I added the function FDR() which uses these points to create a ranking scaled between 1.0 and 5.0, depending on the amount of points scored by players of a certain position (goalkeepers, defenders, midfielders and forwards). I implemented something similar when creating the initial Nutmega application, so it wasn't that hard to wrap my head around.

What can you do with this? Well, let me show you a small code snippet that I created to show the FDR for each team per position and location:


carbon (1).png

which outputs the following:

The above shows, for example, that if you have any defenders playing against Man City away from home, then you probably shouldn't start them... what a surprise! In all seriousness, this should actually be really helpful for team selection and I will be using it to create a linear optimiser that creates the best team in the world!

I also added the FDR to each team's fixtures, so you can use that to see which team has the easiest upcoming fixtures for example. This can be useful for long term planning of transfers and such, and I'll probably expand on this in the future as well.

Import your FPL account

Usage: fpl [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  deleteaccount  Deletes an imported FPL account.
  importaccount  Imports an FPL account.
  listaccounts   Lists all imported FPL accounts.
  myteam         Echoes a logged in user's team to the terminal.
  picks          Echoes a user's picks to the terminal.

In the last update I added a CLI with the command myteam, which outputs the team of a (logged in) user to the terminal. Usually you'd have to type in your FPL user ID, as well as your email and password (if not set as environment variables). This is of course a hassle, especially because the user ID is quite long, so inspired by beem I added the commands importaccount, deleteaccount and listaccounts.

Full disclaimer: I have never really used MySQL for stuff other than university assignments, so I'm probably doing things wrong or in an inefficient way. If that's the case, then please let me know!

The new CLI commands

As the name suggests the importaccount command allows you to import an FPL account. To implement this I had to add a few validation checkers, like making sure the user is actually logged in properly and if the user ID matches the email address. Unfortunately the API doesn't include an easy way to do this, so I just did the follow:

  • When logging in, check if the session's response contains the text "Incorrect email or password", and if so, log out
  • When using the User.my_team() function, check if the user is allowed to access the user with the given ID's team, and if not, then raise an error

Pretty simple, but this should prevent users from importing accounts with FPL IDs and email addresses that aren't associated to each other. Once an account imported then that means the myteam command can now be used without entering a user ID each time!

importaccounts.gif
Usage of importaccountand listaccounts commands

Of course you want to keep track of the accounts you have imported, which can be done by using the listaccounts command as seen in the GIF above. Also, sometimes you will want to get rid of an imported account, which can be done by using the deleteaccount command.


deleteaccount.gif
Usage of the myteam command when an account is imported and the deleteaccount command

CLI roadmap

I have a couple of commands / things I want to add to the CLI:

  • A way to add an account locally, similar to beempy importaccount
  • Favouriting users, since it's a hassle to type in a user's ID, and there is seemingly no way to map an email address to a user ID using their API
  • A command that streams information about your team / all the players currently playing if the gameweek is live

If you have any thoughts about this please let me know. Especially ideas for new commands or suggestions to improve existing ones are very much appreciated!


Also thanks to @emrebeyler for telling me to use find_packages() in setup.py! It should hopefully have fixed the ModuleNotFoundError that was occurring in the previous version(s).

Sort:  

Regarding github installation method:

# installing from GitHub
git clone https://github.com/amosbastian/fpl.git
cd fpl
pip install -e .

Also, It can be done with a one-liner:

pip install -e git://github.com/amosbastian/fpl.git#egg=fpl

On a minor note, using a logger instead of prints
may be more extensive and flexible.


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? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @emrebeyler!

So far this week you've reviewed 3 contributions. 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

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

Award for the total payout received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

Presentamos el Ranking de SteemitBoard

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

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!

Coin Marketplace

STEEM 0.31
TRX 0.11
JST 0.034
BTC 65139.82
ETH 3206.69
USDT 1.00
SBD 4.16