Skip to content

Instantly share code, notes, and snippets.

@akej74
Created October 28, 2017 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akej74/b7933ff389158ace24abcbb9a7237dbb to your computer and use it in GitHub Desktop.
Save akej74/b7933ff389158ace24abcbb9a7237dbb to your computer and use it in GitHub Desktop.
SpaceDock API test
import requests
import time
# How many mods to get per page from SpaceDock API
MODS_PER_PAGE = 100
def test_spacedock_api():
"""Requests SpaceDock for all mods using the API and returns a dictionary with the data in JSON format."""
# Initial request to SpaceDock API
req = "https://spacedock.info/api/browse?count=" + str(MODS_PER_PAGE)
# Empty dictionary to store the raw mod data in JSON format
mod_data = {}
# Get the first page of mods
print("Getting first page with request:", req)
response = requests.get(req)
# Store the first page of mod data in the dictionary
mod_data[1] = response
# Decode JSON data that will be used to check how many sub pages there are
spacedock_data = response.json()
# Number of pages as returned from SpaceDock API
pages = spacedock_data["pages"]
print("Pages to get:", pages)
# Request SpaceDock for each page available and store the result in the dictionary
# Start from page 2 as the first page has already been retrieved
for page in range(2, pages + 1):
# Create the URL to fetch each page
req = "https://spacedock.info/api/browse?page=" + str(page) + "&count=" + str(MODS_PER_PAGE)
# Fetch page
print('Getting page:', page)
mod_data[page] = requests.get(req)
# Empty dictionary to store mod name and id
mod_details = {}
# Get all mods from each sub-page and store them in the dict
for key, value in mod_data.items():
page = value.json()
for mod in page['result']:
mod_name = mod['name']
mod_id = mod['id']
# Update dict
mod_details[mod_name] = [mod_id]
#print(sorted(mod_details.keys(), key=str.lower))
return mod_details
def write_to_disk(filename, data):
"""Write mod name and mod id to a text file."""
with open(filename, "w") as f:
for key in sorted(data.keys(), key=str.lower):
row = str(key) + ' ' + str(data[key])
f.write('{}\n'.format(row))
print('Writing file', filename)
if __name__ == "__main__":
# How many times to parse SpaceDock
test_runs = 3
for i in range(test_runs):
print('##########################')
print('Test run:', i)
filename = 'spacedock_test_' + str(i) + '.txt'
mod_details = test_spacedock_api()
write_to_disk(filename, mod_details)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment