-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathget_language_bundle.py
More file actions
50 lines (38 loc) · 1.77 KB
/
Copy pathget_language_bundle.py
File metadata and controls
50 lines (38 loc) · 1.77 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
# coding=utf-8
"""
get_language_bundle.py
Sample script for retrieving a specific language bundle using the async client.
"""
import asyncio
import base64
import io
import zipfile
from swgoh_comlink import SwgohComlinkAsync
async def main():
# Create a SwgohComlinkAsync instance
async with SwgohComlinkAsync() as cl:
# Retrieve the German language strings (Default is to collect data as a base64 encoded compressed string
german_language_bundle = await cl.get_localization(locale="ger_de")
# First decode the base64 string into the compressed object
german_language_bundle_decoded = base64.b64decode(german_language_bundle['localizationBundle'])
# Next create a ZipFile handler to interact with the compressed data object
zip_obj = zipfile.ZipFile(io.BytesIO(german_language_bundle_decoded))
"""
The compressed object will contain two elements. These can be seen using the namelist() method
of the zipfile package.
>>> zip_obj.namelist()
['Loc_GER_DE.txt', 'Loc_Key_Mapping.txt']
>>>
"""
# Extract the string information, decode it and split into individual lines
der_obj = zip_obj.read('Loc_GER_DE.txt')
der_obj_decoded = der_obj.decode('utf-8')
der_obj_lines = der_obj_decoded.splitlines()
# Each line has a key value and the corresponding text separated by a pipe ('|')
# To create a dictionary for easy mapping of strings from IDs used in game data collections,
# loop through the lines, split on the '|' character and store the information in an empty dict
der_str_lookup = {}
for line in der_obj_lines:
key, value = line.split('|')
der_str_lookup[key] = value
asyncio.run(main())