-
Notifications
You must be signed in to change notification settings - Fork 8
/
start.py
380 lines (330 loc) · 14.1 KB
/
start.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
#!/usr/bin/env python
import requests
import http.cookiejar
import bs4
import time
import re
import subprocess
import sys
import os
import json
import logging
import datetime
import ctypes
from colorama import init, Fore, Back, Style
init()
version = "v2.2"
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
logging.basicConfig(filename = "idlemaster.log", filemode = "w", format = "[ %(asctime)s ] %(message)s", datefmt = "%m/%d/%Y %I:%M:%S %p", level = logging.DEBUG)
console = logging.StreamHandler()
console.setLevel(logging.WARNING)
console.setFormatter(logging.Formatter("[ %(asctime)s ] %(message)s", "%m/%d/%Y %I:%M:%S %p"))
logging.getLogger('').addHandler(console)
logging.warning(Fore.GREEN + "WELCOME TO IDLE MASTER - " + Fore.YELLOW + version + Fore.RESET)
pyPath = "python"
try:
subprocess.call(["python3", "--version"], stdout = subprocess.DEVNULL)
except:
logging.warning(Fore.RED + "python3 not installed" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
try:
pyOut = subprocess.check_output(["python", "--version"])
pyVer = re.search("Python 3", str(pyOut))
if not pyVer:
pyPath = "python3"
logging.warning(Fore.YELLOW + "Python pointing to incorrect version, using Python3 instead" + Fore.RESET)
except:
pyPath = "python3"
logging.warning(Fore.YELLOW + "Python path error, using Python3 instead" + Fore.RESET)
try:
authData = {}
authData["sort"] = ""
authData["steamparental"] = ""
authData["hasPlayTime"] = "false"
exec(open("./settings.txt").read(), authData)
myProfileURL = "https://steamcommunity.com/profiles/" + authData["steamLoginSecure"][:17]
except:
logging.warning(Fore.RED + "Error loading config file" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
if not authData["sessionid"]:
logging.warning(Fore.RED + "No sessionid set" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
if not authData["steamLoginSecure"]:
logging.warning(Fore.RED + "No steamLoginSecure set" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
def generateCookies():
global authData
try:
cookies = dict(sessionid = authData["sessionid"], steamLoginSecure = authData["steamLoginSecure"], steamparental = authData["steamparental"], Steam_Language = "english")
except:
logging.warning(Fore.RED + "Error setting cookies" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
return cookies
def idleOpen(appID):
try:
logging.warning("Starting game " + getAppName(appID) + " to idle cards")
global process_idle
global idle_time
idle_time = time.time()
process_idle = subprocess.Popen([pyPath, "steam-idle.py", str(appID)])
except:
logging.warning(Fore.RED + "Error launching steam-idle with game ID " + str(appID) + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
def idleClose(appID):
try:
logging.warning("Closing game " + getAppName(appID))
process_idle.terminate()
total_time = int(time.time() - idle_time)
logging.warning(getAppName(appID) + " idled for " + Fore.GREEN + str(datetime.timedelta(seconds = total_time)) + Fore.RESET)
except:
logging.warning(Fore.RED + "Error closing game" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
def chillOut(appID):
logging.warning(Fore.YELLOW + "Suspending operation for " + Fore.RESET + getAppName(appID))
idleClose(appID)
stillDown = True
while stillDown:
logging.warning("Sleeping for 5 minutes")
time.sleep(300)
try:
# Check if cookies still valid or steam is down (network error)
steamUp = requests.get("https://store.steampowered.com")
rCode = steamUp.status_code
if rCode == 200:
expired = cookieTest()
if expired:
idleClose(appID)
logging.warning(Fore.RED + "Cookie session expired" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
else:
stillDown = False
else:
logging.warning(Fore.YELLOW + "Still unable to connect to Steam" + Fore.RESET)
except SystemExit:
sys.exit()
except:
logging.warning(Fore.YELLOW + "Still unable to find drop info" + Fore.RESET)
# Resume operations.
idleOpen(appID)
def getAppName(appID):
try:
api = requests.get("https://store.steampowered.com/api/appdetails/?appids=" + str(appID) + "&filters=basic")
api_data = json.loads(api.text)
return Fore.CYAN + api_data[str(appID)]["data"]["name"] + Fore.RESET
except:
return Fore.CYAN + "App " + str(appID) + Fore.RESET
def get_blacklist():
try:
with open("blacklist.txt", "r") as f:
lines = f.readlines()
blacklist = [int(n.strip()) for n in lines]
except:
blacklist = [];
if not blacklist:
logging.warning("No games have been blacklisted")
return blacklist
def blacklist_game(appID):
try:
with open("blacklist.txt", "a") as f:
f.write(str(appID) + "\n")
except:
logging.warning(Fore.RED + "Failed to blacklist game" + Fore.RESET)
def cookieTest():
try:
r = requests.get(myProfileURL + "/badges/", cookies = cookies)
badgePageData = bs4.BeautifulSoup(r.text, "html.parser")
userinfo = badgePageData.find("a", {"class": "user_avatar"})
if userinfo:
return False
else:
return True
except:
return True
logging.warning("Finding games that have card drops remaining")
try:
cookies = generateCookies()
r = requests.get(myProfileURL + "/badges/", cookies = cookies)
except:
logging.warning(Fore.RED + "Error reading badge page" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
try:
badgesLeft = []
badgePageData = bs4.BeautifulSoup(r.text, "html.parser")
badgeSet = badgePageData.find_all("div", {"class": "badge_title_stats"})
except:
logging.warning(Fore.RED + "Error finding drop info" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
# For profiles with multiple pages
try:
badgePages = int(badgePageData.find_all("a", {"class": "pagelink"})[-1].text)
if badgePages:
logging.warning(str(badgePages) + " badge pages found, gathering additional data")
currentpage = 2
while currentpage <= badgePages:
r = requests.get(myProfileURL + "/badges/?p=" + str(currentpage), cookies = cookies)
badgePageData = bs4.BeautifulSoup(r.text, "html.parser")
badgeSet = badgeSet + badgePageData.find_all("div", {"class": "badge_title_stats"})
currentpage = currentpage + 1
except:
logging.warning("Reading badge page, please wait")
userinfo = badgePageData.find("a", {"class": "user_avatar"})
if not userinfo:
logging.warning(Fore.RED + "Invalid cookie data, cannot log in to Steam" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
blacklist = get_blacklist()
if authData["sort"] == "mostvalue" or authData["sort"] == "leastvalue":
logging.warning("Getting card values, please wait...")
for badge in badgeSet:
try:
badge_text = badge.get_text()
dropCount = badge.find_all("span", {"class": "progress_info_bold"})[0].contents[0]
has_playtime = re.search("hrs on record", badge_text) != None
if "No card drops" in dropCount or (has_playtime == False and authData["hasPlayTime"].lower() == "true") :
continue
else:
# Remaining drops
dropCountInt, junk = dropCount.split(" ", 1)
dropCountInt = int(dropCountInt)
linkGuess = badge.find_parent().find_parent().find_parent().find_all("a")[0]["href"]
junk, badgeId = linkGuess.split("/gamecards/", 1)
badgeId = int(badgeId.replace("/", ""))
if badgeId in blacklist:
logging.warning(getAppName(badgeId) + Fore.YELLOW + " on blacklist, skipping game" + Fore.RESET)
continue
else:
if authData["sort"] == "mostvalue" or authData["sort"] == "leastvalue":
api = requests.get("https://api.augmentedsteam.com/v2/market/cards/average-prices/?appids=" + str(badgeId) + "¤cy=usd")
api_data = json.loads(api.text)
if api.text.find('regular') == -1:
logging.warning("No card data for" + Fore.CYAN + " App " + str(badgeId) + Fore.RESET + " skipping...")
gameValue = "0"
else:
gameValue = api_data['data'][str(badgeId)]['regular']
push = [badgeId, dropCountInt, float(str(gameValue))]
badgesLeft.append(push)
else:
push = [badgeId, dropCountInt, 0]
badgesLeft.append(push)
except:
continue
logging.warning("Idle Master needs to idle " + Fore.GREEN + str(len(badgesLeft)) + Fore.RESET + " games")
def getKey(item):
if authData["sort"] == "mostcards" or authData["sort"] == "leastcards":
return item[1]
elif authData["sort"] == "mostvalue" or authData["sort"] == "leastvalue":
return item[2]
else:
return item[0]
sortValues = ["", "mostcards", "leastcards", "mostvalue", "leastvalue"]
if authData["sort"] in sortValues:
if authData["sort"] == "":
games = badgesLeft
if authData["sort"] == "mostcards" or authData["sort"] == "mostvalue":
games = sorted(badgesLeft, key = getKey, reverse = True)
if authData["sort"] == "leastcards" or authData["sort"] == "leastvalue":
games = sorted(badgesLeft, key = getKey, reverse = False)
else:
logging.warning(Fore.RED + "Invalid sort value" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
numSkip = 0
for appID, drops, value in games:
delay = (int(drops) * 600)
stillHaveDrops = 1
numCycles = 50
maxFail = 2
skip = False
idleOpen(appID)
logging.warning(getAppName(appID) + " has " + str(drops) + " card drops remaining")
while stillHaveDrops == 1:
try:
ftime = "{:n}".format(delay / 60)
logging.warning("Sleeping for " + str(ftime) + " minutes")
time.sleep(delay)
numCycles -= 1
# Sanity check against infinite loop
if numCycles < 1:
stillHaveDrops = 0
# Error Checking
# Check if cookies still valid or steam is down (network error)
expired = cookieTest()
if expired:
steamUp = requests.get("https://store.steampowered.com")
rCode = steamUp.status_code
if rCode == 200:
idleClose(appID)
logging.warning(Fore.RED + "Cookie session expired" + Fore.RESET)
input("Press Enter to continue...")
sys.exit()
else:
logging.warning("Checking to see if " + getAppName(appID) + " has remaining card drops")
rBadge = requests.get(myProfileURL + "/gamecards/" + str(appID) + "/", cookies = cookies)
indBadgeData = bs4.BeautifulSoup(rBadge.text, "html.parser")
badgeLeftString = indBadgeData.find_all("span", {"class": "progress_info_bold"})[0].contents[0]
dropCountInt, junk = badgeLeftString.split(" ", 1)
if dropCountInt.isdigit():
dropCountInt = int(dropCountInt)
delay = (dropCountInt * 600)
logging.warning(getAppName(appID) + " has " + str(dropCountInt) + " card drops remaining")
else:
logging.warning("No card drops remaining")
stillHaveDrops = 0
except KeyboardInterrupt:
idleClose(appID)
logging.warning(Fore.YELLOW + "User interrupted script" + Fore.RESET)
ans = True
while ans:
print(" q = Quit")
print(" r = Resume")
print(" s = Skip game")
print(" b = Blacklist game")
ans = input("Select option ... ")
if ans == "q" or ans == "Q":
logging.warning(Fore.RED + "User quit script" + Fore.RESET)
sys.exit()
elif ans == "r" or ans == "R" or ans == "":
logging.warning(Fore.GREEN + "Resuming idling" + Fore.RESET)
idleOpen(appID)
break
elif ans == "s" or ans == "S":
logging.warning(Fore.YELLOW + "Skipping game " + Fore.GREEN + getAppName(appID) + Fore.RESET)
skip = True
numSkip += 1
break
elif ans == "b" or ans == "B":
logging.warning(Fore.RED + "Blacklisting game " + Fore.GREEN + getAppName(appID) + Fore.RESET)
blacklist_game(appID)
skip = True
break
elif ans != "":
logging.warning(Fore.YELLOW + "Invalid option ... " + Fore.RESET)
if skip:
break
except SystemExit:
sys.exit()
except:
if maxFail > 0:
logging.warning(Fore.YELLOW + "Steam unreachable or network error, number of tries remaining: " + Fore.RESET + str(maxFail))
delay = 300
maxFail -= 1
else:
# Suspend operations until Steam can be reached
chillOut(appID)
maxFail += 1
if not skip:
idleClose(appID)
logging.warning(Fore.GREEN + "Successfully completed idling cards for " + getAppName(appID) + Fore.RESET)
logging.warning(Fore.GREEN + "Successfully completed idling process" + Fore.RESET)
logging.warning(Fore.YELLOW + str(numSkip) + " games skipped" + Fore.RESET)
input("Press Enter to continue...")