-
Notifications
You must be signed in to change notification settings - Fork 0
/
pybaidupan.py
executable file
·237 lines (214 loc) · 9.33 KB
/
pybaidupan.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
#!/usr/bin/env python3.3
# -*- coding:utf-8 -*-
###################################################
#A simple command-line client for Baidu Netdisk #
#author: Kather Lee #
#python version: 3.3 #
#required packages: #
# python-requests 1.2.3 #
###################################################
import requests, hashlib, json, argparse, os, sys
KEY = 'Pfb12nmBr971bAaB33ubfXbp'
SEC = '4ihI1zVf1tk2tjOEVUBxvQa35UZAf3xq'
APPNAME = 'PanLin'
auth_url = 'https://openapi.baidu.com/oauth/2.0/device/code'
token_url = 'https://openapi.baidu.com/oauth/2.0/token'
quota_url = 'https://pcs.baidu.com/rest/2.0/pcs/quota'
file_url = 'https://pcs.baidu.com/rest/2.0/pcs/file'
cloud_url = 'https://psc.baidu.com/rest/2.0/pcs/services/cloud_dl'
def oauth_get_code(appkey, appsec):
device_para = {'client_id' : appkey, 'response_type': 'device_code', 'scope': 'basic,netdisk'}
res = requests.post(auth_url, device_para)
device_json = res.json()
auth_err(device_json)
return device_json
def oauth_user_auth(url, user_code):
print('''Follow these steps to complete authentication:
1. Visit '''+ url + ''' in your browser;
2. Copy or input the following code when asked:
''' + user_code + '''
3. After granted access permission, hit Enter to continue.
''')
input('Hit [Enter] when finished... ')
return True
def oauth_get_token(appkey, appsec, device_code):
token_para = {'grant_type': 'device_token', 'code': device_code, 'client_id': appkey, 'client_secret': appsec}
res = requests.post(token_url, token_para)
token_json = res.json()
auth_err(token_json)
return {'access_token': token_json['access_token'], 'refresh_token': token_json['refresh_token']}
def oauth_refresh(appkey, appsec, refresh_token):
para = {'grant_type': 'refresh_token', 'refresh_token': refresh_token, 'client_id': appkey, 'client_secret': appsec}
res = requests.post(token_url, para)
res_json = res.json()
auth_err(res_json)
return {'access_token': res_json['access_token'], 'refresh_token': res_json['refresh_token']}
def get_quota(access_token):
para = {'method': 'info', 'access_token': access_token}
res = requests.get(quota_url,params = para)
res_json = res.json()
api_err(res_json)
return res_json
def list_file(access_token, r_path, by = None, order = None, limit = None):
para = {'access_token': access_token, 'method': 'list', 'path': r_path}
if by:
para['by'] = by
if order:
para['order'] = order
if limit:
para['limit'] = limit
res = requests.get(file_url,params = para)
res_json = res.json()
api_err(res_json)
return res_json
def make_dir(access_token, r_path):
para = {'access_token': access_token, 'method': 'mkdir', 'path': r_path}
res = requests.post(file_url, params = para)
res_json = res.json()
api_err(res_json)
return res_json
def file_info(access_token, r_path):
para = {'access_token': access_token, 'method': 'meta', 'path': r_path}
res = requests.get(file_url, params = para)
res_json = res.json()
api_err(res_json)
return res_json
def move(access_token, from_path, to_path):
para = {'access_token': access_token, 'method': 'move', 'from': from_path, 'to': to_path}
res = requests.post(file_url, params = para)
res_json = res.json()
api_err(res_json)
return res_json
def copy(access_token, from_path, to_path):
para = {'access_token': access_token, 'method': 'copy', 'from': from_path, 'to': to_path}
res = requests.post(file_url, params = para)
res_json = res.json()
api_err(res_json)
return res_json
def delete(access_token, r_path):
para = {'access_token': access_token, 'method': 'delete', 'path': r_path}
res = requests.get(file_url, params = para)
res_json = res.json()
api_err(res_json)
return res_json
def search(access_token, r_path, keyword, recursive = False):
para = {'access_token': access_token, 'method': 'search', 'path': r_path, 'wd': keyword}
if recursive:
para['re'] = '1'
res = requests.get(file_url, params = para)
res_json = res.json()
api_err(res_json)
return res_json
def cloud(access_token, save_path, source_url, expires = False, rate_limit = False, timeout = False, callback = False):
para = {'access_token': access_token, 'method': 'add_task', 'save_path': save_path, 'source_url': source_url}
if expires:
para['expires'] = expires
if rate_limit:
para['rate_limit'] = rate_limit
if timeout:
para['timeout'] = timeout
if callback:
para['callback'] = callback
res = requests.post(cloud_url, params = para)
res_json = res.json()
api_err(res_json)
return res_json
def download(access_token, r_path, l_path, chunksize = None):
para = {'access_token':access_token, 'method': 'download', 'path': r_path}
#To do: check if file exists
size = file_info(access_token, r_path)['list'][0]['size']
with open(l_path, 'wb') as f:
if chunksize:
pos = 0
print('0.0%', end = '')
while pos <= size:
headers = {'range':'bytes=' + str(pos) + '-' + str(pos + chunksize - 1)}
res= requests.get('https://d.pcs.baidu.com/rest/2.0/pcs/file', params = para, headers = headers)
length = int(res.headers['Content-Length'])
if pos <= size - chunksize and length < chunksize:
pass
#api_err(res.json())
f.write(res.content)
print('\r{:.1f}%'.format((pos + length)/size*100), end = '')
pos+= chunksize
print('\rDone')
else:
res = requests.get('https://d.pcs.baidu.com/rest/2.0/pcs/file', params = para)
f.write(res.content)
def upload_single(access_token, r_path, l_path, ondup = 'overwrite'):
para = {'access_token': access_token, 'path': r_path, 'method': 'upload', 'ondup': ondup}
with open(l_path, 'rb') as f:
res = requests.post('https://c.pcs.baidu.com/rest/2.0/pcs/file', params = para, files = {'file':f})
res_json = res.json()
api_err(res_json)
return res_json
def upload_multiple(access_token, r_path, l_path, chunksize = 134217728, ondup = 'overwrite'):
md5_list = []
with open(l_path, 'rb') as f:
while True:
chunk = f.read(chunksize)
para = {'access_token': access_token, 'method': 'upload', 'type': 'tmpfile'}
res = requests.post('https://c.pcs.baidu.com/rest/2.0/pcs/file', params = para, files = {'file':chunk})
res_json = res.json()
api_err(res_json)
md5_list.append(res_json['md5'])
if(len(chunk) < chunksize):
break
httpbody = 'param='+json.dumps({'block_list':md5_list})
para = {'access_token':access_token, 'method': 'createsuperfile', 'path': r_path, 'ondup': ondup}
res = requests.post(file_url, params = para, data = httpbody.encode('utf-8'))
res_json = res.json()
api_err(res_json)
return res_json
def api_err(res_json):
print(res_json)
def auth_err(auth_json):
print(auth_json)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--init', action = "store_true", help = "Initialization")
subparsers = parser.add_subparsers(title = 'commands', help = 'Usage', dest = 'command_name')
parser_upload = subparsers.add_parser('upload', help = 'Upload single file')
parser_upload.add_argument('LPATH', help = 'Local path')
parser_upload.add_argument('RPATH', help = 'Remote path')
parser_download = subparsers.add_parser('download', help = 'Download single file')
parser_download.add_argument('RPATH', help = 'Remote path')
parser_download.add_argument('LPATH', help = 'Local path')
parser_download.add_argument('--multipart', '-m', action = "store_true", help = 'Multipart download')
args = parser.parse_args()
actk = ''
if args.init:
if os.path.isfile('.pybaidupan'):
with open('.pybaidupan', 'r+') as f:
lastactk = f.readline()
refresh_token = f.readline().strip()
tokens = oauth_refresh(KEY, SEC, refresh_token)
actk = tokens['access_token']
f.seek(0)
f.truncate()
f.write(tokens['access_token'] + os.linesep + tokens['refresh_token'])
else:
codes = oauth_get_code(KEY, SEC)
if oauth_user_auth(codes['verification_url'], codes['user_code']):
tokens= oauth_get_token(KEY, SEC, codes['device_code'])
actk = tokens['access_token']
with open('.pybaidupan', 'w') as f:
f.write(tokens['access_token'] + os.linesep + tokens['refresh_token'])
else:
print('Error!')
sys.exit(1)
elif os.path.isfile('.pybaidupan'):
with open('.pybaidupan', 'r') as f:
actk = f.readline().strip()
else:
print('Error')
sys.exit(1)
if args.command_name=='upload':
upload_single(actk, args.RPATH, args.LPATH)
elif args.command_name=='download':
if args.multipart:
download(actk, args.RPATH, args.LPATH, chunksize = 1024 * 1024)
else:
download(actk, args.RPATH, args.LPATH)
if __name__ == '__main__':
main()