-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropbox_class.py
executable file
·54 lines (46 loc) · 1.78 KB
/
dropbox_class.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
import dropbox
import os
class Dropbox(object):
def __init__(self):
try:
self.__app_keys = open(
os.path.expanduser('~') +
'/Dropbox/Stuff/.dropbox_screenshot_keys', 'r'
)
except IOError:
print('You need to get the dropbox app keys from me'
' (Josh Manning), or create your own \'Dropbox App\'')
return
try:
self.__access_token = open(
os.path.expanduser('~') +
'/Dropbox/Stuff/.secret_auth', 'r'
).readline()
except IOError:
self.__create_auth()
self.client = dropbox.client.DropboxClient(self.__access_token)
def __create_auth(self):
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(
self.__app_keys.readline()[:-1],
self.__app_keys.readline()[:-1]
)
self.__app_keys.close()
authorize_url = flow.start()
print('1. Go to: ' + authorize_url)
print('2. Click "Allow" (you might have to log in first)')
print('3. Copy the authorization code.')
code = raw_input("Enter the authorization code here: ").strip()
self.__access_token, user_id = flow.finish(code)
try:
auth_file = open(os.path.expanduser('~') + '/Dropbox/.secret_auth', 'w')
except IOError:
print("Change the file names to not use dropbox.")
return
auth_file.write(self.__access_token)
auth_file.close()
def put_file(self, path, name):
file_upload = open(path + '/' + name, 'rb')
self.client.put_file('/Screenshots/' + name, file_upload)
file_upload.close()
def get_link_for_file(self, path):
return self.client.share(path, False)['url']