-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy path189disk.py
372 lines (301 loc) · 13 KB
/
189disk.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
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
365
366
367
368
369
370
371
372
'''
Created on 2016年4月17日
189开放云使用接口
@author: heguofeng
'''
from time import gmtime
import time
import requests
import rsa
import re
import json
import binascii
import base64
from urllib.parse import urlparse
import urllib
import hashlib
import hmac
class OAuth2(object):
ACCESS_TOKEN_URL = "https://oauth.api.189.cn/emp/oauth2/v3/access_token"
AUTHORIZE_URL = "https://oauth.api.189.cn/emp/oauth2/v3/authorize"
AccessToken=""
def __init__(self,app_key, app_secret, call_back_url):
self.version = 1.0
self.app_key = app_key
self.app_secret = app_secret
self.call_back_url = call_back_url
#display = default|mobile|popup
def authorize(self,response_type = "code",display = "page",state = ""):
querystring = {"app_id":self.app_key,
"app_secret":self.app_secret,
"redirect_uri":self.call_back_url,
"response_type":response_type,
"display":display}
if len(state) > 0:
querystring["state"] = state
#return OAuth2.AUTHORIZE_URL + "?" + urllib.urlencode(data)
return querystring
#grant_type = authorization_code|refresh_token
def access_token(self,grant_type = "authorization_code",code = "",refresh_token = ""):
querystring = {"app_id":self.app_key,
"app_secret":self.app_secret,
"grant_type":grant_type}
if grant_type == "authorization_code":
querystring["code"] = code
querystring["redirect_uri"] = self.call_back_url
elif grant_type == "refresh_token":
querystring["refresh_token"] = refresh_token
try:
response = requests.get(OAuth2.ACCESS_TOKEN_URL,params=querystring)
return response.json()
except requests.exceptions:
return requests.exceptions
'''
#autologin
1. get authorizeform
2.
'''
def autologin(self,username,password):
authorform=self.getAuthorizeform()
r=requests.get(authorform["url"],allow_redirects=False)
print(r.headers)
location=r.headers['Location']
print(location)
r=requests.get(location)
print(r.headers)
headers = {'content-type':'application/x-www-form-urlencoded',
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Referer":location,
"Accept-Language":"zh-cn",
"User-Agent":"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; MyIE9; BTRS123646; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)",
"Accept-Encoding": "gzip, deflate",
"Connection": "Keep-Alive",
"Host": "open.e.189.cn",
"Cookie":"[email protected]"
}
payload={"userName":"[email protected]",
"password":"hgf020613",
"accountType":"",
"appId":"tyopen",
"mail":"@189.cn",
"ValidateCode":""}
r=requests.post(location,allow_redirects=False,params=payload)
print(r.url)
print(r.headers)
location=r.headers['Location']
print(location)
query=urlparse(location).query
querydict=urllib.parse.parse_qs(query)
print(querydict)
print(querydict["redirect_uri"][0])
headers = {'content-type':'application/x-www-form-urlencoded',
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Referer":location,
"Accept-Language":"zh-cn",
"User-Agent":"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; MyIE9; BTRS123646; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)",
"Accept-Encoding": "gzip, deflate",
"Connection": "Keep-Alive",
"Host": "open.e.189.cn",
"Cookie":"[email protected]"
}
payload={"paras":querydict["paras"],
"appId":querydict["appId"],
"sign":querydict["sign"],
}
r=requests.get(querydict["redirect_uri"][0],allow_redirects=False,headers=headers,params=payload)
print(r.url)
print(r.headers)
location=r.headers['Location']
print(location)
#print(re.search(r'(?<=\?)[\S]*', location))
query=urlparse(location).query
querydict=urllib.parse.parse_qs(query)
print(querydict)
print(querydict["access_token"][0])
self.AccessToken=querydict["access_token"][0]
return(self.AccessToken)
def getAuthorizeform(self):
authorizeformdata={}
#print(self.authorize("token"))
response=requests.get(self.AUTHORIZE_URL,params=self.authorize("token"),allow_redirects=False)
print(response.request.url)
#print(response.request.headers)
html=response.text
#print(html)
match=re.findall(r'[a-zA-z]+://[\S]*udb[^\s|"]*',html)
print(match)
authorizeformdata["url"]=match[0]
return authorizeformdata
class vDiskClient(object):
API_URL = 'https://api.weipan.cn/2/'
AccessToken = ""
AppId="711690470000250948"
AppSecret="68a6d3dd1605f108d627472951f25ef7"
def __init__(self,token=""):
self.setAccessToken(token)
if self.AccessToken!="":
self.metadata()
def setAccessToken(self,token):
#self.AccessToken = "fe34416661iDpiR2XCKhG1HxVLR4166e"
if token!="":
self.AccessToken = token
return True
return False
def getUserInfo(self):
url="http://api.189.cn/ChinaTelecom/getUserInfo.action"
payload={"access_token":self.AccessToken,
"app_id":self.AppId,
}
headers = {'content-type':'application/json',"Accept":"application/json"}
r=requests.get(url,params=payload,headers=headers)
return(r.json())
def metadata(self):
url="http://api.189.cn/ChinaTelecom/getFolderInfo.action"
payload={"access_token":self.AccessToken,
"app_id":self.AppId,
}
headers = {'content-type':'application/json',"Accept":"application/json"}
r=requests.get(url,params=payload,headers=headers)
self.folderId=r.json()["id"]
self.folderPath=r.json()["path"]
return(r.json())
def listfiles(self):
url="http://api.189.cn/ChinaTelecom/listFiles.action"
payload={"access_token":self.AccessToken,
"app_id":self.AppId,
"orderBy":"filename",
"pageNum":"1",
"pageSize":"20"
}
headers = {'content-type':'application/json',"Accept":"application/json"}
r=requests.get(url,params=payload,headers=headers)
return(r.json())
def getfileid(self,filename):
url="http://api.189.cn/ChinaTelecom/getFileInfo.action"
payload={"access_token":self.AccessToken,
"app_id":self.AppId,
"filePath":filename,
}
headers = {'content-type':'application/json',"Accept":"application/json"}
r=requests.get(url,params=payload,headers=headers)
rjson=r.json()
print(r.json())
fileid=rjson["id"]
return(rjson["id"])
def getfile(self,files):
#GET http://api.189.cn/ChinaTelecom/getFileInfo.action
#files = {"file":(cloudfilename,fopenhandle)}
#1\get fileid
#2\get downloadurl
#3\download file
data=self.getFiletoData(files["file"][0])
#print(r.headers)
file=files["file"][1]
len=file.write(data.encode())
file.close()
return len
def getFiletoData(self,filename):
fileid=self.getfileid(filename)
url="http://api.189.cn/ChinaTelecom/getFileDownloadUrl.action"
querystring={"access_token":self.AccessToken,
"app_id":self.AppId,
"fileId":fileid,
}
headers = {'content-type':'application/json',"Accept":"application/json"}
r=requests.get(url,params=querystring,headers=headers)
#print(r.json())
filedownloadurl=r.json()["fileDownloadUrl"]
print(filedownloadurl)
r=requests.get(filedownloadurl)
return r.text
def putfile(self,files):
#PUT http://upload-vdisk.sina.com.cn/2/files_put/<root>/<path>
r=self.putFileFromData(files["file"][0], files["file"][1].read())
return(r)
def putFileFromData(self,filename,data):
'''
#1\get upload url
#headers - >> PUT /uploadFile.action?param=eqiIe9A1C7jnEv2E3&app_id=12437572&access_token=HCabctl7NpZcgk7MT8OVbSnQy317 HTTP/1.1
headers - >> Host: upload.cloud.189.cn
headers - >> Edrive-ParentFolderId: 1234
headers - >> Edrive-FileName: test.txt
headers - >> Edrive-FileMD5: 9999999999999999999999999999999
headers - >> Content-Length: 1024
AccessToken
'''
url="http://api.189.cn/ChinaTelecom/getFileUploadUrl.action"
#url="http://api.cloud.189.cn/getUserInfo.action"
querystring={"access_token":self.AccessToken,
"app_id":self.AppId,
}
headers = {'content-type':'application/json',"Accept":"application/json"}
r=requests.get(url,params=querystring,headers=headers)
#print(r.json())
fileUploadUrl=r.json()["FileUploadUrl"]
print(fileUploadUrl)
querystring={"access_token":self.AccessToken,
"app_id":self.AppId,
}
#b64=base64.encodebytes(data)
files = {'file': (filename,data)}
curdate=time.strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime())
print(curdate)
message="AccessToken="+self.AccessToken+"&Operate=PUT&RequestURI=/uploadFile.action"
message+="&Date="+curdate
print(message)
signature=self.signature(message)
md5=hashlib.md5(data).hexdigest()
size=len(data)
headers = {"Edrive-FileLength":size,
"Edrive-FileName":"safedata.txt",
"Edrive-ParentFolderId":self.folderId,
"Edrive-FileMD5":md5,
"Host": "upload.cloud.189.cn",
"AccessToken":self.AccessToken,
"app_id":self.AppId,
"Signature":signature,
"Date":curdate
#"Content-Length":1024
}
fileUploadUrl="http://upload.cloud.189.cn/uploadFile.action"
r=requests.put(fileUploadUrl,headers=headers,files=files)
print(r.request.url)
print(r.request.headers)
print(r.text)
return(r.json())
def signature(self,message):
#message="AccessToken=AFED506D70B5047B4B1BA87CDACF1082&Operate=GET&RequestURI=/getUserInfo.action&Date=Tue, 03 Dec 2013 08:57:48 GMT"
#AppSecret="ebc0a751e9154e5e0b0cd5a13d4bca9b"
Signature=hmac.new(str.encode(self.AppSecret),str.encode(message),hashlib.sha1).hexdigest()
print(Signature)
return Signature
if __name__ == '__main__':
AppSecret="68a6d3dd1605f108d627472951f25ef7"
AppId="711690470000250948"
oauth2=OAuth2(AppId,AppSecret,"http://127.0.0.1/")
accesstoken=oauth2.autologin("[email protected]", "hgf020613")
vdc=vDiskClient()
vdc.setAccessToken(accesstoken)
print(vdc.getUserInfo())
print(vdc.metadata())
print(vdc.listfiles())
#ct,b=vdc.encode_multipart_formdata({"data":"date"}, [])
#print(ct,b)
#print(vdc.createFolder("safebox9"))
files={"file":("safedata",open("C://Users//heguofeng//Downloads//t.html", 'rb'))}
print(vdc.putfile(files))
#s1=file.read()
#file.close()
#files = {'file': ("safedata9",base64.encodebytes(s1))}
#print(vdc.putfile(files))
#print(vdc.getfile("safedata8"))
#vdc.putFileFromData("safedata10", s1)
files={"file":("//我的应用//天翼开放平台应用//数据宝//test.txt",open("C://Users//heguofeng//Downloads//t.txt", 'wb'))}
vdc.getfile(files)
#s=vdc.getFiletoData("safedata10")
#s2=base64.decodebytes(s.encode())
#print(len(s),s)
#file.write(s.encode())
#file.close()
#vdc.postfile()
pass