forked from konomae/bitcasapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
executable file
·158 lines (109 loc) · 4.43 KB
/
upload.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
#!/usr/bin/env python
# coding: utf-8
from __future__ import unicode_literals, print_function
import argparse
import os
import re
import sys
if sys.version_info.major == 3:
print("Python 3.x is not supported (thanks to poster)")
sys.exit(1)
from bitcasa import BitcasaClient
from path import path
from poster.encode import multipart_encode, MultipartParam, encode_and_quote
from poster.streaminghttp import register_openers
from pprint import pprint
from urllib2 import urlopen
from urllib2 import Request
from urllib2 import HTTPError, URLError
path_cache = {}
class MultipartParamFixed(MultipartParam):
def __init__(self, *args, **kwargs):
super(MultipartParamFixed, self).__init__(*args, **kwargs)
def encode_hdr(self, boundary):
"""Returns the header of the encoding of this parameter"""
boundary = encode_and_quote(boundary)
headers = ["--%s" % boundary]
disposition = 'form-data; name="file"; filename="%s"' % self.name
headers.append("Content-Disposition: %s" % disposition)
headers.append("")
headers.append("")
return "\r\n".join(headers)
def wordlist_to_regex(words):
escaped = map(re.escape, words)
combined = '|'.join(sorted(escaped, key=len, reverse=True))
return re.compile(combined)
def filename_is_compliant(filename):
if filename.name.startswith("."):
return False
r = wordlist_to_regex(["<", ">", ":", "\"", "/", "\\", "|", "?", "*"])
if r.search(filename.name):
return False
return True
def get_path(bitcasa, dst_path):
try:
return path_cache[dst_path]
except KeyError:
pass
dst = path(dst_path)
if not str(dst).startswith("/"):
print("Destination name must be absolute.")
print("/cats/dogs")
sys.exit(1)
bitcasa_path = "/"
virtual_path = ""
for d in dst.abspath().splitall():
if str(d) == "/":
continue
r = bitcasa.post(
'folders' + bitcasa_path, data="folder_name=" + str(d))
bitcasa_path = r.json()['result']['items'][0]['path']
virtual_path += "/" + str(d)
path_cache[virtual_path] = bitcasa_path
print("Get path {} > {}".format(virtual_path, bitcasa_path))
return bitcasa_path
def upload_file(bitcasa, f, dst_path, r):
register_openers()
datagen, headers = multipart_encode(
[MultipartParamFixed(f.name, fileobj=f.open("rb"))])
url = "https://files.api.bitcasa.com/v1/files{}?access_token={}".format(get_path(bitcasa, dst_path), r.request.headers['Authorization'].split()[1])
request = Request(url, datagen, headers)
request.add_header('Authorization', r.request.headers['Authorization'])
try:
resp = urlopen(request)
print("Upload file OK {} > {}/{}".format(f, dst_path, f.name))
except (HTTPError, URLError) as e:
print("Upload file KO {} > {}/{}".format(f, dst_path, f.name))
pprint(e.__dict__)
def upload(bitcasa, src_path, dst_path):
print("Upload {} > {}".format(src_path, dst_path))
r = bitcasa.get('folders/')
for f in src_path.abspath().listdir():
if filename_is_compliant(f.name):
if f.isfile():
upload_file(bitcasa, f, dst_path, r)
elif f.isdir():
slash = "/" if not dst_path.abspath().endswith("/") else ""
remote = dst_path.abspath() + slash + f.name
print('Upload dir > {}'.format(remote))
upload(bitcasa, f, remote)
else:
print('Error {}'.format(f))
def main():
client_id = os.environ.get("BITCASA_CLIENT_ID", "")
client_secret = os.environ.get("BITCASA_CLIENT_SECRET", "")
access_token = os.environ.get("BITCASA_ACCESS_TOKEN", "")
assert client_id, 'Please set "BITCASA_CLIENT_ID".'
assert client_secret, 'Please set "BITCASA_CLIENT_SECRET".'
assert access_token, 'Please set "BITCASA_ACCESS_TOKEN".'
bitcasa = BitcasaClient(client_id, client_secret, access_token)
parser = argparse.ArgumentParser(prog=sys.argv[0],
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('src', nargs=1, help='Source directory or filename')
parser.add_argument('dst', nargs=1, help='Destination directory')
args = parser.parse_args()
src = path(args.src[0])
dst = path(args.dst[0])
upload(bitcasa, src, dst)
if __name__ == '__main__':
sys.exit(main())