Created
October 9, 2017 00:43
-
-
Save vpzed/35d05f6b2abeffec596ac7bf8953c9ba to your computer and use it in GitHub Desktop.
Get manifest files from destiny.plumbing
This file contains hidden or 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 | |
""" | |
TEST code for using destiny.plumbing | |
""" | |
import os | |
import sys | |
import requests | |
def main(): | |
""" Download Destiny 2 Manifest information from destiny.plumbing""" | |
# Set initial destiny.plumbing D2 Mnaifest URL | |
index_url = 'https://destiny.plumbing/2/index.json' | |
# Get index.json | |
res1 = requests.get(index_url, timeout=10) | |
if not res1.status_code == 200: | |
sys.exit('Error connecting to destiny.plumbing D2 index, exiting.') | |
else: | |
jres1 = res1.json() | |
# Test response | |
if not jres1['id']: | |
sys.exit('Error could not find destiny.plumbing D2 index id, exiting') | |
else: | |
# If we made it this far then we should have information to proceed | |
man_id = jres1['id'] | |
man_links = jres1['en']['raw'] | |
# If we've already grabbed this version (identified by id) there should be a directory already | |
# If not then we should get these new files | |
if not os.path.isdir(man_id): | |
# Try to make a new directory | |
try: | |
os.mkdir(man_id) | |
except OSError: | |
print('Error making new directory') | |
else: | |
# Try to cd into the new directory | |
try: | |
os.chdir(man_id) | |
except OSError: | |
print('Error changing to new directory') | |
else: | |
# Try to write the index file | |
try: | |
index_file = open('index.json', 'wb') | |
except IOError: | |
print('Error opening new index.json file') | |
else: | |
with index_file: | |
index_file.write(res1.content) | |
print('Writing index file') | |
# Work through each link in the en.raw list and download those files. | |
for table_name, url in man_links.items(): | |
res2 = requests.get(url, timeout=10) | |
if not res2.status_code == 200: | |
print('Error downloading destiny.plumbing table file.') | |
else: | |
# Build new shorter table name from original | |
new_name = table_name.replace('Definition', '').replace('Destiny', 'm_').lower() | |
# Try to write the json data to a file with the new name | |
try: | |
table_file = open(f'{new_name}.json', 'wb') | |
except IOError: | |
print('Error opening table file, {}.json'.format(new_name)) | |
else: | |
with table_file: | |
table_file.write(res2.content) | |
print('Writing file for {}'.format(new_name)) | |
else: | |
print('Manifest files already current.') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment