This repository has been archived by the owner on Oct 28, 2019. It is now read-only.
/
start.py
292 lines (251 loc) · 9.68 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
import requests
import cookielib
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()
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)
if sys.platform.startswith('win32'):
ctypes.windll.kernel32.SetConsoleTitleA("Idle Master")
logging.warning(Fore.GREEN + "WELCOME TO IDLE MASTER" + Fore.RESET)
try:
authData={}
authData["sort"]=""
authData["steamparental"]=""
authData["hasPlayTime"]="false"
execfile("./settings.txt",authData)
myProfileURL = "http://steamcommunity.com/profiles/"+authData["steamLogin"][:17]
except:
logging.warning(Fore.RED + "Error loading config file" + Fore.RESET)
raw_input("Press Enter to continue...")
sys.exit()
if not authData["sessionid"]:
logging.warning(Fore.RED + "No sessionid set" + Fore.RESET)
raw_input("Press Enter to continue...")
sys.exit()
if not authData["steamLogin"]:
logging.warning(Fore.RED + "No steamLogin set" + Fore.RESET)
raw_input("Press Enter to continue...")
sys.exit()
def generateCookies():
global authData
try:
cookies = dict(sessionid=authData["sessionid"], steamLogin=authData["steamLogin"], steamparental=authData["steamparental"])
except:
logging.warning(Fore.RED + "Error setting cookies" + Fore.RESET)
raw_input("Press Enter to continue...")
sys.exit()
return cookies
def dropDelay(numDrops):
if numDrops>1:
baseDelay = (15*60)
else:
baseDelay = (5*60)
return baseDelay
def idleOpen(appID):
try:
logging.warning("Starting game " + getAppName(appID) + " to idle cards")
global process_idle
global idle_time
idle_time = time.time()
if sys.platform.startswith('win32'):
process_idle = subprocess.Popen("steam-idle.exe "+str(appID))
elif sys.platform.startswith('darwin'):
process_idle = subprocess.Popen(["./steam-idle", str(appID)])
elif sys.platform.startswith('linux'):
process_idle = subprocess.Popen(["python2", "steam-idle.py", str(appID)])
except:
logging.warning(Fore.RED + "Error launching steam-idle with game ID " + str(appID) + Fore.RESET)
raw_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) + " took " + Fore.GREEN + str(datetime.timedelta(seconds=total_time)) + Fore.RESET + " to idle.")
except:
logging.warning(Fore.RED + "Error closing game. Exiting." + Fore.RESET)
raw_input("Press Enter to continue...")
sys.exit()
def chillOut(appID):
logging.warning("Suspending operation for "+getAppName(appID))
idleClose(appID)
stillDown = True
while stillDown:
logging.warning("Sleeping for 5 minutes.")
time.sleep(5*60)
try:
rBadge = requests.get(myProfileURL+"/gamecards/" + str(appID) + "/",cookies=cookies)
indBadgeData = bs4.BeautifulSoup(rBadge.text)
badgeLeftString = indBadgeData.find_all("span",{"class": "progress_info_bold"})[0].contents[0]
if "card drops" in badgeLeftString:
stillDown = False
except:
logging.warning("Still unable to find drop info.")
# Resume operations.
idleOpen(appID)
def getAppName(appID):
try:
api = requests.get("http://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"].encode('ascii', 'ignore') + Fore.RESET
except:
return Fore.CYAN + "App "+str(appID) + Fore.RESET
def getPlainAppName(appid):
try:
api = requests.get("http://store.steampowered.com/api/appdetails/?appids=" + str(appID) + "&filters=basic")
api_data = json.loads(api.text)
return api_data[str(appID)]["data"]["name"].encode('ascii', 'ignore')
except:
return "App "+str(appID)
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
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)
raw_input("Press Enter to continue...")
sys.exit()
try:
badgesLeft = []
badgePageData = bs4.BeautifulSoup(r.text)
badgeSet = badgePageData.find_all("div",{"class": "badge_title_stats"})
except:
logging.warning(Fore.RED + "Error finding drop info" + Fore.RESET)
raw_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)
badgeSet = badgeSet + badgePageData.find_all("div",{"class": "badge_title_stats"})
currentpage = currentpage + 1
except:
logging.warning("Reading badge page, please wait")
userinfo = badgePageData.find("div",{"class": "user_avatar"})
if not userinfo:
logging.warning(Fore.RED + "Invalid cookie data, cannot log in to Steam" + Fore.RESET)
raw_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("[0-9\.] 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) + " on blacklist, skipping game")
continue
else:
if authData["sort"]=="mostvalue" or authData["sort"]=="leastvalue":
gameValue = requests.get("http://api.enhancedsteam.com/market_data/average_card_price/?appid=" + str(badgeId) + "&cur=usd")
push = [badgeId, dropCountInt, float(str(gameValue.text))]
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)
raw_input("Press Enter to continue...")
sys.exit()
for appID, drops, value in games:
delay = dropDelay(int(drops))
stillHaveDrops=1
numCycles=50
maxFail=2
idleOpen(appID)
logging.warning(getAppName(appID) + " has " + str(drops) + " card drops remaining")
if sys.platform.startswith('win32'):
ctypes.windll.kernel32.SetConsoleTitleA("Idle Master - Idling " + getPlainAppName(appID) + " [" + str(drops) + " remaining]")
while stillHaveDrops==1:
try:
logging.warning("Sleeping for " + str(delay / 60) + " minutes")
time.sleep(delay)
numCycles-=1
if numCycles<1: # Sanity check against infinite loop
stillHaveDrops=0
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)
badgeLeftString = indBadgeData.find_all("span",{"class": "progress_info_bold"})[0].contents[0]
if "No card drops" in badgeLeftString:
logging.warning("No card drops remaining")
stillHaveDrops=0
else:
dropCountInt, junk = badgeLeftString.split(" ",1)
dropCountInt = int(dropCountInt)
delay = dropDelay(dropCountInt)
logging.warning(getAppName(appID) + " has " + str(dropCountInt) + " card drops remaining")
if sys.platform.startswith('win32'):
ctypes.windll.kernel32.SetConsoleTitleA("Idle Master - Idling " + getPlainAppName(appID) + " [" + str(dropCountInt) + " remaining]")
except:
if maxFail>0:
logging.warning("Error checking if drops are done, number of tries remaining: " + str(maxFail))
maxFail-=1
else:
# Suspend operations until Steam can be reached.
chillOut(appID)
maxFail+=1
break
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)
raw_input("Press Enter to continue...")