Skip to content

Commit bec0e43

Browse files
committed
Sanity commit to include Black formatting rules
1 parent ba73853 commit bec0e43

File tree

1 file changed

+60
-26
lines changed

1 file changed

+60
-26
lines changed

redeem_code.py

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
in case it runs into errors without retrying to redeem a code
88
for everyone
99
"""
10+
1011
import argparse
1112
import hashlib
1213
import json
@@ -19,12 +20,10 @@
1920

2021
# Handle arguments the script is called with
2122
parser = argparse.ArgumentParser()
22-
parser.add_argument('-c', '--code', required=True)
23-
parser.add_argument('-f', '--player-file',
24-
dest='player_file', default='player.json')
25-
parser.add_argument('-r', '--results-file',
26-
dest='results_file', default='results.json')
27-
parser.add_argument('--restart', dest='restart', action="store_true")
23+
parser.add_argument("-c", "--code", required=True)
24+
parser.add_argument("-f", "--player-file", dest="player_file", default="player.json")
25+
parser.add_argument("-r", "--results-file", dest="results_file", default="results.json")
26+
parser.add_argument("--restart", dest="restart", action="store_true")
2827
args = parser.parse_args()
2928

3029
# Open and read the user files
@@ -42,8 +41,7 @@
4241
# Retrieve the result set if it exists or create an empty one
4342
# We make sure that we get a view of the dictionary so we can modify
4443
# it in our code and simply write the entire result list to file again later
45-
found_item = next(
46-
(result for result in results if result["code"] == args.code), None)
44+
found_item = next((result for result in results if result["code"] == args.code), None)
4745

4846
if found_item is None:
4947
print("New code: " + args.code + " adding to results file and processing.")
@@ -61,24 +59,36 @@
6159
URL = "https://wos-giftcode-api.centurygame.com/api"
6260
# The salt is appended to the string that is then signed using md5 and sent as part of the request
6361
SALT = "tB87#kPtkxqOS2"
64-
HTTP_HEADER = {"Content-Type": "application/x-www-form-urlencoded",
65-
"Accept": "application/json"}
62+
HTTP_HEADER = {
63+
"Content-Type": "application/x-www-form-urlencoded",
64+
"Accept": "application/json",
65+
}
6666

6767
i = 0
6868

6969
# Enable retry login and backoff behavior so if you have a large number of players (> 30) it'll not fail
7070
# Default rate limits of WOS API is 30 in 1 min.
7171
r = requests.Session()
72-
retry_config = Retry(total=5, backoff_factor=1, status_forcelist=[ 429 ], allowed_methods=False)
72+
retry_config = Retry(
73+
total=5, backoff_factor=1, status_forcelist=[429], allowed_methods=False
74+
)
7375
r.mount("https://", HTTPAdapter(max_retries=retry_config))
7476

7577
for player in players:
7678

7779
# Print progress bar
7880
i += 1
7981

80-
print("\x1b[K" + str(i) + "/" + str(len(players)) +
81-
" complete. Redeeming for " + player["original_name"], end="\r", flush=True)
82+
print(
83+
"\x1b[K"
84+
+ str(i)
85+
+ "/"
86+
+ str(len(players))
87+
+ " complete. Redeeming for "
88+
+ player["original_name"],
89+
end="\r",
90+
flush=True,
91+
)
8292

8393
# Check if the code has been redeemed for this player already
8494
# Continue to the next iteration if it has been
@@ -89,32 +99,50 @@
8999
# This is necessary because we reload the page every 5 players
90100
# and the website isn't sometimes ready before we continue
91101
request_data = {"fid": player["id"], "time": time.time_ns()}
92-
request_data["sign"] = hashlib.md5(("fid=" + request_data["fid"] + "&time=" + str(
93-
request_data["time"]) + SALT).encode("utf-8")).hexdigest()
102+
request_data["sign"] = hashlib.md5(
103+
(
104+
"fid=" + request_data["fid"] + "&time=" + str(request_data["time"]) + SALT
105+
).encode("utf-8")
106+
).hexdigest()
94107

95108
# Login the player
96109
# It is enough to send the POST request, we don't need to store any cookies/session tokens
97110
# to authenticate during the next request
98111
login_request = r.post(
99-
URL + '/player', data=request_data, headers=HTTP_HEADER, timeout=30)
112+
URL + "/player", data=request_data, headers=HTTP_HEADER, timeout=30
113+
)
100114
login_response = login_request.json()
101115

102116
# Login failed for user, report, count error and continue gracefully to complete all other players
103117
if login_response["msg"] != "success":
104-
print("Login not possible for player: " + player["original_name"] + " / " + player["id"] + " - validate their player ID. Skipping.")
118+
print(
119+
"Login not possible for player: "
120+
+ player["original_name"]
121+
+ " / "
122+
+ player["id"]
123+
+ " - validate their player ID. Skipping."
124+
)
105125
counter_error += 1
106126
continue
107127

108128
# Create the request data that contains the signature and the code
109129
request_data["cdk"] = args.code
110-
request_data["sign"] = hashlib.md5(("cdk=" + request_data["cdk"] + \
111-
"&fid=" + request_data["fid"] + \
112-
"&time=" + str(request_data["time"]) + \
113-
SALT).encode("utf-8")).hexdigest()
130+
request_data["sign"] = hashlib.md5(
131+
(
132+
"cdk="
133+
+ request_data["cdk"]
134+
+ "&fid="
135+
+ request_data["fid"]
136+
+ "&time="
137+
+ str(request_data["time"])
138+
+ SALT
139+
).encode("utf-8")
140+
).hexdigest()
114141

115142
# Send the gif code redemption request
116143
redeem_request = r.post(
117-
URL + '/gift_code', data=request_data, headers=HTTP_HEADER, timeout=30)
144+
URL + "/gift_code", data=request_data, headers=HTTP_HEADER, timeout=30
145+
)
118146
redeem_response = redeem_request.json()
119147

120148
# In case the gift code is broken, exit straight away
@@ -137,10 +165,16 @@
137165
print("\nError occurred: " + str(redeem_response))
138166
counter_error += 1
139167

140-
with open(args.results_file, 'w', encoding="utf-8") as fp:
168+
with open(args.results_file, "w", encoding="utf-8") as fp:
141169
json.dump(results, fp)
142170

143171
# Print general stats
144-
print("\nSuccessfully claimed gift code for " + str(counter_successfully_claimed) + " players.\n" +
145-
str(counter_already_claimed) + " had already claimed their gift. \nErrors ocurred for " +
146-
str(counter_error) + " players.")
172+
print(
173+
"\nSuccessfully claimed gift code for "
174+
+ str(counter_successfully_claimed)
175+
+ " players.\n"
176+
+ str(counter_already_claimed)
177+
+ " had already claimed their gift. \nErrors ocurred for "
178+
+ str(counter_error)
179+
+ " players."
180+
)

0 commit comments

Comments
 (0)