daily pastebin goal
27%
SHARE
TWEET

Modify payload

a guest Oct 11th, 2018 4 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. import json
  2. from datetime import datetime
  3.  
  4. api = platform.api
  5. payload = event.request.payload
  6. headers = event.request.headers
  7. resource = event.resource
  8.  
  9. epoch = datetime(1970, 1, 1)
  10.  
  11.  
  12. def timestamp_to_datetime(ts):
  13.     return datetime.utcfromtimestamp(int(ts)).strftime('%Y-%m-%d %H:%M:%S')
  14.    
  15.  
  16. def datetime_to_timestamp(dt):
  17.     return (dt - epoch).total_seconds()
  18.    
  19.  
  20. required_time_fields = ['start', 'start_datetime', 'end', 'end_datetime']
  21. for rec in payload.resource:
  22.     if all([field in rec for field in required_time_fields]):
  23.         continue
  24.     if 'start' in rec and 'start_datetime' not in rec:
  25.         rec.start_datetime = timestamp_to_datetime(rec.start)
  26.     if 'end' in rec and 'end_datetime' not in rec:
  27.         rec.end_datetime = timestamp_to_datetime(rec.end)
  28.     if 'start_datetime' in rec and 'start' not in rec:
  29.         rec.start = datetime_to_timestamp(rec.start_datetime)
  30.     if 'end_datetime' in rec and 'end' not in rec:
  31.         rec.end = datetime_to_timestamp(rec.end_datetime)
  32.        
  33. event.request.content = json.dumps(event.request.payload)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top