o
    :g                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlZee	Z
ddddddZd	ZejZd
d Zdd Zdd Ze	dkrN	 e ZerHee dS ed dS dS )    N)settingsGKDEFMIDFWDMNG)               z7https://fantasy.premierleague.com/api/bootstrap-static/c                     sz  zwt t} |   |  }td|  |d  t|d }||d  }|d d |d< |d d |d< |d	 	d
d |d	< |d 	 fdd|d< |g d j
ddd}|jdddddddddd	d}|jdd}td |W S  t jjy } ztd|  td |  W Y d!}~d!S d!}~w ty } ztd"|  td"|  W Y d!}~d!S d!}~ww )#zG
    Fetches data from the FPL API.  Handles errors and logs them.
    zFPL API response: teamselements
can_selectnow_cost
   selected_by_percentz %element_typec                 S   s   t |  S )N)	POSITIONS)elem r   ./var/www/html/FantasyAPI/fantasy_data/tasks.py<lambda>   s    z fetch_fpl_data.<locals>.<lambda>teamc                    s    | d  d S )Nr   namer   )r   r   r   r   r      s    )	web_namer   formr   r   r   transfers_in_eventtransfers_out_eventtotal_pointsr    F)by	ascendingzPlayer NamePriceFormzSelection %PositionTeamzTransfers InzTransfers OutzTotal Points)columnsrecords)orientz*FPL data fetched and updated successfully.zError fetching FPL data: zError decoding JSON: NAn unexpected error occurred: )requestsgetFPL_API_URLraise_for_statusjsonloggerinfopdjson_normalizemapsort_valuesrenameto_dict
exceptionsRequestExceptionprinterror	Exception)responsedataplayersleaderboarder   r   r   fetch_fpl_data   s8   
 
rB   c              
   C   s(  | du rt d dS z4tjtjtdd ttd}tj	| |dd W d   n1 s/w   Y  t
d t d	 W dS  ttfy_ } zt d
t d|  W Y d}~dS d}~w tyy } zt d|  W Y d}~dS d}~w ty } zt d|  W Y d}~dS d}~ww )zK
    Saves the FPL data to a JSON file.  Handles errors and logs them.
    NzNo data to save.T)exist_okwr   )indentz$Data fetched and saved successfully.zFPL data saved successfully.zError saving FPL data to : z"Type error while saving FPL data: r*   )r0   warningosmakedirspathdirnameDATA_STORAGE_PATHopenr/   dumpr:   r1   IOErrorOSErrorr;   	TypeErrorr<   )r>   frA   r   r   r   save_fpl_data4   s(   
$rS   c               
   C   s   zt td} t| W  d   W S 1 sw   Y  W dS  ty1   tdt d Y dS  tjyD   tdt d Y dS  t	t
fyc } ztdt d|  W Y d}~dS d}~w ty} } ztd|  W Y d}~dS d}~ww )	zi
    Loads the FPL data from the JSON file.  Handles errors, logs them, and returns None on failure.
    rNzFPL data file not found at z.  Returning None.zError decoding JSON from zError reading FPL data from rF   r*   )rM   rL   r/   loadFileNotFoundErrorr0   rG   JSONDecodeErrorr;   rO   rP   r<   )rR   rA   r   r   r   load_fpl_dataK   s&   (rX   __main__zFailed to fetch data.)r+   pandasr2   r/   rH   django.confr   logging	getLogger__name__r0   r   r-   FPL_DATA_FILErL   rB   rS   rX   r>   r:   r   r   r   r   <module>   s(    
'