Skip to content

Commit ba73853

Browse files
authored
Merge pull request #1 from ranieuwe/main
Backoff logic and init results.json + requirements.txt init
2 parents da218b0 + 2d692bb commit ba73853

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

redeem_code.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import json
1313
import sys
1414
import time
15+
from os.path import exists
1516

1617
import requests
18+
from requests.adapters import HTTPAdapter, Retry
1719

1820
# Handle arguments the script is called with
1921
parser = argparse.ArgumentParser()
@@ -29,8 +31,13 @@
2931
with open(args.player_file, encoding="utf-8") as player_file:
3032
players = json.loads(player_file.read())
3133

32-
with open(args.results_file, encoding="utf-8") as results_file:
33-
results = json.loads(results_file.read())
34+
# Initalize results to not error if no results file exists yet
35+
results = []
36+
37+
# If a results file exists, load it
38+
if exists(args.results_file):
39+
with open(args.results_file, encoding="utf-8") as results_file:
40+
results = json.loads(results_file.read())
3441

3542
# Retrieve the result set if it exists or create an empty one
3643
# We make sure that we get a view of the dictionary so we can modify
@@ -39,14 +46,14 @@
3946
(result for result in results if result["code"] == args.code), None)
4047

4148
if found_item is None:
49+
print("New code: " + args.code + " adding to results file and processing.")
4250
new_item = {"code": args.code, "status": {}}
4351
results.append(new_item)
4452
result = new_item
4553
else:
4654
result = found_item
4755

4856
# Some variables that are used to tracking progress
49-
session_counter = 1
5057
counter_successfully_claimed = 0
5158
counter_already_claimed = 0
5259
counter_error = 0
@@ -58,6 +65,13 @@
5865
"Accept": "application/json"}
5966

6067
i = 0
68+
69+
# Enable retry login and backoff behavior so if you have a large number of players (> 30) it'll not fail
70+
# Default rate limits of WOS API is 30 in 1 min.
71+
r = requests.Session()
72+
retry_config = Retry(total=5, backoff_factor=1, status_forcelist=[ 429 ], allowed_methods=False)
73+
r.mount("https://", HTTPAdapter(max_retries=retry_config))
74+
6175
for player in players:
6276

6377
# Print progress bar
@@ -81,12 +95,15 @@
8195
# Login the player
8296
# It is enough to send the POST request, we don't need to store any cookies/session tokens
8397
# to authenticate during the next request
84-
login_request = requests.post(
98+
login_request = r.post(
8599
URL + '/player', data=request_data, headers=HTTP_HEADER, timeout=30)
86100
login_response = login_request.json()
101+
102+
# Login failed for user, report, count error and continue gracefully to complete all other players
87103
if login_response["msg"] != "success":
88-
print("Login not possible")
89-
sys.exit(1)
104+
print("Login not possible for player: " + player["original_name"] + " / " + player["id"] + " - validate their player ID. Skipping.")
105+
counter_error += 1
106+
continue
90107

91108
# Create the request data that contains the signature and the code
92109
request_data["cdk"] = args.code
@@ -96,7 +113,7 @@
96113
SALT).encode("utf-8")).hexdigest()
97114

98115
# Send the gif code redemption request
99-
redeem_request = requests.post(
116+
redeem_request = r.post(
100117
URL + '/gift_code', data=request_data, headers=HTTP_HEADER, timeout=30)
101118
redeem_response = redeem_request.json()
102119

@@ -120,12 +137,6 @@
120137
print("\nError occurred: " + str(redeem_response))
121138
counter_error += 1
122139

123-
# Refresh the webpage every 5 players to avoid getting soft-banned at some point
124-
if session_counter % 5 == 0:
125-
time.sleep(5)
126-
127-
session_counter += 1
128-
129140
with open(args.results_file, 'w', encoding="utf-8") as fp:
130141
json.dump(results, fp)
131142

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
certifi==2024.7.4
2+
charset-normalizer==3.3.2
3+
idna==3.7
4+
requests==2.32.3
5+
urllib3==2.2.2

0 commit comments

Comments
 (0)