a
    8ñ»j—B  ã                   @   s    d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlZddlZddlmZmZmZ ddlmZmZmZmZmZ ed	 Zd
ZG dd	„ d	eƒZdS )zScraper for http://fotmob.com.é    N)ÚIterable)ÚPath)ÚCallableÚOptionalÚUnioné   )ÚBaseRequestsReaderÚadd_standardized_team_nameÚmake_game_id)ÚDATA_DIRÚNOCACHEÚNOSTOREÚTEAMNAME_REPLACEMENTSÚloggerÚFotMobzhttps://www.fotmob.com/api/c                
       s.  e Zd ZdZdddeeefeee	e
e	 f  eee	eeee	ef  f  eee	e
e	 eg e	f f  eeedœ‡ fdd„Zdeee	e	f  ejdœ‡ fdd„Zee
e	 d	œd
d„ƒZejd	œdd„Zejd	œdd„Zdeejdœdd„Zdeejdœdd„Zde	eeee	e
e	 f  eejdœdd„Z‡  ZS )r   a  Provides pd.DataFrames from data available at http://www.fotmob.com.

    Data will be downloaded as necessary and cached locally in
    ``~/soccerdata/data/FotMob``.

    Parameters
    ----------
    leagues : string or iterable, optional
        IDs of Leagues to include.
    seasons : string, int or list, optional
        Seasons to include. Supports multiple formats.
        Examples: '16-17'; 2016; '2016-17'; [14, 15, 16]
    proxy : 'tor' or or dict or list(dict) or callable, optional
        Use a proxy to hide your IP address. Valid options are:
            - "tor": Uses the Tor network. Tor should be running in
              the background on port 9050.
            - str: The address of the proxy server to use.
            - list(str): A list of proxies to choose from. A different proxy will
              be selected from this list after failed requests, allowing rotating
              proxies.
            - callable: A function that returns a valid proxy. This function will
              be called after failed requests, allowing rotating proxies.
    no_cache : bool
        If True, will not use cached data.
    no_store : bool
        If True, will not store downloaded data.
    data_dir : Path
        Path to directory where data will be cached.
    N)ÚleaguesÚseasonsÚproxyÚno_cacheÚno_storeÚdata_dirc                    sb   t ƒ j|||||d� || _| js^| jd jddd� | jd jddd� | jd jddd� dS )zInitialize the FotMob reader.)r   r   r   r   r   r   T)ÚparentsÚexist_okr   ÚmatchesN)ÚsuperÚ__init__r   r   r   Úmkdir)Úselfr   r   r   r   r   r   ©Ú	__class__© ú7E:\soccer\ml_env\Lib\site-packages\soccerdata/fotmob.pyr   2   s    
ûzFotMob.__init__)ÚheadersÚreturnc                    sL   zt  d¡}| ¡  W n t jjy4   tdƒ‚Y n0 | ¡ }tƒ j|d�S )Nzhttp://46.101.91.154:6006/z/Unable to connect to the session cookie server.)r"   )	Útls_requestsÚgetÚraise_for_statusÚ
exceptionsÚ	HTTPErrorÚConnectionErrorÚjsonr   Ú_init_session)r   r"   ÚrÚresultr   r    r!   r+   I   s    
zFotMob._init_session)r#   c                 C   s   t | j ¡ ƒS )z"Return a list of selected leagues.)ÚlistZ_leagues_dictÚkeys)r   r    r    r!   r   R   s    zFotMob.leaguesc                 C   s  t d }| jd }|  ||¡}t |¡}g }| ¡ D ]˜\}}|dkr„|d d D ]0}| |d d |d |d d	|d
  dœ¡ qPq4|dvr4|D ]:}	|	d D ],}
| |	d |
d |
d d	|
d
  dœ¡ qœq�q4t |¡j	dd„ d� 
| j¡ d¡j| j ¡   ¡ }||j | j¡ S )zqRetrieve the selected leagues from the datasource.

        Returns
        -------
        pd.DataFrame
        Z
allLeagueszallLeagues.jsonÚinternationalr   r   ZccodeÚidÚnameúhttps://fotmob.comÚpageUrl)ÚregionÚ	league_idÚleagueÚurl)Z	favouriteZpopularZuserSettings)r5   r7   r6   r8   c                 S   s   | j d | j S )Nú-)r5   r7   ©Úxr    r    r!   Ú<lambda>{   ó    z%FotMob.read_leagues.<locals>.<lambda>)r7   r7   )Ú
FOTMOB_APIr   r%   r*   ÚloadÚitemsÚappendÚpdÚ	DataFrameÚassignÚpipeZ_translate_leagueÚ	set_indexÚlocZ_selected_leaguesr/   Ú
sort_indexÚindexÚisinr   )r   r8   ÚfilepathÚreaderÚdatar   ÚkÚvZ
int_leagueZcountryZ
dom_leagueÚdfr    r    r!   Úread_leaguesW   sH    



üÿ
üÿ
	
ÿþýüÿzFotMob.read_leaguesc              
   C   sÒ   d}t d }|  ¡ }g }| ¡ D ]v\}}| |j¡}| j| |¡ }|  ||¡}	t |	¡}
|
d }|D ].}| 	|| j
 |¡|j||jd | dœ¡ qfq t |¡ ddg¡ ¡ }|j|j tt | j| j¡ƒ¡ S )zvRetrieve the selected seasons for the selected leagues.

        Returns
        -------
        pd.DataFrame
        zleagues/{}.jsonzleagues?id={}ZallAvailableSeasonsz?season=)r7   Úseasonr6   Ú	season_idr8   r7   rR   )r>   rQ   ÚiterrowsÚformatr6   r   r%   r*   r?   rA   Z_season_codeÚparser8   rB   rC   rF   rH   rG   rI   rJ   r.   Ú	itertoolsÚproductr   r   )r   ÚfilemaskÚurlmaskZ
df_leaguesr   Úlkeyr7   r8   rK   rL   rM   Zavail_seasonsrR   rP   r    r    r!   Úread_seasonsƒ   s*    

ûÿ
zFotMob.read_seasonsF)Úforce_cacher#   c              
   C   s„  d}t d }ddg}g d¢}|  ¡ }g }| ¡ D �]\\}}	}
| j| ||	¡ }| |
j|
j¡}|  ||	¡ }| j|||o~| d�}t	 
|¡}|d d d	 }d
|v �rd|vrº| d¡ |d
 }g }tt|ƒƒD ]4}t || d d ¡}|| d |d< | |¡ qÒtj|dd�}nt |d d ¡}|d jjddd�|ddg< |jddddddddœd�}||d< |	|d< d|d v �rFd|v�r”| d¡ d |d< |d d! }tt|ƒƒD ]�}g }|| d" D ]x}t|ƒ�sÚ�qÈ|d }| |d# ¡ | |d$ ¡ ||j|d%  |¡df< |d&k�rÈ|d' }d(|j|d% |kdf< �qÈ�q´| |¡ q0tj|dd�jd)did� dti¡ |¡ ¡ | S )*a;  Retrieve the league table for the selected leagues.

        Parameters
        ----------
        force_cache : bool
             By default no cached data is used for the current season.
             If True, will force the use of cached data anyway.

        Returns
        -------
        pd.DataFrame
        úseasons/{}_{}.htmlúleagues?id={}&season={}r7   rR   )	ÚteamÚMPÚWÚDÚLÚGFÚGAÚGDÚPts©r   Útabler   rM   ZtablesÚstageÚallZ
leagueName©ZaxisZ	scoresStrr9   T©Úexpandre   rf   r`   ra   rb   rc   rd   rg   rh   )r2   ZplayedZwinsZdrawsZlossesZgoalConDiffZpts©ÚcolumnsZplayoffÚtabsNÚroundsZmatchupsZ
homeTeamIdZ
awayTeamIdr1   ÚfinalÚwinnerZ
cup_winnerZSquad)r>   r\   rT   r   rU   r6   rS   Ú_is_completer%   r*   r?   rA   ÚrangeÚlenrB   Újson_normalizeÚconcatÚstrÚsplitÚrenameÚboolrG   rJ   Úreplacer   rF   rH   )r   r]   rY   rZ   ÚidxÚcolsr   Zmult_tablesr[   ÚskeyrR   rK   r8   Úcurrent_seasonrL   Úseason_dataZ
table_dataZgroups_dataZ
all_groupsÚiZgroup_tableZdf_tableZplayoff_roundsZstage_teamsÚgamerk   ru   r    r    r!   Úread_league_table£   s‚    


ùÿ



ÿþýüÿzFotMob.read_league_tablec              	   C   s@  d}t d }g d¢}|  ¡ }g }| ¡ D ]ˆ\\}}}	| j| ||¡ }
| |	j|	j¡}|  ||¡ }| j||
|ot| d�}t	 
|¡}t |d d ¡}||d< ||d< | |¡ q(t |¡jd	d
ddddddœd� ttdœ¡jdd„ d�}|jtdd�|d< d|d  |d< |d jjddd�|ddg< | g d ¢¡ ¡ | S )!aH  Retrieve the game schedule for the selected leagues and seasons.

        Parameters
        ----------
        force_cache : bool
             By default no cached data is used for the current season.
             If True, will force the use of cached data anyway.

        Returns
        -------
        pd.DataFrame
        r^   r_   )
ÚroundÚweekÚdateÚ	home_teamÚ	away_teamÚ
home_scoreÚ
away_scoreÚstatusÚgame_idr8   ri   ZfixturesZ
allMatchesr7   rR   rˆ   r‰   r‹   rŒ   r�   r8   r�   )Z	roundNamerˆ   z	home.namez	away.namezstatus.reason.shortr4   r1   rp   )r‹   rŒ   c                 S   s   t j| d dd�S )Nzstatus.utcTimeÚmixed)rU   )rB   Úto_datetimer:   r    r    r!   r<   8  r=   z&FotMob.read_schedule.<locals>.<lambda>)rŠ   r   rm   r†   r3   zstatus.scoreStrr9   Trn   r�   rŽ   )r7   rR   r†   )r>   r\   rT   r   rU   r6   rS   rv   r%   r*   r?   rB   ry   rA   rz   r}   r   r   rD   Úapplyr
   r{   r|   rF   rH   )r   r]   rY   rZ   r�   Z
df_seasonsZall_schedulesr[   r‚   rR   rK   r8   rƒ   rL   r„   rP   r    r    r!   Úread_scheduleù   sH    

ùþþóîÿzFotMob.read_scheduleú	Top statsT)Ú	stat_typeÚopponent_statsr`   r]   r#   c              	      sÂ  d}t d }|  |¡}|j|d  g d¢¡ }|durrt|ƒ}	|j|j |	¡|j |	¡B  }
t|
ƒdkrŠtdƒ‚n|}
|
j 	¡ |
j 	¡  }	g }|
 
¡  ¡ D �]š\‰}|d |d	 |d
   }}}| |j¡}| j| |||j¡ }|  ||¡}t dˆd t|
ƒ|d ¡ t |¡}|d d d d d }zt‡fdd„|D ƒƒ}W n" t�yh   tdˆ› �ƒ‚Y n0 t |d ¡}|j|jg}t|ƒD ]¨\‰}| ¡ }|d  ‡fdd„¡|d< ||d< ||d	< ||d
< ||d< |�sì||j |	¡ }|jg d¢dd� ||d dk }|jddd� 
¡ }d|j_|  |¡ �qŒqštj!|dd�‰ ˆ  g d¢¡ "¡ ‰ ‡ fd d!„ˆ jD ƒ}|D ]L}ˆ | j#j$dd"�ˆ ||d# g< ˆ |d#  j# %d$¡ &t'¡ (d%¡ˆ |d# < �qpˆ S )&a#  Retrieve the match stats for the selected leagues and seasons.

        The following stat types are available:
            * 'Top stats'
            * 'Shots'
            * 'Expected goals (xG)'
            * 'Passes'
            * 'Defence'
            * 'Duels'
            * 'Discipline'

        Parameters
        ----------
        stat_type : str
            Type of stats to retrieve.
        opponent_stats: bool
            If True, will retrieve opponent stats.
        team: str or list of str, optional
            Team(s) to retrieve. If None, will retrieve all teams.
        force_cache : bool
            By default no cached data is used to scrape the list of available
            games for the current season. If True, will force the use of
            cached data anyway.

        Raises
        ------
        TypeError
            If ``stat_type`` is not valid.
        ValueError
            If no games with the given IDs were found for the selected seasons and leagues.

        Returns
        -------
        pd.DataFrame
        zmatches/{}_{}_{}.htmlzmatchDetails?matchId={}r�   )ZFTZAETZPenNr   z:No data found for the given teams in the selected seasons.r7   rR   r†   z"[%s/%s] Retrieving game with id=%sr   r�   ÚcontentÚstatsZPeriodsZAllc                 3   s   | ]}|d  ˆ kr|V  qdS )ÚtitleNr    )Ú.0Ústat)r–   r    r!   Ú	<genexpr>“  r=   z/FotMob.read_team_match_stats.<locals>.<genexpr>zInvalid stat type: c                    s   | ˆ  S )Nr    r:   )r…   r    r!   r<   ›  r=   z.FotMob.read_team_match_stats.<locals>.<lambda>rœ   r`   )r7   rR   r†   r`   T)ZinplaceÚtyperš   )rq   Úvaluesrm   c                    s*   g | ]"}ˆ |   t¡j d ¡ ¡ r|‘qS )ú%)Úastyper{   ÚcontainsÚany)r›   Úcol)rP   r    r!   Ú
<listcomp>«  r=   z0FotMob.read_team_match_stats.<locals>.<listcomp>rn   z (%)z(\d+)éd   ))r>   r”   rG   rJ   r	   r‹   rŒ   rx   Ú
ValueErrorÚtolistZreset_indexrT   rU   r�   r   r%   r   Úinfor*   r?   ÚnextÚStopIterationrB   rC   Ú	enumerateÚcopyr“   r`   rF   Zpivotrq   r2   rA   rz   rH   r{   r|   Úextractr¡   ÚfloatÚdiv)r   r–   r—   r`   r]   rY   rZ   Z
df_matchesZdf_completeZteams_to_checkÚiteratorr™   r†   r[   r‚   Zgkeyr8   rK   rL   Z	game_dataZ	all_statsZselected_statsZdf_raw_statsZ
game_teamsZdf_team_statsZpct_colsr¤   r    )rP   r…   r–   r!   Úread_team_match_stats?  sp    *


ÿþ
ü
,zFotMob.read_team_match_stats)N)F)F)r•   TNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚFOTMOB_DATADIRr   r   r{   r.   Úintr   r   r~   r   r   Údictr$   ZClientr+   Úpropertyr   rB   rC   rQ   r\   r‡   r”   r²   Ú__classcell__r    r    r   r!   r      sB    ùù$	, VH    ûú)r¶   rW   r*   Úcollections.abcr   Úpathlibr   Útypingr   r   r   ZpandasrB   r$   Ú_commonr   r	   r
   Ú_configr   r   r   r   r   r·   r>   r   r    r    r    r!   Ú<module>   s   