
    g                         S SK r S SKrS SKrS SKrS SKJr  S SKr\R                  " \	5      r
SSSSSS.rS	r\R                  rS
 rS rS r\	S:X  a!   \" 5       r\(       a	  \" \5        g\" S5        gg)    N)settingsGKDEFMIDFWDMNG)               z7https://fantasy.premierleague.com/api/bootstrap-static/c                  @  ^  [         R                  " [        5      n U R                  5         U R	                  5       n[
        R                  SU 35        US   m[        R                  " US   5      nX"S      nUS   S-  US'   US   S-   US'   US	   R                  S
 5      US	'   US   R                  U4S j5      US'   U/ SQ   R                  SSS9nUR                  SSSSSSSSSS.	S9nUR                  SS9n[
        R                  S5        U$ ! [         R                  R                   a0  n[        SU 35        [
        R!                  SU 35         S nAg S nAf["         a0  n[        S!U 35        [
        R!                  S!U 35         S nAg S nAff = f)"z?
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                     [         U    $ )N)	POSITIONS)elems    4G:\Python Projects\Fantasy API\fantasy_data\tasks.py<lambda> fetch_fpl_data.<locals>.<lambda>   s	    9UY?    teamc                    > TU S-
     S   $ )Nr	   name )r   r   s    r   r   r      s    5a=;Pr   )	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   s        @r   fetch_fpl_datarG      s    <<,!!#}}(/0W##D$45,/0%j1B6
)01F)G4)O%&")."9"="=>Z"[!&/--.PQ  c  d  p  p  tB  NS  p  T!((mY`jp  IV  fp  x~  Uc  {J  [i  2j(  k!)));@A// )!-.,QC01 .qc235aS9:s$   DD F5&E  F-&FFc                    U c  [         R                  S5        g [        R                  " [        R                  R                  [        5      SS9  [        [        S5       n[        R                  " XSS9  SSS5        [        S5        [         R                  S	5        g! , (       d  f       N/= f! [        [        4 a)  n[         R                  S
[         SU 35         SnAgSnAf[         a"  n[         R                  SU 35         SnAgSnAf[          a"  n[         R                  SU 35         SnAgSnAff = f)zC
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/   )r5   warningosmakedirspathdirnameDATA_STORAGE_PATHopenr4   dumpr?   r6   IOErrorOSErrorr@   	TypeErrorrA   )rC   frF   s      r   save_fpl_datarY   4   s     |)*;
BGGOO$56F#S)QIIda( *4523 *) W K01B0C2aSIJJ ?9!=>> ;5aS9::;sH   AB1 !B 7(B1  
B.*B1 1D>C%%D>2DD>D99D>c                      [        [        S5       n [        R                  " U 5      sSSS5        $ ! , (       d  f       g= f! [         a     [
        R                  S[         S35         g[        R                   a     [
        R                  S[         S35         g[        [        4 a)  n[
        R                  S[         SU 35         SnAgSnAf[         a"  n[
        R                  SU 35         SnAgSnAff = f)	za
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 rL   r/   )rS   rR   r4   loadFileNotFoundErrorr5   rM   JSONDecodeErrorr@   rU   rV   rA   )rX   rF   s     r   load_fpl_datar_   K   s    #S)Q99Q< *)) 45F4GGYZ[ 01B0CCUVWW 34E3FbLM 5aS9:sH   A 2	A 
A A  A 'D ,0D D -CD C;;D __main__zFailed to fetch data.)r0   pandasr7   r4   rN   django.confr   logging	getLogger__name__r5   r   r2   FPL_DATA_FILErR   rG   rY   r_   rC   r?   r    r   r   <module>rg      s       	   			8	$eeee4	G** $N;.( z Dd%& r   