-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
446 lines (358 loc) · 14.9 KB
/
utils.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import config
import winreg
import platform
import vdf
import requests
import os
import subprocess
import crc_algorithms
import re
import colorama
import urllib
import math
from PIL import Image, ImageFilter, ImageEnhance, ImageFont, ImageDraw
from difflib import SequenceMatcher
def get_request(url):
"""
Sends a GET request and returns the result as JSON
Parameters:
url - URL to send the GET request to
"""
r = requests.get(url)
return r.json()
def get_id_by_username(username):
"""
Gets a Steam ID by the username
Returns ID or None if could not retrieve.
Parameters:
username - Steam username
"""
result = get_request("http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=" + config.STEAM_API_KEY + "&vanityurl=" + username)
id = None
try:
if result["response"]["success"] == 1:
id = result["response"]["steamid"]
else:
raise Exception
except:
print("An error occured while trying to get your Steam ID.")
return None
return id
def get_libraries(install_dir, including_install=True):
"""
Gets the steam libary locations installed on the system
and returns them as a list
Parameters:
install_dir - Steam installation directory
including_install - Whether to include the installation directory as a library or not.
Defaults to True.
"""
# Parse the vdf file into JSON
try:
f = vdf.parse(open(install_dir + "\\steamapps\\libraryfolders.vdf"))
except:
print("Could not find libraryfoldes.vdf")
return
libraries = []
# Include the installation directory as a library, if wanted
if including_install:
libraries.append(install_dir)
# Loop through the LibraryFolders entry
for i in range(0, len(f["LibraryFolders"])):
# Skip the first and second entries
if i == 0 or i == 1:
continue
libraries.append(f["LibraryFolders"][str(i - 1)])
return libraries
def get_steam_install_path():
"""
Gets the steam installation path on the system
by looking in the registry.
Returns:
The path, or None if it could not find the installatino
"""
try:
# Set the key path according to the architecture - 32/64 bits
key_path = "SOFTWARE\\WOW6432Node\\Valve\\Steam"
if not is_64():
key_path = "SOFTWARE\\Valve\\Steam"
# Open the registry key and query the value of InstallPath
reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
k = winreg.OpenKey(reg, key_path)
value = winreg.QueryValueEx(k, "InstallPath")
winreg.CloseKey(k)
return value[0]
except Exception as e:
print("Could not find Steam installation directory.")
print(e)
return None
def get_steam_users():
"""
Gets the users ID's on Steam by checking in the
Steam installation directory under the "userdata" folder
Returns:
A list of the user ID's on Steam
"""
return os.listdir(get_steam_install_path() + "\\userdata")
def is_64():
"""
Checks if the computer running the program is 64bits.
If so, returns True. Otherwise, returns False.
"""
return platform.machine().endswith("64")
def get_installed_games():
"""
Finds Steam games installed on the system,
by searching each Steam library.
Returns:
A list of dictionary entries of games installed on the system.
For example:
{
id: 228980
name: Steamworks Common Redistributables
}
"""
games = []
for library in get_libraries(get_steam_install_path()):
# Add \steamapps to the library folder
library += "\\steamapps\\"
# Search for manifest files (.acf)
files = os.listdir(library)
for file in files:
if file.endswith("acf"):
# Get the game details from the manifest file
f = vdf.parse(open(library + file))
appid = f["AppState"]["appid"]
name = f["AppState"]["name"]
games.append({"name": name, "appid": appid})
return games
def launch_steam_game(id):
"""
Launches a Steam game by its ID
Paramteres:
id - Steam game ID
"""
path = get_steam_install_path() + "\\Steam.exe"
subprocess.call(path + " -applaunch " + id)
def generate_appid_for_nonsteam_game(name, target):
"""
(Thanks to github.com/scottrice)
Generates the app ID for a Non-Steam game.
This ID is a 64bit integer, where the first 32bits are
a CRC32 based off of the name and target (with the added
condition that the first bit is always high), and
the last 32bits are 0x02000000.
Paramteters:
name - Game name
target - Exe file location
Returns:
The app ID as a string
"""
algorithm = crc_algorithms.Crc(width = 32, poly = 0x04C11DB7, reflect_in = True, xor_in = 0xffffffff, reflect_out = True, xor_out = 0xffffffff)
input_string = ''.join([target,name])
top_32 = algorithm.bit_by_bit(input_string) | 0x80000000
full_64 = (top_32 << 32) | 0x02000000
return str(full_64)
def get_non_steam_games():
games_list = []
# Go through every user on Steam
for user in get_steam_users():
# Open the shortcuts.vdf file that contains a list of Non-Steam games
try:
f = open(get_steam_install_path() + r"\\userdata\\" + user + "\\config\\shortcuts.vdf", "rb")
except:
print("Could not find shortcuts.vdf for user " + user)
continue
allbytes = []
try:
byte = f.read(1)
while byte != b'':
allbytes.append(byte)
byte = f.read(1)
finally:
f.close()
# Decode the bytes to ASCII
decoded = ""
for b in allbytes:
decoded += b.decode('ascii')
# To use for separating elements
b01 = bytearray.fromhex('01').decode()
b02 = bytearray.fromhex('02').decode()
# Find iternations of "AppName" for each game
# TODO: Sometimes it's "appname" and not "AppName", need to normalize it
# to search for both
iters = []
for iter in re.finditer("appname", decoded):
iters.append({'start': iter.start(), 'end': iter.end()})
# Iterate over the AppNames to make a list of the games
# (every game has one AppName)
iters_length = len(iters)
games = []
i = 0
for iter in iters:
if i + 1 < iters_length:
# If there is another game on the file, cut from this AppName's end to the
# next game's AppName's start
games.append(decoded[iter['start']:iters[i + 1]['start']])
else:
# If this is the last game on the file, cut from this AppName's end to
# the end of the file
games.append(decoded[iter['start']:])
i += 1
# Make a list of games, in the right format, by going
# through the games and getting each game's details.
# For example:
# {
# "name": "Minecraft",
# "exe": "C:\Games\Minecraft.exe"
# }
for game in games:
"""
The contents of the game info can contain some of the following:
AppName Overwatch ☺Exe "D:\Program Files\Overwatch\Overwatch Launcher.exe" ☺StartDir "D:\Program Files\Overwatch\" ☺icon
☺ShortcutPath ☺LaunchOptions ☻IsHidden ☻AllowDesktopConfig ☺ ☻AllowOverlay ☺ ☻OpenVR ☻Devkit ☺DevkitGameID
☻LastPlayTime tags
"""
indices = {}
indices["AppName"] = (0, len("AppName") + 1)
indices["Exe"] = (game.index(b01 + "Exe"), game.index(b01 + "Exe") + len(b01 + "Exe") + 1)
indices["StartDir"] = (game.index(b01 + "StartDir"), game.index(b01 + "StartDir") + len(b01 + "StartDir") + 1)
# Get the contents of the indices (the app name, exe, etc.)
name = game[indices["AppName"][1]:indices["Exe"][0]].rstrip('\x00')
exe = game[indices["Exe"][1]:indices["StartDir"][0]].rstrip('\x00')
games_list.append({
"name": name,
"exe": exe,
"appid": generate_appid_for_nonsteam_game(name, exe),
"user": user
})
return games_list
def create_grid_image(game, file_name, with_text=False):
"""
Creates a grid image for a game by looking for a big image
on IGDB, and then manipulating it to look good as a grid image
Paramaters:
game - A dictionary containing the Non-Steam game information
file_name - A string telling the function where to save the image to
"""
headers = {"user-key": config.IGDB_API_KEY}
# Search IGDB for the game ID by providing the game name
content = "search \"{}\"; fields artworks,cover,slug;".format(game["name"])
try:
r = requests.post("https://api-v3.igdb.com/games/", headers=headers, data=content).json()
except:
print("{}[X]{} Could not find {} on IGDB.".format(colorama.Back.RED, colorama.Style.RESET_ALL, game["name"]))
return
if not r:
print("{}[X]{} Could not find {} on IGDB.".format(colorama.Back.RED, colorama.Style.RESET_ALL, game["name"]))
return
# Save the game's slug to use as text for the grid image
slug = r[0]["slug"]
# Set the cover image url from IGDB
cover = r[0]["cover"]
content = "fields *; where id={};".format(cover)
try:
r = requests.post("https://api-v3.igdb.com/covers", headers=headers, data=content).json()
except:
print("{}[X]{} Could not find a cover image for {} on IGDB.".format(colorama.Back.RED, colorama.Style.RESET_ALL, game["name"]))
return False
if not r:
print("{}[X]{} Could not find a cover image for {} on IGDB.".format(colorama.Back.RED, colorama.Style.RESET_ALL, game["name"]))
return False
# Fix the cover image URL and set the link to the "screenshot_huge" template
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"
cover_url = "http://" + r[0]["url"].replace("//", "").replace("t_thumb", "t_1080p")
temp_location = "{}.temp.png".format(file_name, game["name"])
# Set urllib with the user agent to download the image to the temporary location
opener = urllib.request.build_opener()
opener.addheaders = [("User-Agent", user_agent)]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(cover_url, temp_location)
# Open the temporary image to edit it
im = Image.open(temp_location)
width, height = im.size
# If the user wants to have the game's name of the image
if with_text == True:
# Blur the image
im = im.filter(ImageFilter.GaussianBlur(radius=5))
# Darken the image
im = ImageEnhance.Brightness(im).enhance(0.8)
# Draw the game's name on the image
name_text = game["name"]
# Make new lines if the name is too long
# NOTE: Currently only makes one new line if needed
if slug != "":
name_text = slug.replace('-', ' ')
if len(name_text) > 15:
space = name_text.find(' ', 15)
if space != '':
name_text = replace_str_index(name_text, space, '\n')
font = ImageFont.truetype("grid-font.ttf", 120)
# Position the game's name (Align horizontally)
text_width, text_height = ImageDraw.Draw(im).textsize(name_text, font=font)
ImageDraw.Draw(im).text(((width - text_width) / 2, 100), name_text, font=font, fill=(0, 0, 0, 100))
ImageDraw.Draw(im).text(((width - text_width) / 2, 108), name_text, font=font)
im.save(file_name, "PNG")
# Resize the image and crop
resize_and_crop(file_name, file_name, (460, 215))
os.remove(temp_location)
return True
def resize_and_crop(img_path, modified_path, size, crop_type='middle'):
"""
(Thanks to github.com/sigilioso)
Resize and crop an image to fit the specified size.
args:
img_path: path for the image to resize.
modified_path: path to store the modified image.
size: `(width, height)` tuple.
crop_type: can be 'top', 'middle' or 'bottom', depending on this
value, the image will cropped getting the 'top/left', 'middle' or
'bottom/right' of the image to fit the size.
raises:
Exception: if can not open the file in img_path of there is problems
to save the image.
ValueError: if an invalid `crop_type` is provided.
"""
# If height is higher we resize vertically, if not we resize horizontally
img = Image.open(img_path)
# Get current and desired ratio for the images
img_ratio = img.size[0] / float(img.size[1])
ratio = size[0] / float(size[1])
#The image is scaled/cropped vertically or horizontally depending on the ratio
if ratio > img_ratio:
img = img.resize((size[0], round(size[0] * img.size[1] / img.size[0])),
Image.ANTIALIAS)
# Crop in the top, middle or bottom
if crop_type == 'top':
box = (0, 0, img.size[0], size[1])
elif crop_type == 'middle':
box = (0, round((img.size[1] - size[1]) / 2), img.size[0],
round((img.size[1] + size[1]) / 2))
elif crop_type == 'bottom':
box = (0, img.size[1] - size[1], img.size[0], img.size[1])
else :
raise ValueError('ERROR: invalid value for crop_type')
img = img.crop(box)
elif ratio < img_ratio:
img = img.resize((round(size[1] * img.size[0] / img.size[1]), size[1]),
Image.ANTIALIAS)
# Crop in the top, middle or bottom
if crop_type == 'top':
box = (0, 0, size[0], img.size[1])
elif crop_type == 'middle':
box = (round((img.size[0] - size[0]) / 2), 0,
round((img.size[0] + size[0]) / 2), img.size[1])
elif crop_type == 'bottom':
box = (img.size[0] - size[0], 0, img.size[0], img.size[1])
else :
raise ValueError('ERROR: invalid value for crop_type')
img = img.crop(box)
else :
img = img.resize((size[0], size[1]),
Image.ANTIALIAS)
# If the scale is the same, we do not need to crop
img.save(modified_path)
def replace_str_index(text,index=0,replacement=''):
return '%s%s%s'%(text[:index],replacement,text[index+1:])
def string_similarity(a, b):
return SequenceMatcher(None, a, b).ratio()