blizzard-wow-game-data-apis/updater.py

88 lines
3.9 KiB
Python

import os
import errno
import requests
import json
"""
https://develop.battle.net/documentation/guides/regionality-and-apis
"""
regions = [
{'code': 'us', 'locales': ['en_US', 'es_MX', 'pt_BR']},
{'code': 'eu', 'locales': ['en_GB', 'es_ES', 'fr_FR', 'ru_RU', 'de_DE', 'pt_PT', 'it_IT']},
{'code': 'kr', 'locales': ['ko_KR']},
{'code': 'tw', 'locales': ['zh_TW']},
#{'code': 'cn', 'locales': ['zh_CN']}
]
"""
https://develop.battle.net/documentation/world-of-warcraft/game-data-apis
"""
apis = [
{'path': '/data/wow/achievement-category/index', 'namespaces': ['static']},
{'path': '/data/wow/achievement/index', 'namespaces': ['static']},
{'path': '/data/wow/connected-realm/index', 'namespaces': ['dynamic']},
{'path': '/data/wow/covenant/index', 'namespaces': ['static']},
{'path': '/data/wow/covenant/soulbind/index', 'namespaces': ['static']},
{'path': '/data/wow/covenant/conduit/index', 'namespaces': ['static']},
{'path': '/data/wow/creature-family/index', 'namespaces': ['static']},
{'path': '/data/wow/creature-type/index', 'namespaces': ['static']},
{'path': '/data/wow/item-class/index', 'namespaces': ['static']},
{'path': '/data/wow/item-set/index', 'namespaces': ['static']},
{'path': '/data/wow/journal-expansion/index', 'namespaces': ['static']},
{'path': '/data/wow/journal-encounter/index', 'namespaces': ['static']},
{'path': '/data/wow/journal-instance/index', 'namespaces': ['static']},
{'path': '/data/wow/modified-crafting/index', 'namespaces': ['static']},
{'path': '/data/wow/modified-crafting/category/index', 'namespaces': ['static']},
{'path': '/data/wow/modified-crafting/reagent-slot-type/index', 'namespaces': ['static']},
{'path': '/data/wow/mount/index', 'namespaces': ['static']},
{'path': '/data/wow/keystone-affix/index', 'namespaces': ['static']},
{'path': '/data/wow/mythic-keystone/dungeon/index', 'namespaces': ['dynamic']},
{'path': '/data/wow/mythic-keystone/index', 'namespaces': ['dynamic']},
{'path': '/data/wow/mythic-keystone/period/index', 'namespaces': ['dynamic']},
]
credentials = open('.credentials').read().strip().split(':')
cache = './cache'
def getApiHost(region):
return f"{region}.api.blizzard.com" if region != 'cn' else 'gateway.battlenet.com.cn'
def getBnetHost(region):
region = region if region not in ['tw', 'kr'] else 'apac'
return f"{region}.battle.net" if region != 'cn' else 'www.battlenet.com.cn'
s = requests.Session()
for region in regions:
try:
r = s.post(f"https://{getBnetHost(region['code'])}/oauth/token", data={'grant_type': 'client_credentials'}, auth=(credentials[0], credentials[1]))
oauth = r.json()
#print(region, oauth)
r.raise_for_status()
except requests.exceptions.HTTPError as e:
print(region, e)
continue
for api in apis:
for locale in region['locales']:
for namespace in api['namespaces']:
try:
url = f"https://{getApiHost(region['code'])}{api['path']}?namespace={namespace}-{region['code']}&locale={locale}&access_token={oauth['access_token']}"
r = s.get(url) #, headers={'Authorization': f"Authorization: Token {oauth['access_token']}"})
#print(r.request.headers, r.status_code, r.reason, r.headers, url)
print(region['code'], locale, r.status_code, api['path'])
r.raise_for_status()
dst = f"{cache}/{region['code']}/{locale}/{api['path'].replace('/', '_')}.{namespace}.json"
try:
os.makedirs(os.path.dirname(dst))
except OSError as e:
if e.errno != errno.EEXIST:
raise
open(dst, 'w+').write(json.dumps(r.json(), indent=4))
except requests.exceptions.HTTPError as e:
print(e)
continue