-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteampy.py
324 lines (282 loc) · 11.2 KB
/
steampy.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import requests as rq
import time
import asyncio
import aiohttp
import aiofiles
from os.path import exists
import json
import glob
import os, sys
from PIL import Image
from datetime import datetime as dt
import logging
from operator import itemgetter
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
steam_url = "https://api.steampowered.com/ISteamUserStats"
config_file = resource_path("assets\\config\\steam.json")
broken_file = resource_path("assets\\images\\broken.jpg")
colour_dir = resource_path("assets\\images\\colour\\")
grey_dir = resource_path("assets\\images\\grey\\")
latest_image = resource_path("assets\\images\\achievement_icon.jpg")
col_images_cache = []
gry_images_cache = []
logging.basicConfig(
# filename=resource_path("runtime.log"),
# filemode="w",
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%d-%b-%y %H:%M:%S",
)
log = logging.getLogger("steampy.py")
class Steam:
def __init__(self):
self.config = self.get_creds()
def get_creds(self):
if exists(config_file):
log.info("Configuration file opened")
with open(config_file, "r+") as openfile:
return json.load(openfile)
else:
log.warn("File Doesn't Exist, creating it with default data")
log.info("Call configure_creds(creds)")
self.configure_creds(
{
"appid": "GAME_ID",
"steamid": "USER_ID",
"key": "API_KEY",
}
)
def get_completion_stats(self):
response = rq.get(
"{}/GetGlobalAchievementPercentagesForApp/v0002/".format(steam_url),
params={"gameid": self.config["appid"], "format": "json"},
)
if response.status_code == 200:
object = response.json()
percentages = {}
for i in object["achievementpercentages"]["achievements"]:
percentages[i["name"]] = i["percent"]
return percentages
else:
log.warn(
f"Failed to Load get_completion_stats, Response code {response.status_code}"
)
return {"percentages": ["Failed to Load"]}
def configure_creds(self, creds):
data = json.dumps(creds, indent=4)
with open(config_file, "w+") as outfile:
outfile.write(data)
log.info("Configuration file Updated by user")
def get_achievements(self):
response = rq.get(
"{}/GetPlayerAchievements/v1/".format(steam_url),
params=self.config,
)
if response.status_code == 200:
log.info("Successful API Call for get_achievements")
response = response.json()
achievements = response["playerstats"]["achievements"]
total_game_achievements = len(achievements)
return {
"total": total_game_achievements,
"achievements": achievements,
"gamename": response["playerstats"]["gameName"],
}
else:
log.warn(
f"Unsuccessful API Call for get_achievements, Response code {response.status_code}"
)
return {
"total": 0,
"achievements": ["Failed to Load"],
"gamename": "None",
}
def get_details(self):
response = rq.get(
"{}/GetSchemaForGame/v2/".format(steam_url), params=self.config
)
if response.status_code == 200:
response = response.json()
if len(response["game"]) > 1:
achievements = response["game"]["availableGameStats"]["achievements"]
return {"achievement_details": achievements}
else:
log.warn(
f"Unsuccessful API Call for get_details, Response code {response.status_code}"
)
return {"achievement_details": ["API Failure"]}
else:
log.warn(
f"Unsuccessful API Call for get_details, Response code {response.status_code}"
)
return {
"achievement_details": [{"name": "API Failure", "displayName": "None"}]
}
def get_latest(self, achievements):
earned = 0
last_earned = None
achievement_time = 0
for achievement in achievements:
if achievement["achieved"] == 1:
earned += 1
achievement_time = achievement["unlocktime"]
if last_earned is None or achievement_time > last_earned["unlocktime"]:
last_earned = achievement
if earned == 0:
log.warning("No Achievements for this Game")
return {
"num_earned": earned,
"latest": {"apiname": "None Earned Yet!", "name": "None Earned Yet!", "icon": "broken.jpg"},
}
return {"num_earned": earned, "latest": last_earned}
def get_latest_details(self, last_earned):
latest_achievement_earned = None
details = self.get_details()["achievement_details"]
for achievement in details:
if achievement["name"] == last_earned:
latest_achievement_earned = achievement
if latest_achievement_earned is None:
file = broken_file
latest_achievement_earned = {"displayName": last_earned}
else:
icon_url = latest_achievement_earned["icon"]
icon_hash = icon_url[icon_url.rfind("/") :]
file = "{}{}".format(colour_dir, icon_hash.replace("/", ""))
img = Image.open(file)
img.save(latest_image)
log.info("Latest Achievement Image Updated")
return latest_achievement_earned["displayName"]
def download_images(self, urls, folder):
start = time.time()
asyncio.run(self.bulk_request(urls, folder))
log.info("Got Images in {} s".format(time.time() - start))
def clear_images(self, folder):
files = glob.glob(folder + "\*")
for f in files:
if ".jpg" in f:
os.remove(f)
log.info(f"Images Removed from {folder}")
async def bulk_request(self, urls, folder):
async with aiohttp.ClientSession() as session:
tasks = []
for i in urls:
tasks.append(
self.get_store_image(session=session, url=i, folder=folder)
)
await asyncio.gather(*tasks)
async def get_store_image(self, session, url, folder):
try:
resp = await session.request(method="GET", url=url)
except Exception as ex:
print(ex)
log.error(f"Unsuccessful function for get_store_image")
return
if resp.status == 200:
image_name = url[url.rfind("/") :]
path = f"{folder}{image_name.replace('/', '')}"
async with aiofiles.open(path, "wb") as f:
await f.write(await resp.read())
def get_games_list(self):
game_response = rq.get(
"https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/",
params={
"key": self.config["key"],
"steamid": self.config["steamid"],
"format": "json",
"include_appinfo": "true",
"include_played_free_games": "true",
},
)
achievable_list = []
urls = []
if game_response.status_code == 200:
game_list = game_response.json()["response"]["games"]
for i in game_list:
if not (i.get("has_community_visible_stats") is None):
total_minutes = i["playtime_forever"]
time_string = "{} hours and {} minutes".format(
*divmod(total_minutes, 60)
)
achievable_list.append(
{
"appid": str(i["appid"]),
"name": i["name"],
"img_logo_url": i["img_icon_url"],
"playtime_forever": total_minutes,
"time_played": time_string,
}
)
urls.append(
f"http://media.steampowered.com/steamcommunity/public/images/apps/{i['appid']}/{i['img_icon_url']}.jpg"
)
asyncio.run(
self.bulk_request(
urls=urls, folder=resource_path("assets\\games\\icons\\")
)
)
achievable_list.sort(key=itemgetter("name"), reverse=False)
return list(achievable_list)
def check_images(self):
items = [f for f in os.listdir(colour_dir)]
grey = [f for f in os.listdir(grey_dir)]
items.extend(grey)
return items
def combine(self):
color = []
gray = []
details = self.get_details()
compiled_achievement_list = []
achievements = self.get_achievements()
percentages = self.get_completion_stats()
latest = self.get_latest(achievements["achievements"])
for dtl, ach in zip(
details["achievement_details"], achievements["achievements"]
):
if dtl["name"] == ach["apiname"]:
cmbnd = dtl | ach
cmbnd.pop("apiname")
cmbnd["percentage"] = percentages[cmbnd["name"]]
color.append(cmbnd["icon"])
gray.append(cmbnd["icongray"])
compiled_achievement_list.append(cmbnd)
if dtl["name"] == latest["latest"]["apiname"]:
latest["latest"]["name"] = dtl["displayName"]
# IMAGES REFRESH
local_imgs = self.check_images()
for url in [s for s in color if any(xs in s for xs in local_imgs)]:
color.remove(url)
for url in [s for s in gray if any(xs in s for xs in local_imgs)]:
gray.remove(url)
if len(color) > 1:
log.warning(
"COLOUR: Local and Remote Image Count is different, re-downloading"
)
self.clear_images(colour_dir)
self.download_images(color, colour_dir)
elif len(gray) > 1:
log.warning(
"GREY: Local and Remote Image Count is different, re-downloading"
)
self.clear_images(grey_dir)
self.download_images(gray, grey_dir)
else:
log.info("Number of images match, not touching them")
self.get_latest_details(latest["latest"]["apiname"])
data = {
"total": achievements["total"],
"gamename": achievements["gamename"],
"achievements": compiled_achievement_list,
"latest": latest,
"refresh_time": dt.now().strftime("%Y-%m-%d_%H:%M:%S"),
}
out = json.dumps(data, indent=4)
with open("output.json", "w+") as outfile:
outfile.write(out)
return data