forked from yalov/CommNetAntennasInfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_spacedock_utils.py
229 lines (185 loc) · 7.32 KB
/
release_spacedock_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""utils for accessing to the spacedock"""
# Public domain license.
# Based on: https://github.com/ihsoft/KSPDev_ReleaseBuilder
# $version: 3
import json
import urllib.request
import urllib.error
import urllib.parse
import io
import random
import string
import ntpath
# Endpoint for all the API requests
API_BASE_URL = 'https://spacedock.info'
# The actions paths.
API_AUTHORIZE = '/api/login'
API_UPDATE_MOD_TMPL = '/api/mod/{mod_id}/update'
# API_GET_VERSIONS = '/api/kspversions'
API_GET_VERSIONS = '/api/3102/versions'
API_GET_MOD = '/api/mod/{mod_id}'
# The authorization cookie. It's only created once. To refresh it, simply
# set it to None.
authorized_cookie = None
def PublishToSpacedock(mod_id, filepath, changelog, mod_version, game_version, login, password):
"""
Args:
mod_id: The mod ID to update.
filepath: A full or relative path to the local file.
changelog: The change log content.
mod_version: The version of the mod being published.
game_version: The KSP version to publish for.
Returns:
The response object.
"""
headers, data = EncodeFormData([
{'name': 'version', 'data': mod_version},
{'name': 'changelog', 'data': changelog},
{'name': 'game-version', 'data': game_version},
{'name': 'notify-followers', 'data': 'yes'},
{'name': 'zipball', 'filename': filepath},
])
url, headers = _GetAuthorizedEndpoint(
API_UPDATE_MOD_TMPL, headers, login, password, mod_id=mod_id)
resp = _CallAPI(url, data=data, headers=headers)
print('success.')
print('The new version is now available and the followers are notified!')
return resp
def GetSpacedockModDetails(mod_id):
"""Gets the mod informnation.
This call does NOT require authorization.
Args:
mod_id: The mod to request.
Returns:
The response object.
"""
url = _MakeAPIUrl(API_GET_MOD, mod_id=mod_id)
response_obj, _ = _CallAPI(url, None, None)
return response_obj
def GetSpacedockKSPVersions():
"""Gets the available versions of the game.
This call does NOT require authorization.
Returns:
A list of objects: { 'name': <KSP name>, 'id': <Spacedock ID> }
"""
response = _CallAPI(_MakeAPIUrl(API_GET_VERSIONS), None, None)
versions = [{'name': x['friendly_version'], 'id': x['id']} for x in response[0]]
return versions
def _MakeAPIUrl(action_path, **kwargs):
"""Makes a URL for the action."""
return API_BASE_URL + action_path.format(**kwargs)
def _CallAPI(url, data, headers, raise_on_error=True):
"""Invokes the API call."""
resp_obj = {'error': True, 'reason': 'unknown'}
try:
request = urllib.request.Request(url, data, headers=headers or {})
response = urllib.request.urlopen(request)
resp_obj = json.loads(response.read())
headers = response.info()
except urllib.error.HTTPError as ex:
resp_obj = {'error': True, 'reason': '%d - %s' % (ex.code, ex.reason)}
try:
resp_obj = json.loads(ex.read())
except Exception:
pass # Not a JSON response
if ex.code == 401:
print("AuthorizationRequiredError")
if type(resp_obj) is dict and resp_obj.get('error'):
if raise_on_error:
print("BadResponseError")
return resp_obj, None
return resp_obj, headers
def _GetAuthorizedEndpoint(api_path, headers, login, password, **kwargs):
"""Gets API URL and the authorization headers.
The login/password must be set in the global variables API_LOGIN/API_PASS.
"""
global authorized_cookie
url = _MakeAPIUrl(api_path, **kwargs)
if not headers:
headers = {}
if not authorized_cookie:
if not login or not password:
print("BadCredentialsError: API_LOGIN and/or API_PASS not set")
exit(0)
auth_headers, data = EncodeFormData([
{'name': 'username', 'data': login},
{'name': 'password', 'data': password},
])
resp, auth_headers = _CallAPI(
API_BASE_URL + API_AUTHORIZE, data, auth_headers,
raise_on_error=False)
if resp['error']:
print("BadCredentialsError")
exit(0)
authorized_cookie = auth_headers['set-cookie']
headers['Cookie'] = authorized_cookie
return url, headers
# Utils: Provides helpers to deal with the multipart/form-data MIME types.
def EncodeFormData(fields):
"""Encodes the provided arguments as the web form fields.
The argument must be an array of objects that define the fields to be passed:
- 'name': A required property than defines the name of the web form field.
- 'data': The raw data to pass. If it's of type 'string', then it's passed as
'text/plain'. Otherwise, it's serialized as JSON and passed as
'application/json'.
- 'filename': An optional property that designates a path to the binary file
to tansfer. The 'data' field is ignored in this case, and the real data is
read from the file. The data is passed as 'application/octet-stream', and
server will receive the file name part of the path. The part can be
relative, in which case it's counted realtive to the main module, or it can
be absolute.
Example:
[
{ 'name': 'metadata', 'data': metadatObj },
{ 'name': 'file', 'filename': '/home/me/releases/MyMod_v1.0.zip' }
]
@param fields: An array of the fields to encode.
"""
boundry = '----WebKitFormBoundary' + IdGenerator(16)
data_stream = io.BytesIO()
for field in fields:
filename = field.get('filename')
if filename:
with open(filename, 'rb') as f:
data = f.read()
content_type = 'application/octet-stream'
filename = ntpath.basename(filename)
else:
data = field['data']
if type(data) != str:
content_type = 'application/json'
data = json.dumps(data)
else:
content_type = 'text/plain; charset=utf-8'
data = data.encode('utf-8')
_WriteFormData(data_stream, boundry,
field['name'], content_type, data, filename)
data_stream.write(b'--')
data_stream.write(boundry.encode())
data_stream.write(b'--')
data_stream.write(b'\r\n')
headers = {
'Content-Type': 'multipart/form-data; boundary=%s' % boundry,
'Content-Length': str(data_stream.tell()),
}
return headers, data_stream.getvalue()
def IdGenerator(size, chars=string.ascii_letters + string.digits):
"""Makes a unique random string of the requested size."""
return ''.join(random.choice(chars) for _ in range(size))
def _WriteFormData(stream, boundry, name, content_type, data, filename=None):
"""Helper method to write a single web form field."""
stream.write(b'--')
stream.write(boundry.encode())
stream.write(b'\r\n')
if filename:
stream.write(('Content-Disposition: form-data; name="%s"; filename="%s"' %
(name, filename)).encode())
else:
stream.write(('Content-Disposition: form-data; name="%s"' %
(name)).encode())
stream.write(b'\r\n')
stream.write(('Content-Type: %s' % content_type).encode())
stream.write(b'\r\n')
stream.write(b'\r\n')
stream.write(data)
stream.write(b'\r\n')