This repository was archived by the owner on Feb 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebstore.py
More file actions
executable file
·364 lines (303 loc) · 13.9 KB
/
Webstore.py
File metadata and controls
executable file
·364 lines (303 loc) · 13.9 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
def Get(Cfg = None):
import json
import time
import requests
if __name__ == '__main__':
from Merge import MergeCfg
from Aliyun import __AliyunClient__, __AliyunEndPoint__
else:
from .Merge import MergeCfg
from .Aliyun import __AliyunClient__, __AliyunEndPoint__
Config = {
'Web' : True,
'Key' : None, # 记录の键, '' OR ['', '', ...]
'Space' : 'sample-storage', # 记录の域
'AK' : 'AccessKey Id',
'SK' : 'AccessKey Secret',
'STSToken': '', # 临时性凭证 (可留空)
'Region' : 'cn-hangzhou' # 服务接入点 (可留空)
}
Config = MergeCfg(Config, Cfg)
Response = {
'ErrorCode': 0,
'ErrorMsg' : '',
'Data' : {}
}
if Config['Web']:
try:
Url = 'http://storage.edge-routine.yudongling.net.w.cdngslb.com/'
Hed = {
'Content-Type': 'application/json',
'Host' : 'storage.edge-routine.yudongling.net'
}
Dat = {
'action' : 'get',
'namespace': Config['Space'],
'key' : Config['Key'] if isinstance(Config['Key'], list) else [Config['Key']]
}
Rsp = requests.post(Url, headers = Hed, data = json.dumps(Dat), timeout = 5).json()
for Key, Value in Rsp['Data'].items():
try: Rsp['Data'][Key]['value'] = json.loads(Value['value'])
except: pass
return Rsp
except Exception as errorMsg:
Response['ErrorCode'] = -1
Response['ErrorMsg'] = ''
return Response
if not Config['Web']:
import asyncio
from alibabacloud_tea_util import models as UtilModels
from alibabacloud_tea_openapi import models as OpenApiModels
from alibabacloud_openapi_util.client import Client as OpenApiUtilClient
# 限制并发请求数
MaxReq = 16; Semaphore = asyncio.Semaphore(MaxReq)
async def __AsyncReq__(Key, Client, Params, Runtime):
Key = str(Key)
if len(Key) > 512:
return Key, {'code': 400, 'value': ''}
try:
async with Semaphore:
Request = OpenApiModels.OpenApiRequest(query=OpenApiUtilClient.query({
'Namespace': Config['Space'],
'Key' : Key
}))
Result = await Client.call_api_async(Params, Request, Runtime)
Exp, Val = Result['body']['Value'].split('|', 1); Exp = int(Exp)
if Exp != -1 and Exp < time.time(): return Key, {'code': 404, 'value': ''}
try: return Key, {'code': 0, 'value': json.loads(Val)}
except: return Key, {'code': 0, 'value': Val}
except Exception as errorMsg:
if errorMsg.data.get('Code') in ['InvalidKey.NotFound']:
return Key, {'code': 404, 'value': ''}
else:
return Key, {'code': 500, 'value': ''}
async def __AsyncMain__():
try:
Client = __AliyunClient__(AK = Config['AK'], SK = Config['SK'], STSToken = Config['STSToken'], EndPoint = __AliyunEndPoint__(Config['Region'], 'Dcdn'))
Params = OpenApiModels.Params(
action = 'GetDcdnKv',
version = '2018-01-15',
protocol = 'HTTP',
method = 'GET',
auth_type = 'AK',
style = 'RPC',
pathname = '/',
req_body_type = 'json',
body_type = 'json'
)
Runtime = UtilModels.RuntimeOptions(autoretry = True, max_attempts = 3, read_timeout = 10000, connect_timeout = 10000)
except Exception as errorMsg:
Response['ErrorCode'] = -1
Response['ErrorMsg'] = ''
return Response
Key = Config['Key'] if isinstance(Config['Key'], list) else [Config['Key']]
Result = await asyncio.gather(*[__AsyncReq__(_Key, Client, Params, Runtime) for _Key in Key])
for Key, Data in Result: Response['Data'][Key] = Data
return Response
return asyncio.run(__AsyncMain__())
def Put(Cfg = None):
import json
import time
import requests
if __name__ == '__main__':
from Merge import MergeCfg
from Aliyun import __AliyunClient__, __AliyunEndPoint__
else:
from .Merge import MergeCfg
from .Aliyun import __AliyunClient__, __AliyunEndPoint__
Config = {
'Web' : True,
'Key' : [], # 记录の键值对, Eg. { Key: 'Key', Value: 'Value', Ttl: 0, Expire: 1700000000 }
'Space' : 'sample-storage', # 记录の域
'AK' : 'AccessKey Id',
'SK' : 'AccessKey Secret',
'STSToken': '', # 临时性凭证 (可留空)
'Region' : 'cn-hangzhou' # 服务接入点 (可留空)
}
Config = MergeCfg(Config, Cfg)
Response = {
'ErrorCode': 0,
'ErrorMsg' : '',
'Data' : {}
}
if Config['Web']:
Body = []
for _ in Config['Key']:
Info = { 'key' : _['Key'] }
if isinstance(_['Value'], str):
Info['value'] = _['Value']
elif isinstance(_['Value'], dict) or isinstance(_['Value'], list):
Info['value'] = json.dumps(_['Value'], ensure_ascii = False)
else:
Info['value'] = str(_['Value'])
if 'Ttl' in _ and isinstance(_['Ttl'] , int): Info['ttl'] = _['Ttl']
if 'Expire' in _ and isinstance(_['Expire'], int): Info['expire'] = _['Expire']
Body.append(Info)
try:
Url = 'http://storage.edge-routine.yudongling.net.w.cdngslb.com/'
Hed = {
'Content-Type': 'application/json',
'Host' : 'storage.edge-routine.yudongling.net'
}
Dat = {
'action' : 'put',
'namespace': Config['Space'],
'key' : Body
}
return requests.post(Url, headers = Hed, data = json.dumps(Dat), timeout = 5).json()
except Exception as errorMsg:
Response['ErrorCode'] = -1
Response['ErrorMsg'] = ''
return Response
if not Config['Web']:
import asyncio
from alibabacloud_tea_util import models as UtilModels
from alibabacloud_tea_openapi import models as OpenApiModels
from alibabacloud_openapi_util.client import Client as OpenApiUtilClient
# 限制并发请求数
MaxReq = 16; Semaphore = asyncio.Semaphore(MaxReq)
async def __AsyncReq__(KeyVal, Client, Params, Runtime):
Key = str(KeyVal['Key'])
if len(Key) > 512:
return Key, {'code': 400 }
Expire = -1
if 'Expire' in KeyVal and isinstance(KeyVal['Expire'], int) and KeyVal['Expire'] > 0:
Expire = KeyVal['Expire']
elif 'Ttl' in KeyVal and isinstance(KeyVal['Ttl'] , int) and KeyVal['Ttl'] > 0:
Expire = int(time.time() + KeyVal['Ttl'])
if isinstance(KeyVal['Value'], str):
Value = KeyVal['Value']
elif isinstance(KeyVal['Value'], dict) or isinstance(KeyVal['Value'], list):
Value = json.dumps(KeyVal['Value'], ensure_ascii = False)
else:
Value = str(KeyVal['Value'])
try:
async with Semaphore:
if Expire == -1:
Request = OpenApiModels.OpenApiRequest(query=OpenApiUtilClient.query({
'Namespace': Config['Space'],
'Key' : Key
}), body = {'Value': f'{Expire}|{Value}'})
else:
Request = OpenApiModels.OpenApiRequest(query=OpenApiUtilClient.query({
'Namespace' : Config['Space'],
'Key' : Key,
'Expiration': Expire + 30
}), body = {'Value': f'{Expire}|{Value}'})
await Client.call_api_async(Params, Request, Runtime)
return Key, {'code': 0 }
except Exception as errorMsg:
return Key, {'code': 500 }
async def __AsyncMain__():
try:
Client = __AliyunClient__(AK = Config['AK'], SK = Config['SK'], STSToken = Config['STSToken'], EndPoint = __AliyunEndPoint__(Config['Region'], 'Dcdn'))
Params = OpenApiModels.Params(
action = 'PutDcdnKv',
version = '2018-01-15',
protocol = 'HTTP',
method = 'POST',
auth_type = 'AK',
style = 'RPC',
pathname = '/',
req_body_type = 'formData',
body_type = 'json'
)
Runtime = UtilModels.RuntimeOptions(autoretry = True, max_attempts = 3, read_timeout = 10000, connect_timeout = 10000)
except Exception as errorMsg:
Response['ErrorCode'] = -1
Response['ErrorMsg'] = ''
return Response
KeyVal = Config['Key'] if isinstance(Config['Key'], list) else [Config['Key']]
Result = await asyncio.gather(*[__AsyncReq__(_KeyVal, Client, Params, Runtime) for _KeyVal in KeyVal])
for Key, Data in Result: Response['Data'][Key] = Data
return Response
return asyncio.run(__AsyncMain__())
def Delete(Cfg = None):
import json
import requests
if __name__ == '__main__':
from Merge import MergeCfg
from Aliyun import __AliyunClient__, __AliyunEndPoint__
else:
from .Merge import MergeCfg
from .Aliyun import __AliyunClient__, __AliyunEndPoint__
Config = {
'Web' : True,
'Key' : None, # 记录の键, '' OR ['', '', ...]
'Space' : 'sample-storage', # 记录の域
'AK' : 'AccessKey Id',
'SK' : 'AccessKey Secret',
'STSToken': '', # 临时性凭证 (可留空)
'Region' : 'cn-hangzhou' # 服务接入点 (可留空)
}
Config = MergeCfg(Config, Cfg)
Response = {
'ErrorCode': 0,
'ErrorMsg' : '',
'Data' : {}
}
if Config['Web']:
try:
Url = 'http://storage.edge-routine.yudongling.net.w.cdngslb.com/'
Hed = {
'Content-Type': 'application/json',
'Host' : 'storage.edge-routine.yudongling.net'
}
Dat = {
'action' : 'delete',
'namespace': Config['Space'],
'key' : Config['Key'] if isinstance(Config['Key'], list) else [Config['Key']]
}
return requests.post(Url, headers = Hed, data = json.dumps(Dat), timeout = 5).json()
except Exception as errorMsg:
Response['ErrorCode'] = -1
Response['ErrorMsg'] = ''
return Response
if not Config['Web']:
import asyncio
from alibabacloud_tea_util import models as UtilModels
from alibabacloud_tea_openapi import models as OpenApiModels
from alibabacloud_openapi_util.client import Client as OpenApiUtilClient
# 限制并发请求数
MaxReq = 16; Semaphore = asyncio.Semaphore(MaxReq)
async def __AsyncReq__(Key, Client, Params, Runtime):
Key = str(Key)
if len(Key) > 512:
return Key, {'code': 400 }
try:
async with Semaphore:
Request = OpenApiModels.OpenApiRequest(query=OpenApiUtilClient.query({
'Namespace': Config['Space'],
'Key' : Key
}))
await Client.call_api_async(Params, Request, Runtime)
return Key, {'code': 0 }
except Exception as errorMsg:
if errorMsg.data.get('Code') in ['InvalidAccount.NotFound', 'InvalidNameSpace.NotFound', 'InvalidKey.NotFound']:
return Key, {'code': 404 }
else:
return Key, {'code': 500 }
async def __AsyncMain__():
try:
Client = __AliyunClient__(AK = Config['AK'], SK = Config['SK'], STSToken = Config['STSToken'], EndPoint = __AliyunEndPoint__(Config['Region'], 'Dcdn'))
Params = OpenApiModels.Params(
action = 'DeleteDcdnKv',
version = '2018-01-15',
protocol = 'HTTP',
method = 'POST',
auth_type = 'AK',
style = 'RPC',
pathname = '/',
req_body_type = 'json',
body_type = 'json'
)
Runtime = UtilModels.RuntimeOptions(autoretry = True, max_attempts = 3, read_timeout = 10000, connect_timeout = 10000)
except Exception as errorMsg:
Response['ErrorCode'] = -1
Response['ErrorMsg'] = ''
return Response
Key = Config['Key'] if isinstance(Config['Key'], list) else [Config['Key']]
Result = await asyncio.gather(*[__AsyncReq__(_Key, Client, Params, Runtime) for _Key in Key])
for Key, Data in Result: Response['Data'][Key] = Data
return Response
return asyncio.run(__AsyncMain__())