11#!/usr/bin/env python3
2+ import datetime
23import gdown
34import json
45import os
@@ -109,6 +110,7 @@ def rest_cb(self, goal):
109110
110111 # Card
111112 json_body ['cards' ] = []
113+ timestamp = '{0:%Y%m%d%H%M%S}' .format (datetime .datetime .now ())
112114 if goal .cards :
113115 if goal .update_message :
114116 json_body ['actionResponse' ] = {"type" : "UPDATE_MESSAGE" }
@@ -124,10 +126,12 @@ def rest_cb(self, goal):
124126 if card .header .image_url :
125127 header ['imageUrl' ] = card .header .image_url
126128 elif card .header .image_filepath :
127- header ['imageUrl' ] = self ._upload_file (card .header .image_filepath )
129+ header ['imageUrl' ] = self ._upload_file (
130+ card .header .image_filepath , timestamp = timestamp )
128131 # card/sections
129132 sections = []
130- sections = self ._make_sections_json (card .sections )
133+ sections = self ._make_sections_json (
134+ card .sections , timestamp = timestamp )
131135 # card/actions
132136 card_actions = []
133137 for card_action_msg in card .card_actions :
@@ -269,7 +273,7 @@ def _make_message_msg(self, event):
269273
270274 return message
271275
272- def _make_sections_json (self , sections_msg ):
276+ def _make_sections_json (self , sections_msg , timestamp = None ):
273277 """
274278 :type msg: list of google_chat_ros.msgs/Section
275279 :rtype json_body: list of json
@@ -279,11 +283,12 @@ def _make_sections_json(self, sections_msg):
279283 section = {}
280284 if msg .header :
281285 section ['header' ] = msg .header
282- section ['widgets' ] = self ._make_widget_markups_json (msg .widgets )
286+ section ['widgets' ] = self ._make_widget_markups_json (
287+ msg .widgets , timestamp = timestamp )
283288 json_body .append (section )
284289 return json_body
285290
286- def _make_widget_markups_json (self , widgets_msg ):
291+ def _make_widget_markups_json (self , widgets_msg , timestamp = None ):
287292 """Make widget markup json lists.
288293 See https://developers.google.com/chat/api/reference/rest/v1/cards#widgetmarkup for details.
289294 :rtype widgets_msg: list of google_chat_ros.msgs/WidgetMarkup
@@ -298,7 +303,8 @@ def _make_widget_markups_json(self, widgets_msg):
298303 buttons = []
299304 buttons_msg = msg .buttons
300305 for button_msg in buttons_msg :
301- buttons .append (self ._make_button_json (button_msg ))
306+ buttons .append (
307+ self ._make_button_json (button_msg , timestamp = timestamp ))
302308 if buttons :
303309 json_body .append ({'buttons' :buttons })
304310
@@ -311,7 +317,8 @@ def _make_widget_markups_json(self, widgets_msg):
311317 if msg .image .image_url :
312318 image_json ['imageUrl' ] = msg .image .image_url
313319 elif msg .image .localpath :
314- image_json ['imageUrl' ] = self ._upload_file (msg .image .localpath )
320+ image_json ['imageUrl' ] = self ._upload_file (
321+ msg .image .localpath , timestamp = timestamp )
315322 if msg .image .on_click .action .action_method_name or msg .image .on_click .open_link_url :
316323 image_json ['onClick' ] = self ._make_on_click_json (msg .image .on_click )
317324 if msg .image .aspect_ratio :
@@ -330,9 +337,12 @@ def _make_widget_markups_json(self, widgets_msg):
330337 elif msg .key_value .original_icon_url :
331338 keyval_json ['iconUrl' ] = msg .key_value .original_icon_url
332339 elif msg .key_value .original_icon_localpath :
333- keyval_json ['iconUrl' ] = self ._upload_file (msg .key_value .original_icon_localpath )
340+ keyval_json ['iconUrl' ] = self ._upload_file (
341+ msg .key_value .original_icon_localpath ,
342+ timestamp = timestamp )
334343 if msg .key_value .button .text_button_name or msg .key_value .button .image_button_name :
335- keyval_json ['button' ] = self ._make_button_json (msg .key_value .button )
344+ keyval_json ['button' ] = self ._make_button_json (
345+ msg .key_value .button , timestamp = timestamp )
336346 json_body .append ({'keyValue' :keyval_json })
337347 return json_body
338348
@@ -357,7 +367,7 @@ def _make_on_click_json(self, on_click_msg):
357367 json_body ['openLink' ] = {'url' : on_click_msg .open_link_url }
358368 return json_body
359369
360- def _make_button_json (self , button_msg ):
370+ def _make_button_json (self , button_msg , timestamp = None ):
361371 """Make button json.
362372 See https://developers.google.com/chat/api/reference/rest/v1/cards#button for details.
363373 :rtype button_msg: google_chat_ros.msg/Button.msg
@@ -381,7 +391,8 @@ def _make_button_json(self, button_msg):
381391 elif button_msg .original_icon_url :
382392 image_icon ['iconUrl' ] = button_msg .original_icon_url
383393 elif button_msg .original_icon_filepath :
384- image_icon ['iconUrl' ] = self ._upload_file (button_msg .original_icon_filepath )
394+ image_icon ['iconUrl' ] = self ._upload_file (
395+ button_msg .original_icon_filepath , timestamp = timestamp )
385396 return json_body
386397
387398 def _get_user_info (self , item ):
@@ -396,7 +407,7 @@ def _get_user_info(self, item):
396407 user .human = True if item .get ('type' ) == "HUMAN" else False
397408 return user
398409
399- def _upload_file (self , filepath , return_id = False ):
410+ def _upload_file (self , filepath , return_id = False , timestamp = None ):
400411 """Get local filepath and upload to Google Drive
401412 :param filepath: local file's path you want to upload
402413 :type filepath: string
@@ -412,11 +423,16 @@ def _upload_file(self, filepath, return_id=False):
412423 rospy .logerr (e )
413424 return
414425 # upload
426+ if timestamp is None :
427+ parents_path = self .upload_parents_path
428+ else :
429+ parents_path = '{}/{}' .format (
430+ self .upload_parents_path , timestamp )
415431 try :
416432 res = gdrive_upload (
417433 file_path = filepath ,
418- parents_path = self . upload_parents_path ,
419- use_timestamp_folder = True ,
434+ parents_path = parents_path ,
435+ use_timestamp_folder = False ,
420436 )
421437 except rospy .ServiceException as e :
422438 rospy .logerr ("Failed to call Google Drive upload service, status:{}" .format (str (e )))
0 commit comments