7 from httplib2
import ServerNotFoundError
8 from pydrive.auth
import GoogleAuth
9 from pydrive.drive
import GoogleDrive
10 from pydrive.files
import ApiRequestError
13 from gdrive_ros.srv
import MultipleUpload
14 from gdrive_ros.srv
import MultipleUploadResponse
15 from gdrive_ros.srv
import Upload
16 from gdrive_ros.srv
import UploadResponse
20 folder_mime_type =
'application/vnd.google-apps.folder' 21 folder_url_format =
'https://drive.google.com/drive/folders/{}' 22 file_url_format =
'https://drive.google.com/uc?id={}' 25 settings_yaml = rospy.get_param(
'~settings_yaml',
None)
26 self.
share_type = rospy.get_param(
'~share_type',
'anyone')
28 self.
share_role = rospy.get_param(
'~share_role',
'reader')
30 if settings_yaml
is not None:
31 self.
gauth = GoogleAuth(settings_yaml)
33 rospy.logerr(
'param: ~settings_yaml is not correctly set.')
36 rospy.loginfo(
'Google drive authentication starts.')
37 self.gauth.LocalWebserverAuth()
39 rospy.loginfo(
'Google drive authentication finished.')
45 timestamp =
'{0:%Y%m%d%H%M%S}'.format(datetime.datetime.now())
46 parents_path = req.parents_path
47 parents_id = req.parents_id
50 res = UploadResponse()
55 if parents_id
and parents_path:
56 rospy.logerr(
'parents_path and parents_id is both set.')
57 rospy.logerr(
'parents_id: {} is selected to upload.'.format(parents_id))
63 parents_path, mkdir=
True)
64 except (ValueError, ApiRequestError, ServerNotFoundError)
as e:
67 'Failed to get parents_id: {}'.format(parents_path))
70 elif parents_id ==
'' and parents_path ==
'':
73 if req.use_timestamp_folder:
76 [timestamp], parents_id=parents_id, mkdir=
True)
77 except (ValueError, ApiRequestError, ServerNotFoundError)
as e:
80 'Failed to get parents_id: {} in {}'.format(
81 timestamp, self.folder_url_format.format(parents_id)))
85 req.file_path, req.file_title, parents_id,
86 req.use_timestamp_file_title, timestamp)
89 res.file_url = file_url
90 res.parents_id = parents_id
91 res.parents_url = self.folder_url_format.format(parents_id)
95 timestamp =
'{0:%Y%m%d%H%M%S}'.format(datetime.datetime.now())
96 parents_path = req.parents_path
97 parents_id = req.parents_id
100 res = MultipleUploadResponse()
101 res.successes = [
False] * len(req.file_titles)
102 res.file_ids = [
''] * len(req.file_titles)
103 res.file_urls = [
''] * len(req.file_titles)
105 if parents_id
and parents_path:
106 rospy.logerr(
'parents_path and parents_id is both set.')
107 rospy.logerr(
'parents_id: {} is selected to upload.'.format(parents_id))
113 parents_path, mkdir=
True)
114 except (ValueError, ApiRequestError, ServerNotFoundError)
as e:
117 'Failed to get parents_id: {}'.format(parents_path))
120 elif parents_id ==
'' and parents_path ==
'':
123 if req.use_timestamp_folder:
126 [timestamp], parents_id=parents_id, mkdir=
True)
127 except (ValueError, ApiRequestError, ServerNotFoundError)
as e:
130 'Failed to get parents_id: {} in {}'.format(
131 timestamp, self.folder_url_format.format(parents_id)))
134 for i, (file_path, file_title)
in enumerate(
135 zip(req.file_paths, req.file_titles)):
137 file_path, file_title, parents_id,
138 req.use_timestamp_file_title, timestamp)
139 res.successes[i] = success
140 res.file_ids[i] = file_id
141 res.file_urls[i] = file_url
142 res.parents_id = parents_id
143 res.parents_url = self.folder_url_format.format(parents_id)
146 def _upload_step(self, file_path, file_title, parents_id,
147 use_timestamp_file_title=
False, timestamp=
None):
148 file_title = file_title
if file_title
else file_path.split(
'/')[-1]
149 file_path = os.path.expanduser(file_path)
150 if use_timestamp_file_title:
151 file_title =
'{}_{}'.format(timestamp, file_title)
156 folder_url = self.folder_url_format.format(parents_id)
159 file_path, file_title, parents_id=parents_id)
160 file_url = self.file_url_format.format(file_id)
163 'Success to upload: {} -> {}'.format(file_path, file_url))
164 except (OSError, ApiRequestError, ServerNotFoundError)
as e:
167 'Failed to upload: {} -> {}'.format(file_path, folder_url))
168 return success, file_id, file_url
171 if not os.path.exists(file_path):
172 raise OSError(
'File not found: {}'.format(file_path))
173 rospy.loginfo(
'Start uploading a file: {}'.format(file_title))
175 gfile = self.gdrive.CreateFile(
176 {
'parents': [{
'id': parents_id}]})
178 gfile = self.gdrive.CreateFile()
179 gfile.SetContentFile(file_path)
180 gfile[
'title'] = file_title
182 gfile.InsertPermission({
188 rospy.loginfo(
'Finish uploading a file: {}'.format(file_title))
192 rospy.loginfo(
'Start making a folder: {}'.format(folder_title))
194 gfolder = self.gdrive.CreateFile(
195 {
'title': folder_title,
196 'parents': [{
'id': parents_id}],
197 'mimeType':
'application/vnd.google-apps.folder'})
199 gfolder = self.gdrive.CreateFile(
200 {
'title': folder_title,
201 'mimeType':
'application/vnd.google-apps.folder'})
203 rospy.loginfo(
'Finish making a folder: {}'.format(folder_title))
207 if parents_path ==
'':
210 if not isinstance(parents_path, list):
211 parents_path = [p
for p
in parents_path.split(
'/')
if p !=
'']
213 folder_title = parents_path[0]
214 parent = parents_id
if parents_id
else 'root' 215 gfiles = self.gdrive.ListFile(
216 {
'q':
"'{}' in parents and trashed=false".format(parent)})
217 gfiles = gfiles.GetList()
221 and gf[
'title'] == folder_title):
224 if len(parents_path) == 1:
225 if len(gfolders) > 0:
226 return gfolders[0][
'id']
229 folder_title, parents_id=parents_id)
233 'Folder is not found: {}'.format(folder_title))
235 if len(gfolders) > 0
or mkdir:
236 if len(gfolders) > 0:
237 next_parents_id = gfolders[0][
'id']
240 folder_title, parents_id=parents_id)
242 parents_path[1:], parents_id=next_parents_id,
246 raise ValueError(
'folder is not found: {}', folder_title)
249 if __name__ ==
'__main__':
250 rospy.init_node(
'gdrive_server')
def _upload_folder(self, folder_title, parents_id=None)
def _upload_file(self, file_path, file_title, parents_id=None)
def _upload_multi_cb(self, req)
def _upload_step(self, file_path, file_title, parents_id, use_timestamp_file_title=False, timestamp=None)
def _get_parents_id(self, parents_path, parents_id=None, mkdir=False)
def _upload_cb(self, req)