forked from Ridepad/uwu-logs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_upload.py
197 lines (164 loc) · 5.61 KB
/
s_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
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
# python s_upload.py file_name --server server_name --chunk-size 200000 --threads 1
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from pathlib import Path
import sys
import time
import requests
HOST_NAME_MAIN = "https://uwu-logs.xyz"
HOST_NAME_TEST = "http://localhost:4000"
def local_timezone_name():
timezone = time.altzone if time.daylight else time.timezone
offset = timezone // 3600
return f"Etc/GMT{offset:+}"
def send_post(d):
print("send_post", len(d["data"]), d["headers"])
q = requests.post(d["url"], data=d["data"], headers=d["headers"])
return (d["headers"]["X-Chunk"], q)
class Upload:
def __init__(
self,
file_name: str,
server: str=None,
threads=None,
chunk_size=None,
test=False,
) -> None:
self.file_name = file_name
self.file_path = Path(file_name)
if not self.file_path.is_file():
raise FileNotFoundError
if self.file_path == Path(__file__).resolve():
raise ValueError("Must include logs file as last argument")
self.server = server
try:
self.threads = int(threads)
except (TypeError, ValueError):
self.threads = 1
try:
self.chunk_size = int(chunk_size)
except (TypeError, ValueError):
self.chunk_size = 256*1024
if self.chunk_size > 100*1024*1024:
raise ValueError("Chunk size must not exceed 100 megabytes")
self.upload_id = str(datetime.now().timestamp())
self.host_name = HOST_NAME_MAIN
if test:
self.host_name = HOST_NAME_TEST
def run(self):
self.send_file()
self.send_finish()
for response_json in self.upload_progress():
print(response_json)
def send_file(self):
with ThreadPoolExecutor(self.threads) as t:
responses = t.map(send_post, self._chunks_gen())
for chunk_id, response in responses:
print(f"{chunk_id:>4} | {response.status_code} | {response.content}")
print(">>> Uploaded all chunks")
def send_finish(self):
headers = {
"Content-Type": "application/json",
}
request_json = {
"filename": self.file_path.name,
"server": self.server,
"timezone": local_timezone_name(),
"chunks": self.chunks_amount(),
}
response = requests.post(
f"{self.host_name}/upload",
json=request_json,
headers=headers,
)
print(f">>> Send finish {response.status_code} | {response.content}")
def upload_progress(self):
for _ in range(200):
response = requests.get(f"{self.host_name}/upload_progress")
if response.headers.get("Content-Type") != "application/json":
break
if response.status_code != 200:
break
response_json = response.json() or {}
yield response_json
if response_json.get("done") in [1, "1"]:
break
time.sleep(1)
def chunks_amount(self):
chunks, _mod = divmod(self.full_size(), self.chunk_size)
if _mod:
chunks += 1
return chunks
def full_size(self):
return self.file_path.stat().st_size
def _chunks_gen(self):
max_chunks = self.chunks_amount()
with open(self.file_path, "rb") as f:
for chunk in range(max_chunks):
file_slice = f.read(self.chunk_size)
if not file_slice:
break
yield {
"url": f"{self.host_name}/upload",
"data": file_slice,
"headers": self._headers(chunk),
}
def _headers(self, chunk):
return {
"X-Chunk": str(chunk),
"X-Upload-ID": self.upload_id,
}
def argv_get_server(flags: list[str]):
for flag in ["-s", "--server"]:
try:
i = flags.index(flag) + 1
return flags[i]
except IndexError:
print(f"{flag} flag must be paired with a server name")
return
except ValueError:
pass
def argv_get_chunk_size(flags: list[str]):
for flag in ["-c", "--chunk-size"]:
try:
i = flags.index(flag) + 1
return flags[i]
except IndexError:
print(f"{flag} flag must be paired with a chunk size in bytes")
return
except ValueError:
pass
def argv_get_threads(flags: list[str]):
for flag in ["-t", "--threads"]:
try:
i = flags.index(flag) + 1
return flags[i]
except IndexError:
print(f"{flag} flag must be paired with threads amount")
return
except ValueError:
pass
def argv_upload_file_name(flags: list[str]):
for flag in flags[1:]:
p = Path(flag)
if p.is_file():
return flag
raise ValueError("Must include logs file name")
def main():
file_name = argv_upload_file_name(sys.argv)
server = argv_get_server(sys.argv)
threads = argv_get_threads(sys.argv)
chunk_size = argv_get_chunk_size(sys.argv)
upload = Upload(
file_name,
server=server,
threads=threads,
chunk_size=chunk_size,
)
upload.run()
def _test1():
file_name = r"F:\wow-3.3.5a-HD\Logs\logs_cache\24-05-14--21-17--Meownya--Lordaeron.7z"
upload = Upload(file_name, threads=16, test=1)
upload.run()
if __name__ == "__main__":
main()