player_tools.cpython-311.pyc•11.2 kB
�
߭[ho �
�V � d Z ddlmZmZmZ ddlmZmZ ddlm Z ddl
Z
ddlZ ej e
� � Zdedeeef fd�Zdedeeef fd �Zdedeeef fd
�Zdedeeef fd�Zdeded
edeeef fd�Zdedeeee f fd�Zdeded
edefd�ZdS )z!
Chess.com player-related tools.
� )�Dict�Any�List)�make_api_request�
get_cache_ttl)�dbN�username�returnc � � K � d| � �}t d� � } t ||�� � � d{V ��} t j d| |� dd� � |� dd � � d
�� � n4# t
$ r'}t � d|� �� � Y d}~nd}~ww xY w|S # t
$ r}dd
t |� � � �icY d}~S d}~ww xY w)z�
Get a player's profile information from Chess.com.
Args:
username: The Chess.com username
Returns:
Player profile data including join date, status, followers, etc.
�player/�player_profile�� cache_ttlNz�
INSERT INTO player_profile_views (username, viewed_at, follower_count, country)
VALUES ($username, CURRENT_TIMESTAMP, $followers, $country)
� followersr �country� )r r r zCould not store analytics: �errorz Failed to fetch player profile: ) r r r �execute�get� Exception�logger�debug�str�r �endpointr �data�es �,/home/yori/chess-mxcp/python/player_tools.py�get_player_profiler
s? � � � � $��#�#�H��.�/�/�I�F�%�h�)�D�D�D�D�D�D�D�D�D��
<��J� � %�!�X�X�k�1�5�5��8�8�I�r�2�2�� �
�
�
�
�� � <� <� <��L�L�:�q�:�:�;�;�;�;�;�;�;�;����� <���� ���� F� F� F��D�C��F�F�D�D�E�E�E�E�E�E�E�����F���sG �B( �AA2 �1B( �2
B#�<B�B( �B#�#B( �(
C�2C�C�Cc � � K � d| � d�}t d� � } t ||�� � � d{V ��}|� � � D ]�\ }}t |t � � r�d|v r�|d }t j d| ||� dd � � |� d
i � � � dd � � |� d
i � � � dd � � z |� d
i � � � d
d � � z d�� � ��|S # t $ r}ddt |� � � �icY d}~S d}~ww xY w)z�
Get a player's chess statistics from Chess.com.
Args:
username: The Chess.com username
Returns:
Player statistics for different game types (chess_blitz, chess_rapid, etc.)
r z/stats�player_statsr N�lastz�
INSERT INTO rating_history
(username, game_type, rating, games_played, recorded_at)
VALUES ($username, $game_type, $rating, $games, CURRENT_TIMESTAMP)
�ratingr �record�win�loss�draw)r � game_typer# �gamesr zFailed to fetch player stats: )
r r �items�
isinstance�dictr r r r r )r r r r r( �stats�rating_datar s r �get_player_statsr/ / s� � � � � *��)�)�)�H��n�-�-�I�D�%�h�)�D�D�D�D�D�D�D�D�D�� !%�
�
��� � ��I�u��%��&�&�
�6�U�?�?�#�F�m���
� �
!)�!*�)�o�o�h��:�:�"�Y�Y�x��4�4�8�8���B�B�!�I�I�h��3�3�7�7���B�B�C�!�I�I�h��3�3�7�7���B�B�C� � � � � �� ���� D� D� D��B�#�a�&�&�B�B�C�C�C�C�C�C�C�����D���s �C?D �
E�#D<�6E�<Ec � � K � d| � d�}t d� � } t ||�� � � d{V ��}|S # t $ r}dt |� � d�cY d}~S d}~ww xY w)z�
Check if a player is currently online on Chess.com.
Args:
username: The Chess.com username
Returns:
Dictionary with 'online' boolean value
r z
/is-online�
current_gamesr NF)�onliner �r r r r r s r �is_player_onliner4 U s� � � � � .��-�-�-�H��o�.�.�I�2�%�h�)�D�D�D�D�D�D�D�D�D������ 2� 2� 2��#�a�&�&�1�1�1�1�1�1�1�1�����2���� �2 �
A�A�
A�Ac � �, K � d| � d�}t d� � } t ||�� � � d{V ��}t |� dg � � � � }t j d| |d�� � |S # t $ r}g t |� � d �cY d}~S d}~ww xY w)
z�
Get a list of a player's current games on Chess.com.
Args:
username: The Chess.com username
Returns:
List of ongoing games
r z/gamesr1 r Nr) z�
INSERT INTO player_activity (username, game_count, checked_at)
VALUES ($username, $count, CURRENT_TIMESTAMP)
)r �count�r) r )r r �lenr r r r r )r r r r �
game_countr s r �get_player_current_gamesr; i s� � � � � *��)�)�)�H��o�.�.�I�.�%�h�)�D�D�D�D�D�D�D�D�D�� ����'�2�.�.�/�/�
�
�
� � #�Z�
8�
8� :� :� :�
���� .� .� .��c�!�f�f�-�-�-�-�-�-�-�-�����.���s �AA- �-
B�7B�B�B�year�monthc
� � K � t |� � � d� � }d| � d|� d|� �}t j d| ||d�� � }|rd� |D � � }d|iS t |d �
� � � d {V ��}|� dg � � }|D ]�}|� di � � � dd
� � � d|� di � � � dd
� � � d|� dd
� � � �} t j d| | |||d�� � ��|S # t $ r}
g t |
� � d�cY d }
~
S d }
~
ww xY w)z�
Get a player's games for a specific month from Chess.com.
Args:
username: The Chess.com username
year: Year (YYYY format)
month: Month (1-12)
Returns:
List of games for the specified month
� r �/games/�/z�
SELECT game_data
FROM chess_games_cache
WHERE username = $username
AND year = $year
AND month = $month
ORDER BY cached_at DESC
�r r<