Last active
August 15, 2020 14:42
-
-
Save buddhapest/27c3896595703d7d71652d889f487be5 to your computer and use it in GitHub Desktop.
combine output of new APIs to create an IEconItems_440/GetSchema style schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# generate the old IEconItems_440/GetSchema style as a file using the new APIs | |
# buddhapest 2018 | |
# | |
# pip install request | |
# pip install simplejson | |
# | |
# fill in your API key | |
# | |
# ./get_schema.py | |
# | |
# outputs schema.json file that can be consumed just like the old schema output | |
import os | |
import time | |
import requests | |
import simplejson as json | |
params = { | |
'key': '### your API key here ###', | |
'language': 'en', | |
} | |
# output filename | |
fname = 'schema.json' | |
headers = None | |
# if we already have a schema.json... | |
if os.path.isfile(fname): | |
# ... get its file system time and set a header for the request | |
headers = {'If-Modified-Since': time.ctime(os.path.getmtime(fname))} | |
r = requests.get('https://api.steampowered.com/IEconItems_440/GetSchemaOverview/v1/', | |
params=params, headers=headers) | |
# if 304 (Not Modified) was returned, short circuit - our schema.json is up to date | |
if r.status_code == requests.codes.not_modified: | |
exit() | |
# blow up if not 200 (OK) | |
if r.status_code != requests.codes.ok: | |
r.raise_for_status() | |
schema = r.json() | |
schema['result']['items'] = [] | |
params['start'] = 0 | |
# loop on GetSchemaItems until there's no "next" key | |
while True: | |
r = requests.get('https://api.steampowered.com/IEconItems_440/GetSchemaItems/v1/', params=params) | |
if r.status_code != requests.codes.ok: | |
r.raise_for_status() | |
result = r.json()['result'] | |
schema['result']['items'].extend(result['items']) | |
if 'next' in result: | |
params['start'] = result['next'] | |
else: | |
break | |
# write out our new schema | |
with open(fname, 'w') as fd: | |
json.dump(schema, fd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment