Skip to content

Commit c50e91a

Browse files
committed
Add utils.get_resource_url to get resource url
1 parent 7bc551e commit c50e91a

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

sc2reader/resources.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,6 @@ def __getstate__(self):
914914

915915

916916
class Map(Resource):
917-
url_template = "https://{}-s2-depot.classic.blizzard.com/{}.s2ma"
918-
919917
def __init__(self, map_file, filename=None, region=None, map_hash=None, **options):
920918
super(Map, self).__init__(map_file, filename, **options)
921919

@@ -998,9 +996,7 @@ def __init__(self, map_file, filename=None, region=None, map_hash=None, **option
998996
def get_url(cls, region, map_hash):
999997
"""Builds a download URL for the map from its components."""
1000998
if region and map_hash:
1001-
# it seems like sea maps are stored on us depots.
1002-
region = "us" if region == "sea" else region
1003-
return cls.url_template.format(region, map_hash)
999+
return utils.get_resource_url(region, hash, "s2ma")
10041000
else:
10051001
return None
10061002

@@ -1021,8 +1017,6 @@ class GameSummary(Resource):
10211017
that the data is not necessarily in the places we expect.
10221018
"""
10231019

1024-
url_template = "https://{}-s2-depot.classic.blizzard.com/{}.s2gs"
1025-
10261020
#: Game speed
10271021
game_speed = str()
10281022

@@ -1450,10 +1444,6 @@ def __str__(self):
14501444
class MapHeader(Resource):
14511445
"""**Experimental**"""
14521446

1453-
base_url_template = "https://{}-s2-depot.classic.blizzard.com/{}.{}"
1454-
url_template = "https://{}-s2-depot.classic.blizzard.com/{}.s2mh"
1455-
image_url_template = "https://{}-s2-depot.classic.blizzard.com/{}.s2mv"
1456-
14571447
#: The name of the map
14581448
name = str()
14591449

@@ -1488,21 +1478,21 @@ def __init__(self, header_file, filename=None, **options):
14881478
# Parse image hash
14891479
parsed_hash = utils.parse_hash(self.data[0][1])
14901480
self.image_hash = parsed_hash["hash"]
1491-
self.image_url = self.image_url_template.format(
1492-
parsed_hash["server"], parsed_hash["hash"]
1481+
self.image_url = utils.get_resource_url(
1482+
parsed_hash["server"], parsed_hash["hash"], "s2mv"
14931483
)
14941484

14951485
# Parse map hash
14961486
parsed_hash = utils.parse_hash(self.data[0][2])
14971487
self.map_hash = parsed_hash["hash"]
1498-
self.map_url = self.base_url_template.format(
1488+
self.map_url = utils.get_resource_url(
14991489
parsed_hash["server"], parsed_hash["hash"], parsed_hash["type"]
15001490
)
15011491

15021492
# Parse localization hashes
15031493
l18n_struct = self.data[0][4][8]
15041494
for l in l18n_struct:
15051495
parsed_hash = utils.parse_hash(l[1][0])
1506-
self.localization_urls[l[0]] = self.base_url_template.format(
1496+
self.localization_urls[l[0]] = utils.get_resource_url(
15071497
parsed_hash["server"], parsed_hash["hash"], parsed_hash["type"]
15081498
)

sc2reader/utils.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,9 @@ class DepotFile(object):
1919
and assembles them into a URL so that the dependency can be fetched.
2020
"""
2121

22-
#: The url template for all DepotFiles
23-
url_template = "{}://{}-s2-depot.{}/{}.{}"
24-
2522
def __init__(self, bytes):
2623
#: The server the file is hosted on
27-
self.server = bytes[4:8].decode("utf-8").strip("\x00 ")
28-
29-
# Used to make it possible to load maps from CN.
30-
# This isn't needed for any other region and so is blank by default.
31-
self.scheme = "https"
32-
self.domain = "classic.blizzard.com"
33-
34-
# There is no SEA depot, use US instead
35-
if self.server == "SEA":
36-
self.server = "US"
37-
elif self.server == "CN":
38-
self.scheme = "http"
39-
self.domain = "battlenet.com.cn"
24+
self.server = bytes[4:8].decode("utf-8").strip("\x00 ").lower()
4025

4126
#: The unique content based hash of the file
4227
self.hash = binascii.b2a_hex(bytes[8:]).decode("utf8")
@@ -47,9 +32,7 @@ def __init__(self, bytes):
4732
@property
4833
def url(self):
4934
"""Returns url of the depot file."""
50-
return self.url_template.format(
51-
self.scheme, self.server, self.domain, self.hash, self.type
52-
)
35+
return get_resource_url(self.server, self.hash, self.type)
5336

5437
def __hash__(self):
5538
return hash(self.url)
@@ -210,6 +193,19 @@ def get_files(
210193
depth -= 1
211194

212195

196+
def get_resource_url(region, hash, type):
197+
url_template = "{}://{}-s2-depot.{}/{}.{}"
198+
scheme = "https"
199+
domain = "classic.blizzard.com"
200+
201+
if region == "sea":
202+
region = "us"
203+
elif region == "cn":
204+
scheme = "http"
205+
domain = "battlenet.com.cn"
206+
return url_template.format(scheme, region, domain, hash, type)
207+
208+
213209
class Length(timedelta):
214210
"""
215211
Extends the builtin timedelta class. See python docs for more info on

0 commit comments

Comments
 (0)