forked from logbird/360yunpan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirYunPan.py
242 lines (210 loc) · 7.05 KB
/
dirYunPan.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
# -*- coding: utf8 -*-
"""
目录下载 文件下载
360yunpan - 360YunPan Command-line tools, support: Linux Mac Windows
Licensed under the MIT license:
http://www.opensource.org/licenses/mit-license.php
Project home:
https://github.com/logbird/360yunpan
Version: 1.0.0
@Author [email protected]
"""
import sys
import urllib
import urllib2
import cookielib
import time
import random
import hashlib
import json
import re
import os
reload(sys)
sys.setdefaultencoding("utf-8")
import utilsYunPan
from loginYunPan import loginYunPan
class dirYunPan:
serverAddr = None
pathYunPan = None
directoryTree = None
def __init__(self, pathYunPan, serverAddr):
'''构造函数
pathYunPan String 本地云盘的绝对路径
serverAddr String 登陆成功后 loginYunPan的serverAddr属性
'''
self.serverAddr = serverAddr
self.pathYunPan = pathYunPan
self.mkdir(pathYunPan)
def df(self):
'''获取网盘剩余空间
'''
url = self.serverAddr + "/user/getsize/?t=1377829621254&ajax=1"
result = urllib2.urlopen(url).read()
result = json.loads(result)
if int(result['errno']) == 0:
size = {}
size['total'] = long(result['data']['total_size'])
size['used'] = long(result['data']['used_size'])
size['free'] = long(result['data']['total_size']) - long(result['data']['used_size'])
return size
else:
print 'Fetch Free Space Size Error! Message: '+ result['errmsg']
sys.exit()
def offlineList(self):
'''获取离线下载列表
'''
url = self.serverAddr + "/offline/getOfflineTaskList"
args = {
'ajax' : '1',
}
args = urllib.urlencode(args)
reqArgs = urllib2.Request(
url = url,
data = args
)
reqArgs.add_header("Referer", self.serverAddr + "/my/index/");
result = urllib2.urlopen(reqArgs).read()
result = utilsYunPan.jsonRepair(result)
result = json.loads(result)
if int(result['errno']) == 0:
return result['data']
else:
print 'Get Offline Tast List Err! Message: '+ result['errmsg']
sys.exit()
def offlineDownload(self, offurl):
'''离线下载
'''
url = self.serverAddr + "/offline/offlineDownload"
args = {
'ajax' : '1',
'url' : offurl,
}
args = urllib.urlencode(args)
reqArgs = urllib2.Request(
url = url,
data = args
)
reqArgs.add_header("Referer", self.serverAddr + "/my/index/");
result = urllib2.urlopen(reqArgs).read()
result = utilsYunPan.jsonRepair(result)
result = json.loads(result)
if int(result['errno']) == 0:
return result['data']
else:
print 'Create Offline Tast Err! Message: '+ result['errmsg']
sys.exit()
def ls(self, path = '/'):
'''获取path目录下的文件列表
path String 需要查询的路径 根目录为 /
'''
url = self.serverAddr + '/file/list'
args = {
'ajax' : '1',
'field' : 'file_name',
'order' : 'desc',
'page' : '0',
'page_size' : '300',
'path' : path,
't' : str(random.random()),
'type' : '2'
}
args = urllib.urlencode(args)
reqArgs = urllib2.Request(
url = url,
data = args
)
reqArgs.add_header("Referer", "http://c21.yunpan.360.cn/my/index/");
result = urllib2.urlopen(reqArgs).read()
#result = open(sys.path[0] + '/dir.dat').read()
result = utilsYunPan.jsonRepair(result)
result = json.loads(result)
if int(result['errno']) == 0:
return result['data']
else:
print 'Get Dir List Error, Please try again later! Message: '+ result['errmsg']
sys.exit()
def downloadFile(self, fname, nid, fileHash):
''' 根据文件名和节点ip下载文件
fname String 文件路径名称 path
nid Long 节点id
fielHash String 文件的hash值
'''
# 如果hash值相同则无需下载
#if fileHash == utilsYunPan.getFileHash(fname):
# return True
url = self.serverAddr + "/file/download"
args = {
'fname' : fname,
'hid' : '',
'nid' : nid
}
args = urllib.urlencode(args)
reqArgs = urllib2.Request(
url = url,
data = args
)
reqArgs.add_header("Referer", "http://c21.yunpan.360.cn/my/index/");
result = urllib2.urlopen(reqArgs).read()
result = json.loads(result)
# 这里需要做一些验证
result = urllib2.urlopen(result['data']['download_url']).read();
fname = self.pathYunPan + fname
fname = fname.decode()
if utilsYunPan.isText(result[0:512]):
print "Download File: " + fname + " type: ASCII"
open(fname, 'w').write(result)
else:
print "Download File: " + fname + " type: Binary"
open(fname, 'wb').write(result)
def downloadDirTree(self, path = '/', force = False):
dirTreeName = utilsYunPan.getConfig('tmpDir') + '/dirTree.dat'
tree = []
if os.path.exists(dirTreeName):
tree = open(dirTreeName).read()
tree = json.loads(tree)
if len(tree) == 0 or force:
tree = self.fetchDirTree(path)
open(dirTreeName, "w").write(json.dumps(tree))
# 保存文件数
self.directoryTree = tree
# 开始下载文件夹
for i in tree:
if len(i) > 0 and i.has_key('isDir') and i['isDir'] == 1:
self.mkdir(self.pathYunPan + i['path'])
print "Download Directroy: " + i['path']
# 查询子目录
if i.has_key('childs') and len(i['childs']) > 0:
tree += i['childs']
return tree
def fetchDirTree(self, path = '/'):
dirList = self.ls(path)
for i in dirList:
if len(i) > 0 and i.has_key('isDir') and i['isDir'] == 1:
print "Fetch Directroy: " + i['path']
i['childs']= self.fetchDirTree(i['path']);
sys.stdout.flush()
return dirList
def mkdir(self, path):
if not os.path.exists(path):
os.makedirs(path)
if __name__ == '__main__':
login = loginYunPan()
userinfo = login.run('user', 'pwd')
pathYunPan = 'E:/testyun'
dir = dirYunPan(pathYunPan, login.serverAddr)
dir.downloadFile('/文档/体检报告.pdf', '13679929262714855', 'c26aac23bd0e3c1565adeee9d6681dcab37fb2a9')
# 签到 http://c21.yunpan.360.cn/user/signin/
'''
ajax 1
'''
# 提示 http://c21.yunpan.360.cn/notice/getNoticeCount
'''
ajax 1
method check
qid 15747194
t 1377830443043
'''
# 上传 http://c21.yunpan.360.cn/upload/getuploadaddress/
'''
ajax 1
'''