This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gquickdrop.py
71 lines (56 loc) · 2.26 KB
/
gquickdrop.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
from dropbox.auth import Authenticator
from dropbox.client import DropboxClient
import webbrowser, sys
class FileAlreadyPresentError(Exception):
def __init__(self, fileName):
self.fileName = fileName
def __str__(self):
return repr(self.fileName)
def getAccToken(reqToken):
print "Getting Authorization Token"
######################################################
# Need to replace this with gtkmozembed from pygtk.com
webbrowser.open(auth.build_authorize_url(reqToken))
raw_input("\nPress Enter when you have authorized \n")
######################################################
accToken = auth.obtain_access_token(reqToken, "")
return accToken
def openClient():
config = Authenticator.load_config("config.ini")
auth = Authenticator(config)
reqToken = auth.obtain_request_token()
accToken = getAccToken(reqToken)
print "Token: %s" % str(accToken)
client = DropboxClient("api.dropbox.com", "api-content.dropbox.com", 80, auth, accToken)
return client
def uploadFile(fileName, client):
print "Uploading %s" % fileName
fp = open(fileName, "r")
resp = client.put_file("dropbox", "/Public", fp);
fp.close()
def getPublicUrl(fileName, client):
fileMetaData = client.metadata("dropbox", "/Public/%s" % fileName)
publicUrl = fileMetaData['path']
return publicUrl
if __name__ == "__main__":
try:
myFile = sys.argv[1]
except IndexError:
print "Error, you must specify a file to upload"
else:
client = openClient()
public = client.metadata("dropbox", "/Public")
if public.status != 404:
print "Loaded Public directory "
try:
for pubFile in public.data['contents']:
if pubFile['path'].split("/")[-1] == myFile:
raise FileAlreadyPresentError(myFile)
except FileAlreadyPresentError as e:
print "File %s already present" % e.fileName
else:
print "File Not Present"
uploadFile(myFile, client)
print "File URL: %s" % getPublicUrl(e.fileName, client)
else:
print "Public directory not found"