diff --git a/airsenal/framework/schema.py b/airsenal/framework/schema.py index e3d6e0e3..31b4a1e5 100644 --- a/airsenal/framework/schema.py +++ b/airsenal/framework/schema.py @@ -400,6 +400,24 @@ class SessionBudget(Base): id = Column(Integer, primary_key=True, autoincrement=True) session_id = Column(String(100), nullable=False) budget = Column(Integer, nullable=False) + + +class TransferPriceTracker(Base): + __tablename__ = "transfer_price_tracker" + id = Column(Integer, primary_key=True, autoincrement=True) + player_id = Column(Integer, ForeignKey("player.player_id")) + gameweek = Column(Integer, nullable=False) + season = Column(String(4), nullable=False) + timestamp = Column(String(100), nullable=False) + price = Column(Integer, nullable=False) + transfers_in = Column(Integer, nullable=False) + transfers_out = Column(Integer, nullable=False) + + def __str__(self): + return ( + f"{self.player_id} ({self.season} GW{self.gameweek}): " + f"£{self.price / 10}, {self.transfers_in}, {self.transfers_out}" + ) def get_connection_string(): diff --git a/airsenal/scripts/price_change_data.py b/airsenal/scripts/price_change_data.py new file mode 100644 index 00000000..89d0f17a --- /dev/null +++ b/airsenal/scripts/price_change_data.py @@ -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)