-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcore.py
273 lines (218 loc) · 8.2 KB
/
core.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
import os
import subprocess
import shutil
import json
import time
import sys
import logging
from contextlib import contextmanager
from bs4 import BeautifulSoup as parser
# import cfscrape
import requests
from requests.exceptions import ConnectionError, ConnectTimeout
BASE_PATH = "{}/GLR_Manager".format(os.getenv("LOCALAPPDATA"))
PROFILES_PATH = "{}/Profiles".format(BASE_PATH)
CURRENT_VERSION = "1.4.0"
class Game:
def __init__(self, id, name, type):
self.id = id
self.name = name
self.type = type
def to_JSON(self):
return {"id": self.id, "name": self.name, "type": self.type}
def to_string(self):
return "ID: {0}\nName: {1}\nType: {2}\n".format(self.id,self.name,self.type)
def to_list(self):
return [self.id, self.name, self.type]
def __eq__(self, value):
return self.id == value.id and self.name == value.name and self.type == value.type
def __getitem__(self, index):
values_list = list(vars(self).values())
return values_list[index]
@staticmethod
def from_JSON(data):
return Game(data["id"],data["name"],data["type"])
@staticmethod
def from_table_list(list):
games = []
for i in range(int(len(list)/3)):
games.append(Game(list[i * 3], list[i * 3 + 1], list[i * 3 + 2]))
return games
class Profile:
def __init__(self,name = 'default',games = []):
self.name = name
self.games = games
def add_game(self,game):
self.games.append(game)
def remove_game(self,game):
if type(game) is Game:
self.games.remove(game)
else:
for game_ in self.games:
if game_.name == game:
self.games.remove(game_)
def export_profile(self,path = PROFILES_PATH):
data = {"name": self.name, "games": [game.to_JSON() for game in self.games]}
with open("{}/{}.json".format(path, self.name), "w") as outfile:
json.dump(data,outfile,indent=4)
def __eq__(self, value):
return self.name == value.name
@staticmethod
def from_JSON(data):
return Profile(data["name"], [Game.from_JSON(game) for game in data["games"]])
class ProfileManager:
def __init__(self):
self.profiles = {}
self.load_profiles()
def load_profiles(self):
if not os.path.exists(PROFILES_PATH):
os.makedirs(PROFILES_PATH)
self.create_profile("default")
elif len(os.listdir(PROFILES_PATH)) == 0:
self.create_profile("default")
for filename in os.listdir(PROFILES_PATH):
with open("{}/{}".format(PROFILES_PATH,filename), "r") as file:
try:
data = json.load(file)
self.register_profile(Profile.from_JSON(data))
except json.JSONDecodeError as e:
logging.exception(e)
file.close()
os.remove("{}/{}".format(PROFILES_PATH,filename))
def register_profile(self, profile):
self.profiles[profile.name] = profile
def create_profile(self, name, games = []):
if name is "":
return
self.register_profile(Profile(name,games))
self.profiles[name].export_profile(PROFILES_PATH)
def remove_profile(self, profile_name):
self.profiles.pop(profile_name)
os.remove("{}/{}.json".format(PROFILES_PATH,profile_name))
class Config:
def __init__(self, steam_path = "", no_hook = True, compatibility_mode = True, version = CURRENT_VERSION, last_profile = "default", check_update = True):
self.steam_path = steam_path
self.no_hook = no_hook
self.compatibility_mode = compatibility_mode
self.version = version
self.last_profile = last_profile
self.check_update = check_update
def export_config(self):
with open("{}/config.json".format(BASE_PATH), "w") as outfile:
json.dump(vars(self),outfile,indent=4)
@staticmethod
def from_JSON(data):
config = Config()
for key, value in data.items():
if key in vars(config).keys():
setattr(config, key, value)
return config
@staticmethod
def load_config():
if not os.path.isfile("{}/config.json".format(BASE_PATH)):
if not os.path.exists(BASE_PATH):
os.makedirs(BASE_PATH)
config = Config()
config.export_config()
return config
else:
with open("{}/config.json".format(BASE_PATH), "r") as file_:
try:
data = json.load(file_)
config = Config.from_JSON(data)
except Exception as e:
logging.exception(e)
config = Config()
config.version = CURRENT_VERSION
config.export_config()
return config
class ConfigNotLoadedException(Exception):
pass
#-------------
logging.basicConfig(filename='errors.log', filemode="w", level=logging.DEBUG)
config = Config.load_config()
@contextmanager
def get_config():
global config
try:
if config:
yield config
else:
config = Config.load_config()
finally:
config.export_config()
def createFiles(games):
if not os.path.exists("{}/AppList".format(config.steam_path)):
os.makedirs("{}/AppList".format(config.steam_path))
else:
shutil.rmtree("{}/AppList".format(config.steam_path))
time.sleep(0.5)
os.makedirs("{}/AppList".format(config.steam_path))
for i in range(len(games)):
with open("{}/AppList/{}.txt".format(config.steam_path,i),"w") as file:
file.write(games[i].id)
# def parseGames(html):
# p = parser(html, 'html.parser')
# rows = p.find_all("tr", class_= "app")
# games = []
# for row in rows:
# data = row("td")
# if(data[1].get_text() != "Unknown"):
# game = Game(data[0].get_text(),data[2].get_text(),data[1].get_text())
# #print(game.to_string())
# games.append(game)
# return games
def parseDlcs(html):
p = parser(html, 'html.parser')
dlcs = p.find_all("div", class_= "recommendation")
games = []
for dlc in dlcs:
appid = dlc.find("a")["data-ds-appid"]
name = dlc.find("span", class_= "color_created").get_text()
games.append(Game(appid, name, "DLC"))
return games
def getDlcs(appQuery):
appinfo = appQuery.split("/")
appid = appinfo[0]
sanitazedName = appinfo[1]
params = {"sort": "newreleases", "count": 50, "start": 0}
baseUrl = "https://store.steampowered.com/dlc/{0}/{1}/ajaxgetfilteredrecommendations"
response = requests.get(baseUrl.format(appid, sanitazedName), params=params).json()
return parseDlcs(response["results_html"])
def parseGames(html):
p = parser(html, 'html.parser')
results = p.find_all("a", class_= "search_result_row")
games = []
for result in results[:3]:
appid = result["data-ds-appid"]
name = result.find("span", class_= "title").get_text()
games.append(Game(appid, name, "Game"))
appQuery = result["href"].split("app/")
print(appQuery)
if len(appQuery) > 1: games.extend(getDlcs(appQuery[1]))
return games
def queryfy(input_):
arr = input_.split()
result = arr.pop(0)
for word in arr:
result = result + "+" + word
print(result)
return result
def queryGames(query):
try:
params = {"term": query, "count": 25, "start": 0, "category1": 998}
response = requests.get("https://store.steampowered.com/search/results", params=params)
return parseGames(response.content)
except (ConnectionError, ConnectTimeout) as err:
logging.exception(err)
return err
def runUpdater():
if "-NoUpdate" not in sys.argv and config.check_update:
subprocess.run("GLR Updater.exe")
# Post update measure
if "-PostUpdate" in sys.argv:
for fl in os.listdir("./"):
if fl.startswith("new_"):
real_name = fl.replace("new_","")
os.remove(real_name)
os.rename(fl, real_name)