-
-
Save j000/83935debde7f93276130a66105481eca to your computer and use it in GitHub Desktop.
Python script to upload code to Screeps
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
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