Skip to content

Commit

Permalink
Download economy config
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnapalm committed Jul 16, 2024
1 parent eb32038 commit 746c793
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ To obtain your current profile:
* Ensure zoffline is disabled.
* Run ``scripts/get_profile.py -u <your_zwift_username>``
* Or, if using the Windows zoffline.exe version without Python installed you can run ``get_profile.exe`` obtained from https://github.com/zoffline/zwift-offline/releases/tag/zoffline_helper in place of ``scripts/get_profile.py``
* Move the resulting ``profile.bin`` and ``achievements.bin`` (saved in whatever directory you ran get_profile.py in) into the ``storage/1`` directory.
* Move the resulting ``profile.bin``, ``achievements.bin`` and ``economy_config.txt`` (saved in whatever directory you ran get_profile.py in) into the ``storage/1`` directory.
* If using zoffline.exe on Windows, create a ``storage/1`` directory within the same folder as zoffline.exe if it does not already exist.
* If using Docker, the directory ``1`` should be in the path you passed to ``-v``

Expand Down
23 changes: 22 additions & 1 deletion online_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def post_credentials(session, username, password):
print('Invalid uname and/or password')


def query(session, access_token, route):
def get(session, access_token, route):
try:
response = session.get(
url="https://us-or-rly101.zwift.com/%s" % route,
Expand All @@ -87,6 +87,27 @@ def query(session, access_token, route):
print('HTTP Request failed: %s' % e)


def post(session, access_token, route):
try:
response = session.post(
url="https://us-or-rly101.zwift.com/%s" % route,
headers={
"Content-Type": "application/x-protobuf-lite",
"Accept": "application/x-protobuf-lite",
"Connection": "keep-alive",
"Host": "us-or-rly101.zwift.com",
"User-Agent": "Zwift/115 CFNetwork/758.0.2 Darwin/15.0.0",
"Authorization": "Bearer %s" % access_token,
"Accept-Language": "en-us",
},
)

return response.content

except requests.exceptions.RequestException as e:
print('HTTP Request failed: %s' % e)


def logout(session, refresh_token):
# Logout
# POST https://secure.zwift.com/auth/realms/zwift/tokens/logout
Expand Down
39 changes: 36 additions & 3 deletions scripts/get_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import os
import requests
import sys
sys.path.insert(0, '../protobuf')
import login_pb2
from google.protobuf.json_format import MessageToDict


if getattr(sys, 'frozen', False):
Expand Down Expand Up @@ -92,7 +95,7 @@ def post_credentials(session, username, password):
exit(-1)


def query(session, access_token, route):
def get(session, access_token, route):
try:
response = session.get(
url="https://us-or-rly101.zwift.com/%s" % route,
Expand All @@ -118,6 +121,32 @@ def query(session, access_token, route):
print('HTTP Request failed: %s' % e)


def post(session, access_token, route):
try:
response = session.post(
url="https://us-or-rly101.zwift.com/%s" % route,
headers={
"Content-Type": "application/x-protobuf-lite",
"Accept": "application/x-protobuf-lite",
"Connection": "keep-alive",
"Host": "us-or-rly101.zwift.com",
"User-Agent": "Zwift/115 CFNetwork/758.0.2 Darwin/15.0.0",
"Authorization": "Bearer %s" % access_token,
"Accept-Language": "en-us",
},
verify=args.verifyCert,
)

if args.verbose:
print('Response HTTP Status Code: {status_code}'.format(
status_code=response.status_code))

return response.content

except requests.exceptions.RequestException as e:
print('HTTP Request failed: %s' % e)


def logout(session, refresh_token):
# Logout
# POST https://secure.zwift.com/auth/realms/zwift/tokens/logout
Expand Down Expand Up @@ -197,12 +226,16 @@ def main(argv):
session = requests.session()

access_token, refresh_token = login(session, username, password)
profile = query(session, access_token, "api/profiles/me")
profile = get(session, access_token, "api/profiles/me")
with open('%s/profile.bin' % SCRIPT_DIR, 'wb') as f:
f.write(profile)
achievements = query(session, access_token, "achievement/loadPlayerAchievements")
achievements = get(session, access_token, "achievement/loadPlayerAchievements")
with open('%s/achievements.bin' % SCRIPT_DIR, 'wb') as f:
f.write(achievements)
login_response = login_pb2.LoginResponse()
login_response.ParseFromString(post(session, access_token, "api/users/login"))
with open('%s/economy_config.txt' % SCRIPT_DIR, 'w') as f:
json.dump(MessageToDict(login_response, preserving_proto_field_name=True)['economy_config'], f, indent=2)

logout(session, refresh_token)

Expand Down
2 changes: 1 addition & 1 deletion scripts/get_profile.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import sys
sys.modules['FixTk'] = None

a = Analysis(['get_profile.py'],
pathex=[],
pathex=['../protobuf'],
binaries=[],
datas=[],
hiddenimports=[],
Expand Down
10 changes: 8 additions & 2 deletions zwift_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,13 +845,19 @@ def profile(username):
access_token, refresh_token = online_sync.login(session, username, password)
try:
if request.form.get("zwift_profile"):
profile = online_sync.query(session, access_token, "api/profiles/me")
profile = online_sync.get(session, access_token, "api/profiles/me")
profile_file = '%s/profile.bin' % profile_dir
backup_file(profile_file)
with open(profile_file, 'wb') as f:
f.write(profile)
login_response = login_pb2.LoginResponse()
login_response.ParseFromString(online_sync.post(session, access_token, "api/users/login"))
economy_config_file = '%s/economy_config.txt' % profile_dir
backup_file(economy_config_file)
with open(economy_config_file, 'w') as f:
json.dump(MessageToDict(login_response, preserving_proto_field_name=True)['economy_config'], f, indent=2)
if request.form.get("achievements"):
achievements = online_sync.query(session, access_token, "achievement/loadPlayerAchievements")
achievements = online_sync.get(session, access_token, "achievement/loadPlayerAchievements")
achievements_file = '%s/achievements.bin' % profile_dir
backup_file(achievements_file)
with open(achievements_file, 'wb') as f:
Expand Down

0 comments on commit 746c793

Please sign in to comment.