-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropbox_api.py
30 lines (25 loc) · 1002 Bytes
/
dropbox_api.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
import dropbox
import config
class API:
def __init__(self):
conf = config.get_config()
# Create connection to Dropbox.
self.dbx = dropbox.Dropbox(
app_key = conf['dropbox_app_key'],
app_secret = conf['dropbox_app_secret'],
oauth2_refresh_token = conf['dropbox_refresh_token']
)
def upload_file(self, file, share=True):
''' Upload file to Dropbox.'''
# Upload file.
with open(f'tmp/{file}', "rb") as f:
self.dbx.files_upload(f.read(), f'/{file}', mode=dropbox.files.WriteMode("overwrite"))
if share:
shared_link = self.share_file(file)
return shared_link
def share_file(self, file):
''' Share file on Dropbox.'''
# Get shared link.
shared_link_metadata = self.dbx.sharing_create_shared_link_with_settings(f'/{file}')
shared_link = shared_link_metadata.url
return shared_link.replace('?dl=0', '?dl=1')