-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransporter.py
77 lines (72 loc) · 2.99 KB
/
transporter.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
import os
import dropbox
from tqdm import tqdm
from .logger import get_logger
mode = {
'add': 'add',
'overwrite': 'overwrite',
'update': 'update'
}
class DropboxTransporter:
def __init__(self, access_token: str, write_mode: str, autorename: bool):
self.logger = get_logger(__name__)
self.access_token = access_token
self.dropbox_session = dropbox.Dropbox(self.access_token)
self.autorename = autorename
self.chunk_size = 4 * 1024 * 1024
try:
self.write_mode = dropbox.files.WriteMode(mode[write_mode])
except KeyError:
self.logger.error('Please input the correct Write Mode. [add/overwrite/update]')
raise KeyError
def upload(self, file, file_to) -> None:
self.logger.info('initiate dropbox upload.')
with open(file, 'rb') as f:
size = os.path.getsize(file)
if size <= self.chunk_size:
self.logger.info(
self.dropbox_session.files_upload(
f.read(),
file_to,
mode=self.write_mode,
autorename=self.autorename)
)
else:
with tqdm(total=size) as progress:
upload_session = self.dropbox_session.files_upload_session_start(
f.read(self.chunk_size),
)
progress.update(self.chunk_size)
cursor = dropbox.dropbox.files.UploadSessionCursor(
session_id=upload_session.session_id,
offset=f.tell()
)
upload_commit = dropbox.dropbox.files.CommitInfo(
path=file_to,
mode=self.write_mode,
autorename=self.autorename
)
while f.tell() < size:
if (size - f.tell()) <= self.chunk_size:
self.logger.info(
self.dropbox_session.files_upload_session_finish(
f.read(self.chunk_size), cursor, upload_commit
)
)
else:
self.dropbox_session.files_upload_session_append_v2(
f.read(self.chunk_size),
cursor
)
cursor.offset = f.tell()
progress.update(self.chunk_size)
self.logger.info('dropbox upload finished.')
def connection_check(self) -> None:
payload = 'ping'
echo = self.dropbox_session.check_user(payload)
self.logger.debug(echo)
if echo.result != payload:
self.logger.error('Dropbox connection failed.')
raise ConnectionError
else:
self.logger.error('Dropbox connection success.')