Skip to content

Commit

Permalink
Merge pull request #1 from ranieuwe/main
Browse files Browse the repository at this point in the history
Backoff logic and init results.json + requirements.txt init
  • Loading branch information
Crosswind authored Jul 29, 2024
2 parents da218b0 + 2d692bb commit ba73853
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
37 changes: 24 additions & 13 deletions redeem_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import json
import sys
import time
from os.path import exists

import requests
from requests.adapters import HTTPAdapter, Retry

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

with open(args.results_file, encoding="utf-8") as results_file:
results = json.loads(results_file.read())
# Initalize results to not error if no results file exists yet
results = []

# If a results file exists, load it
if exists(args.results_file):
with open(args.results_file, encoding="utf-8") as results_file:
results = json.loads(results_file.read())

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

if found_item is None:
print("New code: " + args.code + " adding to results file and processing.")
new_item = {"code": args.code, "status": {}}
results.append(new_item)
result = new_item
else:
result = found_item

# Some variables that are used to tracking progress
session_counter = 1
counter_successfully_claimed = 0
counter_already_claimed = 0
counter_error = 0
Expand All @@ -58,6 +65,13 @@
"Accept": "application/json"}

i = 0

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

for player in players:

# Print progress bar
Expand All @@ -81,12 +95,15 @@
# Login the player
# It is enough to send the POST request, we don't need to store any cookies/session tokens
# to authenticate during the next request
login_request = requests.post(
login_request = r.post(
URL + '/player', data=request_data, headers=HTTP_HEADER, timeout=30)
login_response = login_request.json()

# Login failed for user, report, count error and continue gracefully to complete all other players
if login_response["msg"] != "success":
print("Login not possible")
sys.exit(1)
print("Login not possible for player: " + player["original_name"] + " / " + player["id"] + " - validate their player ID. Skipping.")
counter_error += 1
continue

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

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

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

# Refresh the webpage every 5 players to avoid getting soft-banned at some point
if session_counter % 5 == 0:
time.sleep(5)

session_counter += 1

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

Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
certifi==2024.7.4
charset-normalizer==3.3.2
idna==3.7
requests==2.32.3
urllib3==2.2.2

0 comments on commit ba73853

Please sign in to comment.