-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.py
69 lines (52 loc) · 1.66 KB
/
publish.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
import sys
import requests
import click
from pathlib import Path
import logging
MOD_PORTAL_URL = "https://mods.factorio.com"
INIT_UPLOAD_URL = f"{MOD_PORTAL_URL}/api/v2/mods/releases/init_upload"
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
logger.addHandler(handler)
def valid_package_dir(ctx, param, value):
path = Path(value)
if not path.exists():
raise click.BadParameter(
f"Package directory must be an existing folder, did not find <{str(path)}>"
)
return path
@click.command()
@click.option(
"--zipfilepath",
"-z",
required=True,
help="Path where the zipped mod directory is located",
callback=valid_package_dir,
)
@click.option(
"--api_key",
"-a",
required=True,
help="API-KEY to use when authenticating against the upload server",
)
def publish(zipfilepath, api_key):
logger.info(f"Preparing publish for the following file: {zipfilepath}")
request_body = {"mod": "time-split"}
request_headers = {"Authorization": f"Bearer {api_key}"}
response = requests.post(
INIT_UPLOAD_URL, data=request_body, headers=request_headers
)
if not response.ok:
logger.error(f"init_upload failed: {response.text}")
sys.exit(1)
upload_url = response.json()["upload_url"]
with open(zipfilepath, "rb") as f:
request_body = {"file": f}
response = requests.post(upload_url, files=request_body)
if not response.ok:
logger.error(f"upload failed: {response.text}")
sys.exit(1)
logger.info(f"upload successful: {response.text}")
if __name__ == "__main__":
publish()