-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
208 lines (167 loc) · 6.14 KB
/
api.py
File metadata and controls
208 lines (167 loc) · 6.14 KB
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
import itertools
import requests
from queue import Queue
import time
import threading
import os.path
from tqdm import tqdm
import cdsapi
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class cdsapi_p:
def __init__(self, keys, url="https://cds.climate.copernicus.eu/api/v2"):
self._url = url
self._params = Queue()
self._init_auths(keys)
def key2auth(self, key):
return tuple(key.split(":", 2))
def _init_auths(self, keys):
keys = set(keys)
auths = [self.key2auth(i) for i in keys]
self._nauth = len(auths)
self._auths = itertools.cycle(enumerate(auths))
def add(self, param):
self._params.put(param)
def count(self):
return self._params.qsize()
def clear(self):
self._params = Queue()
# while not self._params.empty():
# self._params.get_nowait()
def Poster(self, overwrite):
idx, auth = next(self._auths)
while not self._params.empty():
param = self._params.get()
name, request, outfile = param
if os.path.isfile(outfile) and not overwrite:
continue
url = f"{self._url}/resources/{name}"
resp = requests.post(url, json=request, auth=auth)
json = resp.json()
if resp.status_code == 200:
item = (json["location"], outfile)
self._qloc.put(item)
elif resp.status_code == 202:
item = (auth, json["request_id"], outfile)
self._tasks[idx].put(item)
idx, auth = next(self._auths)
else:
self._params.put(param)
for q in self._tasks:
q.put(self._sentinel)
def Checker(self, idx):
while True:
item = self._tasks[idx].get()
if item == self._sentinel:
with self.__lock:
self.__flag += 1
if self.__flag == self._nauth:
self._qloc.put(self._sentinel)
break
while True:
auth, request_id, outfile = item
url = f"{self._url}/tasks/{request_id}"
resp = requests.get(url, auth=auth)
json = resp.json()
if json["state"] == "completed":
item = (json["location"], outfile)
self._qloc.put(item)
break
else:
time.sleep(0.5)
def Downloader(self, pbar):
while True:
item = self._qloc.get()
if item == self._sentinel:
self._qloc.put(item)
break
location, outfile = item
with requests.get(location, stream=True) as r:
r.raise_for_status()
with open(outfile, "wb") as f:
for chunk in r.iter_content(chunk_size=1024):
f.write(chunk)
pbar.update(1)
pbar.set_postfix_str(f"lastest: {outfile}")
def run(self, overwrite=False, workers=None):
pbar = tqdm(total=self.count(), desc="Download")
if workers is None or workers > self._auths:
workers = self._nauth
self._qloc = Queue()
self._tasks = [Queue() for _ in range(self._nauth)]
self._sentinel = object()
self.__lock = threading.Lock()
self.__flag = 0
# create threads
d_threads = []
for _ in range(workers):
d_thread = threading.Thread(target=self.Downloader, args=(pbar,))
d_threads.append(d_thread)
d_thread.start()
c_threads = []
for i in range(self._nauth):
c_thread = threading.Thread(target=self.Checker, args=(i,))
c_threads.append(c_thread)
c_thread.start()
p_thread = threading.Thread(target=self.Poster, args=(overwrite,))
p_thread.start()
# join threads
for d_thread in d_threads:
d_thread.join()
for c_thread in c_threads:
c_thread.join()
p_thread.join()
class cdsapi_s:
def __init__(self, keys, url="https://cds.climate.copernicus.eu/api/v2"):
self._url = url
self._params = Queue()
self.keys = keys
def add(self, func, *args):
self._params.put((func, *args))
def count(self):
return self._params.qsize()
def clear(self):
self._params = Queue()
# while not self._params.empty():
# self._params.get_nowait()
def worker(self, key, pbar):
c = cdsapi.Client(url=self._url, key=key, quiet=True, delete=True, verify=False)
while True:
item = self._params.get()
func, *args = item
func = getattr(c, func)
try:
func(*args)
print(key)
except Exception as e:
print("e")
with self.__lock:
self.__error.append(e)
self.__flag += 1
pbar.set_postfix_str(
f"threads: {len(self.keys) - self.__flag}, lastest: {key} died"
)
if self.__flag == len(self.keys):
print(self.__error)
os._exit(1)
# self._params.task_done()
# for _ in range(self.count()):
# self._params.get()
# self._params.task_done()
# sys.exit(1)
self._params.put(item)
break
pbar.update(1)
print("test")
pbar.set_postfix_str(
f"threads: {len(self.keys) - self.__flag}, lastest: {args[-1]}"
)
self._params.task_done()
def run(self, **kwargs):
self.__flag = 0
self.__lock = threading.Lock()
self.__error = []
pbar = tqdm(total=self.count(), desc="Download")
for key in self.keys:
threading.Thread(target=self.worker, args=(key, pbar), daemon=True).start()
self._params.join()