Skip to content

Instantly share code, notes, and snippets.

@ThyReaper
Created May 30, 2016 03:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ThyReaper/89fb434d413f45a3c033407410ffdfaa to your computer and use it in GitHub Desktop.
Save ThyReaper/89fb434d413f45a3c033407410ffdfaa to your computer and use it in GitHub Desktop.
Python script to upload code to Screeps
import urllib.request;
import json;
api = "https://screeps.com/api/user/code"
#Load account data
keys = open("key.txt");
account = keys.readline().strip("\n")
apikey = keys.readline().strip("\n")
print("\""+account+"\"")
print("\""+apikey+"\"")
#Setup authentication
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, api, account, apikey)
auth_handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
urllib.request.install_opener( urllib.request.build_opener(auth_handler) )
#Load scripts to be submitted to the server
data = {"branch": "api",
"modules": {
"main": open("main.js").read()
}
}
request = urllib.request.Request(api)
request.add_header("Content-Type", "application/json; charset=utf-8")
post = json.dumps(data).encode()
try:
response = urllib.request.urlopen(request, data).read().decode('utf-8')
print(response)
except urllib.error.HTTPError as err:
print("Error " + str(err.code) + " in http request: " + err.reason)
input("Press enter")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment