-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetData.py
172 lines (152 loc) · 6.84 KB
/
getData.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import requests
import json
import pandas as pd
import numpy as np
from datetime import datetime
from influxdb_client import InfluxDBClient, Point, WriteOptions
def getServerStats(filter):
steamAPIKey = ""
with open("steamapitoken.txt") as f:
steamAPIKey = f.readline().strip()
endpoint = "https://api.steampowered.com/IGameServersService/GetServerList/v1/"
params = {
"key":steamAPIKey,
"limit": 20000,
"filter": filter
}
response = requests.get(endpoint,params)
if response.status_code != 200:
print(f"Failed to request data: {response.text}")
return None
responseJSON = json.loads(response.text)
if "response" not in responseJSON:
print(f"Failed to request data: {response.text}")
return None
if "servers" not in responseJSON["response"]:
print(f"Failed to request data: {response.text}")
return None
return responseJSON["response"]["servers"]
def getPlayerNumbers():
baseFilter = "\\appid\\730\\white\\1\\empty\\1"
otherMapFilter = "\\nand\\1\\map\\de_dust2,de_mirage,de_inferno"
dust2 = "\\map\\de_dust2"
mirage = "\\map\\de_mirage"
inferno = "\\map\\de_inferno"
dust2Stats = getServerStats(baseFilter+dust2)
mirageStats = getServerStats(baseFilter+mirage)
infernoStats = getServerStats(baseFilter+inferno)
othermapStats = getServerStats(baseFilter+otherMapFilter)
mapStats = [dust2Stats,mirageStats,infernoStats,othermapStats]
combinedStats = []
for stat in mapStats:
if stat is None:
continue
if len(combinedStats) == 0:
combinedStats = stat
else:
combinedStats += stat
mapStats = pd.DataFrame(combinedStats)
maps = mapStats[mapStats["map"].notna()]["map"].unique()
maps.sort()
region_us = [1,2,22,23,27]
region_eu = [3,8,9,21,28,44,45]
region_sa = [10,14,15,38]
region_asia = [5,6,16,19,24,26,39]
region_china = [12,17,25,42,46,49,51]
region_other = [11,7]
all_regions = region_us+region_eu+region_sa+region_asia+region_china+region_other
region_dict = {
"north_america":region_us,
"europe": region_eu,
"south_america": region_sa,
"other": region_other,
"asia": region_asia,
"china": region_china
}
data = []
for map in maps:
current = mapStats[mapStats["map"]==map]
cur_players = current["players"].unique()
cur_players.sort()
for cur_player in cur_players:
currentP = current[current["players"]==cur_player]
maxplayers = currentP["max_players"].unique()
maxplayers.sort()
maxplayers_string = ""
for maxplayer in maxplayers:
maxplayers_string+=str(maxplayer)+";"
regions = currentP["region"].unique()
regions.sort()
for region in regions:
players = currentP[currentP["region"]==region]["players"].sum()
if players == 0:
continue
regionname = "other"
for k,v in region_dict.items():
if region in v:
regionname = k
break
data.append([map, regionname, region, cur_player, players, maxplayers_string])
#max_players = current["max_players"].unique()
#max_players.sort()
#for max_player in max_players:
# currentMP = current[current["max_players"]==max_player]
# regions = currentMP["region"].unique()
# regions.sort()
# for k, v in region_dict.items():
# players = currentMP[currentMP["region"].isin(v)]["players"].sum()
# if players == 0:
# continue
# data.append([map, k, max_player,players])
# otherplayers = currentMP[~currentMP["region"].isin(all_regions)]["players"].sum()
# if otherplayers != 0:
# data.append([map, "other", max_player,players])
playerStatistics = pd.DataFrame(columns=["map", "region_name", "region_id", "players_per_server","players", "max_players_string"],data=data)
playerStatistics = playerStatistics.set_index(pd.DatetimeIndex(np.full(playerStatistics.index.size, datetime.utcnow())))
return playerStatistics
def insertIntoDB(playerStatistics):
credentials = []
with open("influxdb_cred.json") as f:
credentials = json.load(f)
token = credentials["token"]
org = credentials["org"]
bucket = credentials["bucket"]
url = credentials["url"]
with InfluxDBClient(url=url, token=token, org=org) as _client:
with _client.write_api(write_options=WriteOptions(batch_size=500,
flush_interval=10_000,
jitter_interval=2_000,
retry_interval=5_000,
max_retries=5,
max_retry_delay=30_000,
exponential_base=2)) as _write_client:
_write_client.write(bucket, org, record=playerStatistics, data_frame_measurement_name='player_count_v2',
data_frame_tag_columns=['map','players_per_server','region_name', 'region_id'])
def insertTotalsIntoDB(playerStatistics):
credentials = []
with open("influxdb_cred.json") as f:
credentials = json.load(f)
token = credentials["token"]
org = credentials["org"]
bucket = credentials["bucket"]
url = credentials["url"]
with InfluxDBClient(url=url, token=token, org=org) as _client:
with _client.write_api(write_options=WriteOptions(batch_size=500,
flush_interval=10_000,
jitter_interval=2_000,
retry_interval=5_000,
max_retries=5,
max_retry_delay=30_000,
exponential_base=2)) as _write_client:
_write_client.write(bucket, org, record=playerStatistics, data_frame_measurement_name='player_count_totals',
data_frame_tag_columns=['region'])
def main():
statisticsDF = getPlayerNumbers()
insertIntoDB(statisticsDF)
df = statisticsDF[["region_name", "players"]].groupby("region_name").sum()
df.reset_index(inplace=True)
df = df.set_index(pd.DatetimeIndex(np.full(df.index.size, datetime.utcnow())))
df.rename(columns={'region_name': 'region'}, inplace=True)
insertTotalsIntoDB(df)
if __name__ == "__main__":
main()