processaAuthApiCalendar.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import print_function
  2. import pickle
  3. import os.path
  4. from googleapiclient.discovery import build
  5. from google_auth_oauthlib.flow import InstalledAppFlow
  6. from google.auth.transport.requests import Request
  7. from apiclient import discovery
  8. from datetime import datetime,timedelta
  9. import httplib2
  10. import os
  11. # If modifying these scopes, delete the file token.pickle.
  12. SCOPES = ['https://www.googleapis.com/auth/calendar','https://www.googleapis.com/auth/calendar.events']
  13. def main():
  14. pathJson = "C:\\Users\\chris\\OneDrive\\Documentos\\GitHub\\MOBEES-BR\\Backend\\dsp\\adm\\auth\\credentials_calendar.json"
  15. pathPickle = "C:\\Users\\chris\\OneDrive\\Documentos\\GitHub\\MOBEES-BR\\Backend\\dsp\\adm\\auth\\token_calendar.pickle"
  16. creds = None
  17. # The file token.pickle stores the user's access and refresh tokens, and is
  18. # created automatically when the authorization flow completes for the first
  19. # time.
  20. if os.path.exists(pathPickle):
  21. with open(pathPickle, 'rb') as token:
  22. creds = pickle.load(token)
  23. # If there are no (valid) credentials available, let the user log in.
  24. if not creds or not creds.valid:
  25. if creds and creds.expired and creds.refresh_token:
  26. creds.refresh(Request())
  27. else:
  28. flow = InstalledAppFlow.from_client_secrets_file(
  29. pathJson, SCOPES)
  30. creds = flow.run_local_server(port=0)
  31. with open(pathPickle, 'wb') as token:
  32. pickle.dump(creds, token)
  33. service = build('calendar', 'v3', credentials=creds)
  34. # Call the Calendar API
  35. dtMin = datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
  36. dtMax = (datetime.utcnow()+ timedelta(days=7)).isoformat() + 'Z' # 'Z' indicates UTC time
  37. print('Getting the upcoming 10 events')
  38. events_result = service.events().list(calendarId='primary', timeMin=dtMin,
  39. timeMax=dtMax, singleEvents=True,
  40. orderBy='startTime').execute()
  41. objEventoRet = events_result.get('items', [])
  42. eventos=[]
  43. for evento in objEventoRet:
  44. if evento['kind']=='calendar#event':
  45. info = {}
  46. info['original']=evento
  47. info['titulo']=""
  48. if 'summary' in evento:
  49. info['titulo']=evento['summary']
  50. if 'etag' in evento:
  51. info['etag']=evento['etag']
  52. info['id']=evento['id']
  53. info['status']=evento['status']
  54. info['inicio']=datetime.fromisoformat(evento['start'].get('dateTime', evento['start'].get('date'))).strftime("%Y-%m-%d %H:%M:%S")
  55. info['fim']=datetime.fromisoformat(evento['end'].get('dateTime', evento['end'].get('date'))).strftime("%Y-%m-%d %H:%M:%S")
  56. if 'attendees' in evento:
  57. motoras = evento['attendees']
  58. for motora in motoras:
  59. if motora['email']!="suporte@mobees.com.br":
  60. info['email_motorista']=motora['email']
  61. info['status_motorista']=motora['responseStatus']
  62. break
  63. eventos.append(info)
  64. print(eventos)
  65. strFilter = 'cpf_motorista=04275894790'
  66. events_result = service.events().list(calendarId='primary',privateExtendedProperty=strFilter,
  67. singleEvents=True).execute()
  68. if len(events_result.get('items', []))>0:
  69. eventoCpf = events_result.get('items', [])[0]
  70. print(eventoCpf)
  71. """if len(eventosLivres)>0:
  72. eventoMarcacao = eventosLivres[0]
  73. objEvento = service.events().get(calendarId='primary', eventId=eventoMarcacao['id']).execute()
  74. objEvento['summary']='IDT:12345678'
  75. objEvento['colorId']='10'
  76. objEvento['attendees']=[{'email': 'vidal@mobees.com.br', 'responseStatus': 'needsAction'}, {'email': 'suporte@mobees.com.br', 'organizer': True, 'self': True, 'responseStatus': 'accepted'}]
  77. print(str(objEvento))
  78. objEvento = service.events().update(calendarId='primary', eventId=eventoMarcacao['id'], body=objEvento).execute()"""
  79. if __name__ == '__main__':
  80. main()