-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
491 lines (457 loc) · 20.8 KB
/
main.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# !/usr/bin/env python
# -*-coding:utf-8 -*-
import time
import traceback
import os
from threading import Thread
import re
import requests
from DrissionPage import ChromiumPage, ChromiumOptions
from loguru import logger
from pathlib import Path
from queue import Queue
proceed_node = set()
proceed_files = set()
req_queue = Queue()
download_queue = Queue()
# 配置项
# 公司ID
corpId = ""
# 组织(库)ID
target_orgid = ""
# 无权限文件
no_right_files = []
loggined_done = False
def process_download():
while True:
res = download_queue.get(block=True)
if not res:
continue
try:
node_info, url, headers, cookies, save_path, save_name = res
p = Path(save_path)
# 创建文件夹
os.makedirs(p.absolute(), exist_ok=True)
cookies = {x["name"]: x["value"] for x in cookies}
download_success = False
for retry_times in range(10):
try:
req = requests.request(method="get", url=url, headers=headers, cookies=cookies)
filename = str(url).split("?")[0].split("/")[-1]
save_path = p.joinpath(filename)
if req.status_code == 200:
with open(save_path.absolute(), "wb") as f:
f.write(req.content)
download_success = True
break
else:
raise Exception(f"下载失败,返回状态码{req.status_code},内容:{req.content}")
except Exception as e:
logger.error(f"下载文件{url} 重试{retry_times} 出错:{e}")
time.sleep(5)
continue
if not download_success:
logger.error(f"下载文件{url}失败,推回节点到浏览器进行重试")
q.put(node_info)
except Exception as e:
logger.error(f"下载{res}出错 {e}:{traceback.format_exc()}")
def request_repeater(q):
while True:
res = req_queue.get(block=True)
data = None
if res:
if "/dentry/list?" not in str(res.url):
logger.info(f"跳过{str(res.url)}")
continue
for _ in range(10):
try:
logger.info(f"二次请求{res.url},待请求长度:{req_queue.qsize()}")
cookies = {x["name"]: x["value"] for x in res.request.cookies}
data = requests.request(method="get", url=res.url, headers=res.request.headers,
cookies=cookies)
data = data.json()["data"]
logger.info(f"二次请求完成,待请求长度:{req_queue.qsize()}")
break
except Exception as e:
logger.error(f"二次请求{res.url} 出错:{e}")
time.sleep(5)
continue
if data:
process_req(q, data)
else:
logger.error(f"二次请求{res.url} 失败次数超过10,放弃")
def process_req(q, data):
if not data:
return
if "children" in data:
process_node_name = data['name']
item_list = data["children"]
added_names = []
for node_info in item_list:
node_name = node_info['name']
node_uuid = node_info['dentryUuid']
if node_uuid not in proceed_node:
added_names.append(node_name)
q.put(node_info)
proceed_node.add(node_uuid)
if added_names:
logger.info(f"队列长度:{q.qsize()} 从【{process_node_name}】 添加子节点{len(added_names)}个:{', '.join(added_names)}")
class Processer:
def __init__(self, q, index=0):
self.idx = index
self.q = q
self.page = ChromiumPage(ChromiumOptions().set_local_port(int(f"933{index}")).set_user_data_path(f'data{index}'))
package_urls = ['box/api/v2/dentry/list?']
self.page.listen.start(package_urls, res_type=True)
self.page.get(f'https://alidocs.dingtalk.com/i/desktop/spaces/?corpId={corpId}')
self.inited = False
self.headers = {}
self.cookies = {}
def run(self):
empty_count = 0
while True:
if loggined_done and not self.inited:
self.inited = True
# 打开组织页面
self.page.get(f'https://alidocs.dingtalk.com/i/spaces/{target_orgid}/overview?corpId={corpId}')
self.block_wait()
while self.page.listen._caught.qsize():
res = self.page.listen.wait(timeout=5)
if res:
if res.response.body and res.response.body.get("data"):
data = res.response.body["data"]
process_req(self.q, data)
try:
# 更新最新header以及cookies
self.headers = res.request.headers
self.cookies = res.request.cookies
except Exception:
pass
else:
req_queue.put(res)
if not self.q.empty():
item = self.q.get()
for retry in range(4):
try:
self.process_node(item)
break
except Exception as e:
logger.error(f"处理{item}时发生错误:{e} 重试{retry+1}")
empty_count = 0
continue
if empty_count > 30:
logger.info(f"[{self.idx}] 退出")
break
empty_count += 1
time.sleep(5)
self.page.close()
self.page.browser.quit()
def block_wait(self):
time.sleep(1)
while not self.page.listen.wait_silent(targets_only=True):
time.sleep(1)
def process_node(self, node_info, load_page=True):
node_name = node_info['name']
node_uuid = node_info['dentryUuid']
ancestorList = node_info['ancestorList']
# if node_uuid in proceed_node:
# logger.info(f"[{self.idx}]jump{node_uuid}")
# return
parent_node_name = "根节点"
if ancestorList:
parent_node_name = ancestorList[-1]['name']
logger.info(f"[{self.idx}] 开始处理节点:{node_name} 父节点:{parent_node_name}")
# 直接跳转页面
if load_page:
self.page.get(f"https://alidocs.dingtalk.com/i/nodes/{node_uuid}")
self.block_wait()
# 判断是否页面白屏
if node_info.get('contentType') == 'alidoc' or node_info.get('dentryType') == 'file':
logger.info(f"[{self.idx}] {node_name}是文件,继续处理")
success = self.process_file(node_info)
if not success:
logger.info(f"[{self.idx}] {node_name} 文件 处理失败,推回队列 后续重试")
self.q.put(node_info)
# 选中节点
find_div = f"@data-rbd-draggable-id={node_uuid}"
try:
item = self.scroll_to_see(find_div)
self.to_item(item)
except Exception as e:
logger.info(f"[{self.idx}] {find_div}: {e} {traceback.format_exc()}")
self.process_node(node_info, load_page=False)
else:
# 选中节点
find_div = f"@data-rbd-draggable-id={node_uuid}"
try:
button = self.scroll_to_see(find_div)
if not button:
self.process_node(node_info)
time.sleep(0.5)
self.to_item(button)
button.click()
time.sleep(0.5)
except Exception as e:
logger.info(f"[{self.idx}] {find_div}: {e} {traceback.format_exc()}")
self.process_node(node_info, load_page=False)
def to_item(self, item):
item_loc = item.location
self.page.scroll.to_location(item_loc[0], item_loc[1])
def check_alert(self):
for i in range(2):
# 检查是否有按钮 继续导出
has_limit = self.page.eles("tag:button@@text():继续导出", timeout=0.5)
if has_limit:
has_limit[0].click()
break
def scroll_to_see(self, loc,retry_times=0):
if retry_times > 5:
return
try:
# 先尝试直接找
client_now = self.page.run_js(
'return document.getElementsByClassName("MAINSITE_CATALOG-node-tree-list")[0].scrollTop')
client_height = self.page.run_js(
'return document.getElementsByClassName("MAINSITE_CATALOG-node-tree-list")[0].clientHeight')
tree = self.page.ele(".:MAINSITE_CATALOG-node-tree-list")
item = self.scroll(tree, client_now, client_height, loc)
if item:
return item
else:
return self.scroll_to_see(loc,retry_times+1)
except Exception as e:
logger.error(f"滚动时出错:{e},等待重试")
return self.scroll_to_see(loc,retry_times+1)
def scroll(self, tree, start, client_height, loc):
if start == 0:
tree.scroll.to_top()
tree.scroll.to_location(0, 0)
time.sleep(0.5)
item = self.page.ele(loc, timeout=2)
if item:
return item
last_scrollTop = None
start_height = int(start)
to_height = self.page.run_js(
'return document.getElementsByClassName("MAINSITE_CATALOG-node-tree-list")[0].scrollHeight')
# 每次滚动的高度
roll_height = 300
while start_height < to_height:
to_height = self.page.run_js(
'return document.getElementsByClassName("MAINSITE_CATALOG-node-tree-list")[0].scrollHeight')
if last_scrollTop is not None and last_scrollTop == to_height and (
start_height > (to_height / 2)):
return self.scroll(tree, 0, client_height, loc)
last_scrollTop = self.page.run_js(
'return document.getElementsByClassName("MAINSITE_CATALOG-node-tree-list")[0].scrollTop')
logger.info(f"[{self.idx}] 正在滚动【高度信息:当前:{last_scrollTop or 0}->目标:{start_height}/整体{to_height}】")
start_height += roll_height
tree.scroll.to_location(0, start_height)
item = self.page.ele(loc, timeout=3)
if item:
return item
def process_file(self, node_info, retry_times=0):
def c(x):
x = (x or "").replace('\\', '_').replace(' ', '_').replace(':', '_')
x = x.replace('/', '_').replace('?', '_').replace("*", "_")
x = x.replace('\n', '_').strip()
x = re.sub(r"(?u)[^-\w.]", "", x)
return x
node_name = node_info['name']
file_type = node_name.split(".")[-1]
node_uuid = node_info['dentryUuid']
ancestor_path = [c(x['name']) for x in node_info['ancestorList']]
if node_uuid in proceed_files:
return True
proceed_files.add(node_uuid)
self.block_wait()
file_path = '\\'.join([c(x) for x in ancestor_path])
node_name = c(node_name.rsplit(".", 1)[0])
logger.info(f"[{self.idx}] 处理文件:{node_name} 路径:{file_path} 文件类型:{file_type}")
path = Path(".").absolute()
path = path.joinpath(target_orgid)
path = path.joinpath(file_path)
os.makedirs(str(path.absolute()), exist_ok=True)
fname = path.joinpath(node_name)
if fname.exists() and fname.is_dir() and len(list(os.listdir(fname))) > 0:
logger.info(f"[{self.idx}] 节点已完成下载:{fname} 跳过。")
return True
if retry_times > 2:
self.page.refresh()
if retry_times > 5:
logger.error(f"请求节点:{node_name},{ancestor_path}出错次数超过10次,放弃")
no_right_files.append((file_path, node_name, file_type))
proceed_files.remove(node_uuid)
return
# 选中节点
find_div = f"@data-rbd-draggable-id={node_uuid}"
try:
item = self.scroll_to_see(find_div)
self.to_item(item)
item.click()
except Exception as e:
logger.info(f"[{self.idx}] {find_div}: {e}")
return self.process_file(node_info, retry_times+1)
# 判断是否无权限访问
notice_eles = self.page.eles("@data-item-key=apply-title-view") or []
for ne in notice_eles:
if "暂无权限访问" in str(ne.text):
no_right_files.append((file_path, node_name, file_type))
logger.info(f"[{self.idx}] 节点:{node_name} 无访问权限,跳过")
return True
# 如果是链接
if file_type == "dlink":
file_type = node_info['linkSourceInfo']['extension']
logger.info(f"[{self.idx}] 链接文件:{fname} 真实文件类型为:{file_type}")
self.page.set.download_path(str(fname.absolute()))
self.page.set.download_file_name(node_name)
self.page.set.when_download_file_exists("skip")
time.sleep(5)
try:
download_task = False
last_err = None
if "adoc" in file_type:
limited_toolbar = self.page.eles("@data-testid=doc-header-more-button", timeout=2)
if limited_toolbar:
for i in range(5):
try:
limited_toolbar[0].click()
time.sleep(0.5)
self.page.ele("@data-item-key=export").click()
self.page.ele("@data-item-key=exportAsWord").click()
self.check_alert()
download_task = self.page.wait.download_begin(timeout=120)
last_err = None
break
except Exception as err:
last_err = err
time.sleep(3)
continue
else:
for i in range(5):
try:
normal_toolbar = self.page.eles("@data-testid=bi-toolbar-menu", timeout=2)
if normal_toolbar:
normal_toolbar[0].click()
self.page.ele("@data-testid=bi-toolbar-menu").click()
time.sleep(0.5)
self.page.ele("@data-testid=menu-item-J_file").click()
time.sleep(0.5)
self.page.ele("@data-testid=menu-item-J_fileExport").click()
time.sleep(0.5)
self.page.ele("@data-testid=menu-item-J_exportAsWord").ele("text:Word").click()
self.check_alert()
download_task = self.page.wait.download_begin(timeout=120)
last_err = None
break
except Exception as err:
last_err = err
time.sleep(3)
continue
else:
no_right_files.append((file_path, node_name, file_type))
elif "axls" in file_type:
limited_toolbar = self.page.eles("@data-testid=doc-header-more-button", timeout=2)
if limited_toolbar:
for i in range(5):
try:
limited_toolbar[0].click()
time.sleep(0.5)
self.page.ele("@data-item-key=DOWNLOAD_AS").click()
self.page.ele("@data-item-key=EXCEL").click()
self.check_alert()
download_task = self.page.wait.download_begin(timeout=120)
last_err = None
break
except Exception as err:
last_err = err
time.sleep(3)
continue
else:
for i in range(5):
try:
normal_toolbar = self.page.eles("#wiki-new-sheet-iframe")
if normal_toolbar:
normal_toolbar[0].ele(
"@data-testid=submenu-menubar-table").ele("text:表格").click()
time.sleep(0.5)
self.page.ele("#wiki-new-sheet-iframe").ele(
"@data-testid=submenu-export-excel").ele("text:下载为").click()
time.sleep(0.5)
self.page.ele("#wiki-new-sheet-iframe").ele("text:Excel").click()
self.check_alert()
download_task = self.page.wait.download_begin(timeout=120)
last_err = None
break
except Exception as err:
last_err = err
time.sleep(3)
continue
else:
for i in range(5):
try:
download_button = self.page.eles("@data-item-key=download", timeout=1)
if download_button:
download_button[0].click()
self.check_alert()
download_task = self.page.wait.download_begin(timeout=120)
last_err = None
break
else:
no_right_files.append((file_path, node_name, file_type))
except Exception as err:
last_err = err
time.sleep(3)
continue
else:
logger.error(f"[{self.idx}] 跳过未处理的文件下载:{fname} ")
return True
if last_err:
raise last_err
need_restart = False
if not download_task:
need_restart = True
else:
# 等待下载
while not download_task.is_done:
time.sleep(.5)
if not download_task.final_path and not download_task.state == "skipped":
logger.info(f"[{self.idx}] 下载{fname} 任务失败 任务最终状态:{download_task.state}")
if "blob" not in download_task.url:
res = (node_info, download_task.url, self.headers, self.cookies, str(fname.absolute()), node_name)
download_queue.put(res)
logger.info(f"[{self.idx}] 生成下载{fname}任务")
if need_restart:
logger.error(f"[{self.idx}] 下载:{fname} 未完成任务生成就结束了,重试一次")
proceed_files.remove(node_uuid)
return self.process_file(node_info, retry_times + 1)
except Exception as e:
logger.error(f"[{self.idx}] 下载:{fname} 时出现问题,可能是无下载权限造成的:{e} {traceback.format_exc()}")
# no_right_files.append((file_path, node_name, file_type))
proceed_files.remove(node_uuid)
return self.process_file(node_info, retry_times+1)
logger.info(f"[{self.idx}] 已完成处理文件:{node_name} 路径:{file_path} 文件类型:{file_type},等待..")
time.sleep(0.5)
return True
if __name__ == "__main__":
threads = []
q = Queue()
logger.info("启动浏览器。。。")
for i in range(5):
thread = Thread(target=request_repeater, args=(q,))
thread.start()
for i in range(5):
thread = Thread(target=process_download, args=())
thread.start()
for i in range(5):
thread = Thread(target=Processer(q, i).run, args=())
thread.start()
input(f"请完成所有浏览器的登录,并在完成后任意键继续")
loggined_done = True
input(f"全部下载完成后任意键继续")
[x.join() for x in threads]
time.sleep(5)
while q.qsize():
time.sleep(10)
print(f"无权限文件的列表\n{no_right_files}")
input("全部抓取完成,任意键退出")