Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions sc2reader/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,8 +914,6 @@ def __getstate__(self):


class Map(Resource):
url_template = "https://{}-s2-depot.classic.blizzard.com/{}.s2ma"

def __init__(self, map_file, filename=None, region=None, map_hash=None, **options):
super(Map, self).__init__(map_file, filename, **options)

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

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

url_template = "https://{}-s2-depot.classic.blizzard.com/{}.s2gs"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this url_template unused, or am I not seeing the reference to it in the new code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unused.


#: Game speed
game_speed = str()

Expand Down Expand Up @@ -1450,10 +1444,6 @@ def __str__(self):
class MapHeader(Resource):
"""**Experimental**"""

base_url_template = "https://{}-s2-depot.classic.blizzard.com/{}.{}"
url_template = "https://{}-s2-depot.classic.blizzard.com/{}.s2mh"
image_url_template = "https://{}-s2-depot.classic.blizzard.com/{}.s2mv"

#: The name of the map
name = str()

Expand Down Expand Up @@ -1488,21 +1478,21 @@ def __init__(self, header_file, filename=None, **options):
# Parse image hash
parsed_hash = utils.parse_hash(self.data[0][1])
self.image_hash = parsed_hash["hash"]
self.image_url = self.image_url_template.format(
parsed_hash["server"], parsed_hash["hash"]
self.image_url = utils.get_resource_url(
parsed_hash["server"], parsed_hash["hash"], "s2mv"
)

# Parse map hash
parsed_hash = utils.parse_hash(self.data[0][2])
self.map_hash = parsed_hash["hash"]
self.map_url = self.base_url_template.format(
self.map_url = utils.get_resource_url(
parsed_hash["server"], parsed_hash["hash"], parsed_hash["type"]
)

# Parse localization hashes
l18n_struct = self.data[0][4][8]
for l in l18n_struct:
parsed_hash = utils.parse_hash(l[1][0])
self.localization_urls[l[0]] = self.base_url_template.format(
self.localization_urls[l[0]] = utils.get_resource_url(
parsed_hash["server"], parsed_hash["hash"], parsed_hash["type"]
)
32 changes: 15 additions & 17 deletions sc2reader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,9 @@ class DepotFile(object):
and assembles them into a URL so that the dependency can be fetched.
"""

#: The url template for all DepotFiles
url_template = "https://{}-s2-depot.classic.blizzard.com{}/{}.{}"

def __init__(self, bytes):
#: The server the file is hosted on
self.server = bytes[4:8].decode("utf-8").strip("\x00 ")

# Used to make it possible to load maps from CN.
# This isn't needed for any other region and so is blank by default.
self.url_suffix = ""

# There is no SEA depot, use US instead
if self.server == "SEA":
self.server = "US"
elif self.server == "CN":
self.url_suffix = ".cn"
self.server = bytes[4:8].decode("utf-8").strip("\x00 ").lower()

#: The unique content based hash of the file
self.hash = binascii.b2a_hex(bytes[8:]).decode("utf8")
Expand All @@ -45,9 +32,7 @@ def __init__(self, bytes):
@property
def url(self):
"""Returns url of the depot file."""
return self.url_template.format(
self.server, self.url_suffix, self.hash, self.type
)
return get_resource_url(self.server, self.hash, self.type)

def __hash__(self):
return hash(self.url)
Expand Down Expand Up @@ -208,6 +193,19 @@ def get_files(
depth -= 1


def get_resource_url(region, hash, type):
url_template = "{}://{}-s2-depot.{}/{}.{}"
scheme = "https"
domain = "classic.blizzard.com"

if region == "sea":
region = "us"
elif region == "cn":
scheme = "http"
domain = "battlenet.com.cn"
return url_template.format(scheme, region, domain, hash, type)


class Length(timedelta):
"""
Extends the builtin timedelta class. See python docs for more info on
Expand Down