forked from alan-turing-institute/AIrsenal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [alan-turing-institute#162]: implemented tracking transfers in and …
…out into a new table
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Classes for fetching and storing the transfers in/out for each player | ||
The aim of this is to attempt to predict the price changes of players | ||
and accordingly make transfers to maximise profit | ||
""" | ||
from sqlalchemy.orm.session import Session | ||
|
||
from airsenal.framework.data_fetcher import FPLDataFetcher | ||
from airsenal.framework.schema import session, TransferPriceTracker | ||
from airsenal.framework.utils import NEXT_GAMEWEEK, CURRENT_SEASON | ||
|
||
import datetime as dt | ||
import pandas as pd | ||
|
||
|
||
def fill_global_transfers_from_api(season: str, dbsession=Session): | ||
""" | ||
Get the global transfers in/out at the current time. | ||
""" | ||
fetcher = FPLDataFetcher() | ||
summary: dict = fetcher.get_current_summary_data() | ||
df: pd.DataFrame = ( | ||
pd.DataFrame(summary["elements"]) | ||
.rename(columns={"id": "player_id", "now_cost": "price"})[ | ||
["player_id", "price", "transfers_in", "transfers_out"] | ||
] | ||
.assign( | ||
timestamp=dt.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"), | ||
gameweek=NEXT_GAMEWEEK, | ||
season=season, | ||
) | ||
) | ||
# add to database | ||
for _, row in df.iterrows(): | ||
print(f"Adding transfer data for player {row['player_id']}") | ||
dbsession.add(TransferPriceTracker(**row.to_dict())) | ||
dbsession.commit() | ||
|
||
if __name__ == "__main__": | ||
fill_global_transfers_from_api(CURRENT_SEASON, dbsession=session) |