-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_user_properties_copy.py
131 lines (114 loc) · 4.22 KB
/
query_user_properties_copy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import json
import os
from chesscom.game_gateway.v1.game_gateway_service_pb2 import (
Criteria,
HydrateGamesByCriteriaRequest,
SortDirection,
SortField,
)
from chesscom.game_gateway.v1.game_gateway_service_twirp import GameGatewayServiceClient
from chesscom.game_storage.v1.game_stored_pb2 import TimeClass, Variant
from chesscom.user_properties.v1.user_properties_service_pb2 import (
SearchUserPropertiesRequest,
SearchUserPropertiesResponse,
UserProperties,
UserPropertiesPropertiesData,
)
from chesscom.user_properties.v1.user_properties_service_twirp import (
UserPropertiesServiceClient,
)
from chesscom.user_targeting.v1.user_targeting_pb2 import (
BinaryOperator,
Criterion,
LogicalOperator,
)
# Inject API keys
from dotenv import load_dotenv
from twirp.context import Context
from twirp.exceptions import TwirpServerException
load_dotenv()
agi_client = GameGatewayServiceClient(
"https://services.nex.va-prod-01.chess-platform.com"
)
user_properties_client = UserPropertiesServiceClient("https://prod.chess-platform.com")
RATING_MIN = 1000
RATING_MAX = 2000
PLAYER_PER_BUCKET = 200
GAME_PER_PLAYER = 100
def fetch_players_per_bucket(rating_min: int, rating_max: int):
try:
try:
user_properties_api_key = os.environ["USER_PROPERTIES_API_KEY"]
except KeyError:
raise RuntimeError(
"USER_PROPERTIES_API_KEY environment variable is not set yet. To get the API key in Vault, "
"go to chess-prod > platform > user-properties"
)
response = user_properties_client.SearchUserProperties(
ctx=Context(),
request=SearchUserPropertiesRequest(
criteria=[
Criterion(
user_property="username",
binary_operator=BinaryOperator.EQUALS,
value="MangosCarloson",
),
],
# properties = ["username", 'player_id'],
logical_operator=LogicalOperator.AND,
limit=PLAYER_PER_BUCKET,
),
headers={"X-Api-Key": user_properties_api_key},
server_path_prefix="/service/user-properties",
)
print(response)
print(type(response))
# output = list(response.user_properties)
# print(output[0])
return [
sample.properties["username"].value for sample in response.user_properties
]
except TwirpServerException as e:
print(e.code, e.message, e.meta, e.to_dict())
def fetch_games_per_player(player_id: str):
try:
try:
agi_api_key = os.environ["AGI_API_KEY"]
except KeyError:
raise RuntimeError(
"AGI_API_KEY environment variable is not set yet. To get the API key in Vault, "
"go to cluster-chess-prod-va-01 > platform > foundation > game-archive"
)
response = agi_client.HydrateGamesByCriteria(
ctx=Context(),
request=HydrateGamesByCriteriaRequest(
criteria=Criteria(
player_id=player_id,
time_classes=[TimeClass.TIME_CLASS_BLITZ],
# ply_from = 2,
rated=True,
variants=[Variant.VARIANT_CHESS],
sort_fields=[SortField.SORT_FIELD_GAME_END_TIME],
sort_direction=SortDirection.SORT_DIRECTION_DESC,
page=1,
page_size=GAME_PER_PLAYER,
),
field_mask={"paths": ["eco_metadata"]},
),
headers={"X-Api-Key": agi_api_key},
server_path_prefix="/service/player-game-archive",
)
return list(response.hydrated_games)
except TwirpServerException as e:
print(e.code, e.message, e.meta, e.to_dict())
players = fetch_players_per_bucket(RATING_MIN, RATING_MAX)
print(players)
for player in players:
games = fetch_games_per_player(player)
print(len(games), player)
if games:
print(games[0])
exit()
file_name = "users_" + str(RATING_MIN) + "_" + str(RATING_MAX) + ".json"
with open(file_name, "w") as file:
json.dump(players, file, indent=4)