updated codebase
This commit is contained in:
parent
ab54e91bad
commit
14190bea74
|
@ -1 +1,2 @@
|
||||||
.credentials
|
.credentials
|
||||||
|
/__pycache__/
|
||||||
|
|
129
updater.py
129
updater.py
|
@ -3,85 +3,76 @@ import os
|
||||||
import errno
|
import errno
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
import datetime
|
||||||
|
|
||||||
"""
|
import config
|
||||||
https://develop.battle.net/documentation/guides/regionality-and-apis
|
import blizzard
|
||||||
"""
|
import spec
|
||||||
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']}
|
|
||||||
]
|
|
||||||
|
|
||||||
"""
|
def log(*data):
|
||||||
https://develop.battle.net/documentation/world-of-warcraft/game-data-apis
|
print(datetime.datetime.now(), '|', *data)
|
||||||
"""
|
|
||||||
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(':')
|
class Updater(object):
|
||||||
cache = './cache'
|
def __init__(self):
|
||||||
|
self.http = requests.Session()
|
||||||
|
self.credentials = blizzard.get_credentials(config.pwd)
|
||||||
|
|
||||||
def getApiHost(region):
|
def region_oauth(self, region: str) -> dict:
|
||||||
return f"{region}.api.blizzard.com" if region != 'cn' else 'gateway.battlenet.com.cn'
|
api = self.http.post(f"{blizzard.get_bnet_host(region)}/oauth/token", data={'grant_type': 'client_credentials'}, auth=self.credentials)
|
||||||
|
oauth = api.json()
|
||||||
|
#log(region, oauth)
|
||||||
|
api.raise_for_status()
|
||||||
|
return oauth
|
||||||
|
|
||||||
def getBnetHost(region):
|
def api_call(self, url: str) -> requests.Response:
|
||||||
region = region if region not in ['tw', 'kr'] else 'apac'
|
api = self.http.get(url) #, headers={'Authorization': f"Authorization: Token {access_token}"})
|
||||||
return f"{region}.battle.net" if region != 'cn' else 'www.battlenet.com.cn'
|
api.raise_for_status()
|
||||||
|
return api
|
||||||
|
|
||||||
s = requests.Session()
|
def create_dst(self, dst: str):
|
||||||
|
|
||||||
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:
|
try:
|
||||||
os.makedirs(os.path.dirname(dst))
|
os.makedirs(os.path.dirname(dst))
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno != errno.EEXIST:
|
if e.errno != errno.EEXIST:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
open(dst, 'w+').write(json.dumps(r.json(), indent=4))
|
def save(self, dst, content):
|
||||||
except requests.exceptions.HTTPError as e:
|
with open(dst, 'w+') as f:
|
||||||
print(e)
|
f.write(json.dumps(content.json(), indent=4))
|
||||||
|
|
||||||
|
def iterate_index(self):
|
||||||
|
for region in spec.regions:
|
||||||
|
# access token for region
|
||||||
|
try:
|
||||||
|
oauth = self.region_oauth(region['code'])
|
||||||
|
access_token = oauth['access_token']
|
||||||
|
except (requests.exceptions.HTTPError, KeyError) as e:
|
||||||
|
log(region, type(e), e)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# loop every api
|
||||||
|
for api in spec.apis:
|
||||||
|
if not 'index' in api or not api['index']:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for locale in region['locales']:
|
||||||
|
# retail or classic
|
||||||
|
for namespace in api['namespaces']:
|
||||||
|
try:
|
||||||
|
dst = f"{config.raw}/{region['code']}/{locale}/{api['path'].replace('/', '_')}.{namespace}.json"
|
||||||
|
|
||||||
|
url = f"{blizzard.get_api_host(region['code'])}{api['path']}"
|
||||||
|
url += f"?namespace={namespace}-{region['code']}&locale={locale}&access_token={access_token}"
|
||||||
|
response = self.api_call(url)
|
||||||
|
|
||||||
|
self.create_dst(dst)
|
||||||
|
self.save(dst, response)
|
||||||
|
|
||||||
|
except requests.exceptions.HTTPError as e:
|
||||||
|
log(e)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
updater = Updater()
|
||||||
|
updater.iterate_index()
|
||||||
|
#updater.iterate_links()
|
||||||
|
|
Loading…
Reference in New Issue