This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetFreeGames.py
129 lines (113 loc) · 4.59 KB
/
GetFreeGames.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
import requests
import json
import re
import time
from datetime import datetime
import json
from os import path
from os import remove
###### VARIABLES ######
IPC = "127.0.0.1:1242" #IP of your IPC (default: 127.0.0.1:1242)
saveprogress=True
claimonfind=True
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
configfile = "progress.json"
###### FUNCTIONS ######
def log(x):
print('[' + str(datetime.now()) + "] - "+x)
def redeem(key):
cmd = "addlicense ASF "+str(key)
log("Running: "+key)
r = requests.post("http://"+IPC+"/Api/Command" , json=({"Command":cmd})) #Send the games to ASF
log(json.loads(r.text)["Result"])
def display_time(seconds): #Stole from https://stackoverflow.com/a/24542445
intervals = (
('w', 604800), # 60 * 60 * 24 * 7
('d', 86400), # 60 * 60 * 24
('h', 3600), # 60 * 60
('m', 60),
('s', 1),
)
result = []
for name, count in intervals:
value = seconds // count
if value:
seconds -= value * count
if value == 1:
name = name.rstrip('s')
result.append("{}{}".format(value, name))
return ', '.join(result)
###### START UP ######
if saveprogress: #If the file doesn't exist yet, create it
if not path.exists(configfile):
with open(configfile, "w") as f:
json.dump({"stopped_at":0, "found": []}, f) #create save file
print("Sit back and enjoy while we collect games for you.")
###### LOOP ######
while True:
new = requests.get("http://api.steampowered.com/ISteamApps/GetAppList/v0002/",headers=headers).text #Request list of all games and their IDs
apps = json.loads(new)["applist"]["apps"]
appIDs = []
del new
for a in range(len(apps)):
appIDs.append(apps[a]["appid"]) #Keep only the game IDs
del apps
log("Found all games, now crawling through them to check if any is free..")
ratelimit = 0 # Counter for the rate limit
i = 0
if saveprogress:
with open(configfile,"r") as f: #Take back where we left
content = json.loads(f.read())
i = content["stopped_at"]
if not claimonfind:
freeGames = content["found"]
if i >= len(appIDs):
i = 0
for i in range(i,len(appIDs)): #Loop through all app IDs
ratelimit+=1
i+=1
progress = str(i)+"/"+str(len(appIDs)) #done/total
ETA = display_time(((len(appIDs)-i)/20)*60) # ( ( total apps to go through - apps went through ) / ratelimit ) * 60
appInfo=requests.get("https://store.steampowered.com/api/appdetails?appids="+str(appIDs[i]),headers=headers) #Get info about the current game from steamAPI
if appInfo.status_code != 200: #If the server doesn't answer with OK, assume we're being rate limited and wait a minute before requesting again
log("We're being rate limited! Waiting 60 seconds.. ("+progress+", ETA: "+ETA+" left)")
ratelimit = 0
time.sleep(60)
appInfo=requests.get("https://store.steampowered.com/api/appdetails?appids="+str(appIDs[i]),headers=headers)
appInfo = appInfo.text
if "discount_percent" in appInfo:
s = re.search("discount_percent\":(\d+)",appInfo).group(1)
if s == "100": #If there is a 100% reduction, keep the game
package = re.search("packageid\":(\d+)",appInfo).group(1)
if claimonfind:
redeem("a/"+package) #Redeem the game as an app
redeem("s/"+package) #Redeem the game as a sub
else:
freeGames.append(package)
log("Found game "+str(appIDs[i])+' ')
if re.search("packageid\":(\d+)",appInfo):
print("Searched through "+progress+" titles. Current subID: "+re.search("packageid\":(\d+)",appInfo).group(1)+" ETA:"+ETA+" left.",end=" \r")
else:
print("Searched through "+progress+ " titles. Current subID: INVALID ETA:"+ETA+" left.",end=" \r")
if ratelimit == 20:
print("Sleeping 60 seconds to avoid being rate limited! ("+progress+", ETA:"+ETA+" left.)",end=" \r")
ratelimit = 0
if saveprogress:
content = ""
with open(configfile, 'r+') as f:
content = json.loads(f.read())
content["stopped_at"] = i
if not claimonfind:
content["found"] = freeGames
remove(configfile)
with open(configfile,"w") as f:
json.dump(content, f)
time.sleep(60)
if not claimonfind:
redeem("a/"+",a/".join(freeGames)) #Redeem the games as apps
redeem("s/"+",s/".join(freeGames)) #Redeem the games as subs
del freeGames
if saveprogress:
remove(configfile)
with open(configfile, "w") as f:
json.dump({"stopped_at":0, "found": []}, f)