-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathget_location_bundle.py
More file actions
75 lines (57 loc) · 3.05 KB
/
Copy pathget_location_bundle.py
File metadata and controls
75 lines (57 loc) · 3.05 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
"""
get_location_bundle.py
Script to illustrate the basic usage of the swgoh_comlink wrapper library
"""
# import the SwgohComlink class from the swgoh_comlink module
import base64
import io
import zipfile
from swgoh_comlink import SwgohComlink
# create an instance of a SwgohComlink object
comlink = SwgohComlink()
"""
# Call the get_location_bundle() method of the SwgohComlink instance to retrieve the language localization bundle.
"""
# Get the current game version
game_data_versions = comlink.get_latest_game_data_version()
# Get the language bundle using the latest game data language version
# By default, Comlink compresses the data and encodes it into a BASE64 string for smaller payload and faster delivery.
# The result is a string value that must be decoded before using.
location_bundle = comlink.get_localization_bundle(localization_id=game_data_versions["language"])
# Decode the Base64 result
loc_bundle_decoded = base64.b64decode(location_bundle["localizationBundle"])
# Create a zipfile object to access the compressed content
zip_obj = zipfile.ZipFile(io.BytesIO(loc_bundle_decoded))
# Get a list of language elements contained in the zip data
"""
Sample output:
['Loc_CHS_CN.txt', 'Loc_CHT_CN.txt', 'Loc_ENG_US.txt', 'Loc_FRE_FR.txt', 'Loc_GER_DE.txt', 'Loc_IND_ID.txt',
'Loc_ITA_IT.txt', 'Loc_JPN_JP.txt', 'Loc_Key_Mapping.txt', 'Loc_KOR_KR.txt', 'Loc_POR_BR.txt', 'Loc_RUS_RU.txt',
'Loc_SPA_XM.txt', 'Loc_THA_TH.txt', 'Loc_TUR_TR.txt']
"""
lang_files = zip_obj.namelist()
# Extract the English bundle for use. Note this returns a byte array. Decode if you want to work with it as a string.
# You could also use the various other zipfile methods to extra all files to disk
# (see https://docs.python.org/3/library/zipfile.html for more details), or loop through the namelist()
# output and select only specific languages you are interested in.
eng_obj = zip_obj.read("Loc_ENG_US.txt")
# Decode to string then split into individual lines
eng_obj_decoded = eng_obj.decode("utf-8")
eng_obj_lines = eng_obj_decoded.splitlines()
"""
Alternatively, if you elected to have Comlink send an unzipped response, the result is a dictionary containing keys
for all of the language files (similar to the namelist() output from the zipfile method above.
"""
location_bundle_unzipped = comlink.get_localization_bundle(localization_id=game_data_versions["language"], unzip=True)
"""
Each key of the result dictionary is a string that can be split into individual lines, or written to files.
>>> location_bundle_unzipped.keys()
dict_keys(['Loc_CHS_CN.txt', 'Loc_CHT_CN.txt', 'Loc_ENG_US.txt', 'Loc_FRE_FR.txt', 'Loc_GER_DE.txt', 'Loc_IND_ID.txt',
'Loc_ITA_IT.txt', 'Loc_JPN_JP.txt', 'Loc_Key_Mapping.txt', 'Loc_KOR_KR.txt', 'Loc_POR_BR.txt', 'Loc_RUS_RU.txt',
'Loc_SPA_XM.txt', 'Loc_THA_TH.txt', 'Loc_TUR_TR.txt'])
Depending on the speed of your connection and other resource factors, the time needed to retrieve the localization
data can be significantly higher for unzipped versus zipped.
Results from one sample test run:
Time for zipped: 2.82 seconds
Time for unzipped: 7.85 seconds
"""