-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathactivator.d
More file actions
191 lines (171 loc) · 5.12 KB
/
Copy pathactivator.d
File metadata and controls
191 lines (171 loc) · 5.12 KB
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
import core.runtime;
import core.thread;
import std.algorithm.iteration;
import std.algorithm.searching;
import std.array;
import std.conv;
import std.datetime.systime;
import std.file;
import std.json;
import std.path;
import std.stdio : stderr, File;
import std.string;
import std.typecons;
import ae.sys.file;
import ae.sys.net;
import ae.sys.net.cachedcurl;
import ae.utils.digest;
import ae.utils.funopt;
import ae.utils.main;
import ae.utils.regex;
import ae.utils.time;
bool verbose;
CachedCurlNetwork ccnet;
void activateHBProducts()
{
string[] hbKeys;
{
scope(failure) stderr.writeln("Error extracting Humble Bundle keys array. Make sure you are logged in and your cookies file is up to date.");
hbKeys =
(cast(string)ccnet.getFile("https://www.humblebundle.com/home/keys"))
.extractCapture(re!`"gamekeys": (\[[^\]]*\]), `)
.front
.to!(string[]);
}
stderr.writefln!"Got %d HumbleBundle keys"(hbKeys.length);
activateHBKeys(hbKeys);
}
struct SteamKey
{
string key;
string name;
string toString() const { return name ? format!"%s (%s)"(key, name) : key; }
}
void activateHBKeys(string[] hbKeys)
{
SteamKey[] steamKeys;
foreach (n, hbKey; hbKeys)
{
stderr.writefln!"[%d/%d] Fetching Steam keys for HB product key: %s"(n+1, hbKeys.length, hbKey);
auto res = getFile(cast(string)("https://www.humblebundle.com/api/v1/order/" ~ hbKey ~ "?all_tpkds=true"));
if (verbose) stderr.writeln("\t", cast(string)res);
auto j = parseJSON(cast(string)res);
foreach (tpk; j["tpkd_dict"]["all_tpks"].array)
if (tpk["key_type"].str == "steam" && "redeemed_key_val" in tpk.object)
{
auto steamKey = tpk["redeemed_key_val"].str;
if (!steamKey.canFind("<a href"))
{
stderr.writeln("\t", "Found Steam key: ", steamKey);
steamKeys ~= SteamKey(steamKey, "human_name" in tpk ? tpk["human_name"].str : null);
}
}
}
activateSteamKeys(steamKeys);
}
void activateSteamKeys(SteamKey[] steamKeys)
{
auto sessionID =
(cast(string)ccnet.getFile("https://store.steampowered.com/account/registerkey"))
.extractCapture(re!`var g_sessionID = "([^"]*)";`)
.front;
if (verbose) stderr.writeln("Got Steam session ID: ", sessionID);
enum resultFile = "results.txt";
string[][] results;
if (resultFile.exists)
results = resultFile.readText.splitLines.map!(s => s.split("\t")).array;
foreach (n, key; steamKeys)
{
stderr.writefln!"[%d/%d] Activating Steam key: %s"(n+1, steamKeys.length, key);
auto existingResult = results.find!(result => result[0] == key.key);
if (!existingResult.empty)
{
stderr.writeln("\t", "Already activated (", existingResult.front[1], ")");
continue;
}
ccnet.epoch = 0;
scope(exit) ccnet.epoch = 0;
while (true)
{
auto res = ccnet.post("https://store.steampowered.com/account/ajaxregisterkey/", "product_key=" ~ key.key ~ "&sessionid=" ~ sessionID);
if (verbose) stderr.writeln("\t", cast(string)res);
auto j = parseJSON(cast(string)res);
auto code = j["purchase_result_details"].integer;
switch (code)
{
case 0:
stderr.writeln("\t", "Activated successfully!");
File(resultFile, "a").writefln!"%s\t%s\t%s"(key.key, "Activated", key.name);
break;
case 9:
stderr.writeln("\t", "Already have this product");
File(resultFile, "a").writefln!"%s\t%s\t%s"(key.key, "Already owned", key.name);
break;
case 14:
stderr.writeln("\t", "Invalid key");
File(resultFile, "a").writefln!"%s\t%s\t%s"(key.key, "Invalid key", key.name);
break;
case 15:
stderr.writeln("\t", "Key associated with other account");
File(resultFile, "a").writefln!"%s\t%s\t%s"(key.key, "Key in-use elsewhere", key.name);
break;
case 24:
stderr.writeln("\t", "Need another product");
File(resultFile, "a").writefln!"%s\t%s\t%s"(key.key, "Need another", key.name);
break;
case 53:
stderr.writeln("\t", "Throttled, waiting...");
Thread.sleep(5.minutes);
ccnet.epoch = Clock.currStdTime;
continue;
default:
throw new Exception("Unknown code: " ~ text(code));
}
break;
}
}
}
struct Activator
{
static:
@(`Activate all Steam keys from your HumbleBundle library`)
void humbleBundle()
{
activateHBProducts();
}
@(`Activate Steam keys from HumbleBundle product keys from file`)
void hbKeys(
Parameter!(string, "Text file containing HumbleBundle product keys, one per line") fileName
)
{
activateHBKeys(readText(fileName).splitLines);
}
@(`Activate Steam keys from file`)
void steamKeys(
Parameter!(string, "Text file containing Steam keys, one per line") fileName
)
{
activateSteamKeys(readText(fileName).splitLines.map!(line => SteamKey(line)).array);
}
}
void usageFun(string usage)
{
stderr.writeln(usage, funoptDispatchUsage!Activator);
}
void dispatch(
bool verbose,
Parameter!(string, "Action to perform (see list below)") action,
immutable(string)[] actionArguments = null,
)
{
ccnet = cast(CachedCurlNetwork)net;
ccnet.http.verbose = .verbose = verbose;
ccnet.cookieDir = "cookies";
ccnet.cookieExt = ".txt";
funoptDispatch!Activator([thisExePath, action] ~ actionArguments);
}
void run(string[] args)
{
funopt!(dispatch, FunOptConfig.init, usageFun)(args);
}
mixin main!run;