-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dropbox.py
266 lines (209 loc) · 6.32 KB
/
Dropbox.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2016-11-24 16:33:59
# @Author : Tom Hu ([email protected])
# @Link : http://h1994st.com
# @Version : 1.0
import os
import pytz
import time
from pprint import pprint
import dropbox
import Config
from auto_flow_leaker.auto_flow.post import Post
from auto_flow_leaker.auto_flow.channel import Channel
class Dropbox(Channel):
'''
Default user: [email protected]
'''
def __init__(self, folder=None):
super(Dropbox, self).__init__()
print 'Initialzing Dropbox channel:'
self._dbx = dropbox.Dropbox(Config.Dropbox('access_token'))
print ' Access token: %s' % Config.Dropbox('access_token')
self.default_folder = folder
print ' Default folder: %s' % self.default_folder
def description(self):
return 'access_token={!r}, default_folder={!r}'.format(
Config.Dropbox('access_token'), self.default_folder)
def send(self, content, title=None):
'''
Default title: unix epoch
'''
try:
res = self.create_file(
content, filename=(title or '%.6f.txt' % time.time()))
except Exception:
return None
else:
return Post(
id=res.id,
title=res.name,
create_time=res.server_modified.replace(tzinfo=pytz.utc))
def receive_all(self):
def converter(file_meta):
return Post(
id=file_meta.id,
title=file_meta.name,
content=self.get_file(file_meta.name).content,
create_time=file_meta.server_modified.replace(tzinfo=pytz.utc))
return sorted(
map(converter, self.get_files()),
key=lambda x: x.create_time, reverse=True)
def delete(self, item):
self.delete_file(item.title)
def delete_all(self):
self.delete_all_files()
@property
def default_folder(self):
'''
Default folder for Dropbox channel.
:rtype: str
'''
if self._default_folder_value is not None:
return self._default_folder_value
else:
raise AttributeError('missing required field \'default_folder\'')
@default_folder.setter
def default_folder(self, val):
self._default_folder_value = val or Config.Dropbox('root_dir')
@default_folder.deleter
def default_folder(self):
self._default_folder_value = Config.Dropbox('root_dir')
@property
def files(self):
return self.get_files()
def create_file(self, content, filename=None):
'''
Upload a new file
Default filename: unix epoch
'''
fullpath = os.path.join(self.default_folder, filename)
try:
res = self._dbx.files_upload(content, fullpath, autorename=True)
except dropbox.exceptions.ApiError as err:
print '*** API error', err
raise err
else:
return res
def get_file(self, filename, save=False):
'''
Download a file in 'AutoFlow' folder
'''
fullpath = os.path.join(self.default_folder, filename)
try:
if save:
md, res = self._dbx.files_download_to_file(
Config.Dropbox('local_folder'), fullpath)
else:
md, res = self._dbx.files_download(fullpath)
except dropbox.exceptions.ApiError as err:
print '*** API error', err
raise err
else:
return res
def get_files(self):
'''
Get the metadata of all files
'''
try:
res = self._dbx.files_list_folder(
self.default_folder,
recursive=False, include_deleted=False)
except Exception:
return []
else:
return res.entries
def delete_file(self, filename):
'''
Delete a file
'''
fullpath = os.path.join(self.default_folder, filename)
self._dbx.files_delete(fullpath)
def delete_all_files(self):
'''
Delete all the files
'''
print 'Delete all files in %s' % self.default_folder
# Delete root dir
print ' Delete default folder: %s' % self.default_folder
self._dbx.files_delete(self.default_folder)
# Create new root dir
print ' Create new default folder: %s' % self.default_folder
self._dbx.files_create_folder(self.default_folder)
def test_dropbox():
dbx = Dropbox()
# Read all
pprint(dbx.files)
# Write
print 'Input file: ./data/eva_time_data_2.in'
with open('data/eva_time_data_2.in', 'r') as fp:
md = dbx.create_file(fp.read())
# Read one
res = dbx.get_file(md.name)
print res
print type(res)
print dir(res)
print res.content
# Read all
pprint(dbx.files)
# Delete
dbx.delete_file(md.name)
# Read all
pprint(dbx.files)
def test_upload_files():
dbx = Dropbox()
# Read all
pprint(dbx.files)
print 'Input file: ./data/eva_time_data_2.in'
with open('data/eva_time_data_2.in', 'r') as fp:
content = fp.read()
print '1'
print dbx.send(content)
time.sleep(1)
print '2'
print dbx.send(content)
time.sleep(1)
print '3'
print dbx.send(content)
time.sleep(1)
print '4'
print dbx.send(content)
time.sleep(1)
print '5'
print dbx.send(content)
time.sleep(1)
# time.sleep(3)
pprint(dbx.receive_all())
# Read all
pprint(dbx.files)
# Delete all
dbx.delete_all_files()
# Read all
pprint(dbx.files)
def test_delete_file():
dbx = Dropbox()
# Delete all
dbx.delete_all()
# Read all
pprint(dbx.files)
print 'Input file: ./data/eva_time_data_2.in'
with open('data/eva_time_data_2.in', 'r') as fp:
content = fp.read()
post = dbx.send(content)
print post
time.sleep(3)
# Read all
pprint(dbx.receive_all())
# Delete all
dbx.delete(post)
# Read all
pprint(dbx.receive_all())
if __name__ == '__main__':
# test_delete_file()
dbx = Dropbox(folder='/Zapier')
posts = dbx.receive_all()
print posts
dbx.delete(posts[0])
posts = dbx.receive_all()
print posts