forked from blue-llama/e-treasure-hunt
-
Notifications
You must be signed in to change notification settings - Fork 4
/
upload.py
executable file
·161 lines (127 loc) · 4.78 KB
/
upload.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
#!/usr/bin/env python3
from __future__ import annotations
import asyncio
import json
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING
import aiohttp
if TYPE_CHECKING:
from collections.abc import Iterable
HINTS_PER_LEVEL = 5
SERVER = "http://localhost:8000"
USERNAME = "admin"
PASSWORD = "adminpasswordhere" # noqa: S105
CONTENT_TYPES = {
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
}
@dataclass
class Level:
number: int
path: Path
class Uploader:
def __init__(self, session: aiohttp.ClientSession) -> None:
self.session = session
async def upload_level_without_hints(self, level: Level) -> None:
"""
Upload a level, without creating any of its hints.
:param level: The level to upload.
"""
about = level.path / "about.json"
with about.open(encoding="utf-8") as f:
data = json.load(f)
data["number"] = level.number
# It's OK for there to be no blurb, but we report it in case that wasn't what
# was intended.
blurb = level.path / "blurb.txt"
try:
with blurb.open(encoding="utf-8") as f:
data["description"] = f.read()
except FileNotFoundError:
print(f"No blurb at level {level.number}")
data["description"] = ""
url = f"{SERVER}/api/levels/{level.number}"
auth = aiohttp.BasicAuth(USERNAME, PASSWORD)
async with self.session.put(url, auth=auth, json=data) as r:
if not r.ok:
print(f"Error uploading level {level.number}")
print(await r.text())
r.raise_for_status()
async def upload_hint(self, level: Level, hint: int, image: Path) -> None:
"""
Upload a hint.
:param level: The level that the hint belongs to. The level must already exist.
:param hint: The hint number. Zero-indexed.
:param image: The file containing the hint.
"""
suffix = image.suffix
content_type = CONTENT_TYPES.get(suffix.lower())
if content_type is None:
msg = f"unrecognized suffix: {suffix}"
raise RuntimeError(msg)
with image.open("rb") as f:
url = f"{SERVER}/api/levels/{level.number}/hint"
auth = aiohttp.BasicAuth(USERNAME, PASSWORD)
data = aiohttp.FormData()
data.add_field(
"file",
f,
filename=image.name,
content_type=content_type,
)
payload = {"number": hint}
data.add_field(
"data",
json.dumps(payload),
content_type="application/json",
)
async with self.session.post(url, auth=auth, data=data) as r:
if not r.ok:
print(f"Error uploading level {level.number} hint {hint}")
print(await r.text())
r.raise_for_status()
async def upload_level(self, level: Level) -> None:
"""
Upload a level from a directory, creating all of its hints.
:param level: The level to upload.
"""
print(f"Uploading level {level.number}")
# Find the images.
images = [
file
for file in level.path.iterdir()
if file.suffix.lower() in CONTENT_TYPES
]
# Should find exactly the right number - check the file extensions if not.
if len(images) != HINTS_PER_LEVEL:
msg = f"Found {len(images)} images in {level.path}"
raise RuntimeError(msg)
# Create the level.
await self.upload_level_without_hints(level)
# Upload the hints.
images.sort(key=lambda x: x.name.lower())
hint_uploads = [
self.upload_hint(level, hint, image) for hint, image in enumerate(images)
]
await asyncio.gather(*hint_uploads)
print(f"Uploaded level {level.number}")
async def upload_levels(self, levels: Iterable[Level]) -> None:
# Tried making this more parallel, the server didn't like it...
for level in levels:
await self.upload_level(level)
async def main() -> None:
# Uploads three levels, including the dummy levels 0 and 4.
levels = [
Level(0, Path("/directory/containing/dummy/level")),
Level(1, Path("/directory/containing/level/one")),
Level(2, Path("/directory/containing/level/two")),
Level(3, Path("/directory/containing/level/three")),
Level(4, Path("/directory/containing/dummy/level")),
]
async with aiohttp.ClientSession() as session:
uploader = Uploader(session)
await uploader.upload_levels(levels)
if __name__ == "__main__":
asyncio.run(main())