from __future__ import print_function import pickle import os.path from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from apiclient import discovery from datetime import datetime,timedelta import httplib2 import os # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/calendar','https://www.googleapis.com/auth/calendar.events'] def main(): pathJson = "C:\\Users\\chris\\OneDrive\\Documentos\\GitHub\\MOBEES-BR\\Backend\\dsp\\adm\\auth\\credentials_calendar.json" pathPickle = "C:\\Users\\chris\\OneDrive\\Documentos\\GitHub\\MOBEES-BR\\Backend\\dsp\\adm\\auth\\token_calendar.pickle" creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists(pathPickle): with open(pathPickle, 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( pathJson, SCOPES) creds = flow.run_local_server(port=0) with open(pathPickle, 'wb') as token: pickle.dump(creds, token) service = build('calendar', 'v3', credentials=creds) # Call the Calendar API dtMin = datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time dtMax = (datetime.utcnow()+ timedelta(days=7)).isoformat() + 'Z' # 'Z' indicates UTC time print('Getting the upcoming 10 events') events_result = service.events().list(calendarId='primary', timeMin=dtMin, timeMax=dtMax, singleEvents=True, orderBy='startTime').execute() objEventoRet = events_result.get('items', []) eventos=[] for evento in objEventoRet: if evento['kind']=='calendar#event': info = {} info['original']=evento info['titulo']="" if 'summary' in evento: info['titulo']=evento['summary'] if 'etag' in evento: info['etag']=evento['etag'] info['id']=evento['id'] info['status']=evento['status'] info['inicio']=datetime.fromisoformat(evento['start'].get('dateTime', evento['start'].get('date'))).strftime("%Y-%m-%d %H:%M:%S") info['fim']=datetime.fromisoformat(evento['end'].get('dateTime', evento['end'].get('date'))).strftime("%Y-%m-%d %H:%M:%S") if 'attendees' in evento: motoras = evento['attendees'] for motora in motoras: if motora['email']!="suporte@mobees.com.br": info['email_motorista']=motora['email'] info['status_motorista']=motora['responseStatus'] break eventos.append(info) print(eventos) strFilter = 'cpf_motorista=04275894790' events_result = service.events().list(calendarId='primary',privateExtendedProperty=strFilter, singleEvents=True).execute() if len(events_result.get('items', []))>0: eventoCpf = events_result.get('items', [])[0] print(eventoCpf) """if len(eventosLivres)>0: eventoMarcacao = eventosLivres[0] objEvento = service.events().get(calendarId='primary', eventId=eventoMarcacao['id']).execute() objEvento['summary']='IDT:12345678' objEvento['colorId']='10' objEvento['attendees']=[{'email': 'vidal@mobees.com.br', 'responseStatus': 'needsAction'}, {'email': 'suporte@mobees.com.br', 'organizer': True, 'self': True, 'responseStatus': 'accepted'}] print(str(objEvento)) objEvento = service.events().update(calendarId='primary', eventId=eventoMarcacao['id'], body=objEvento).execute()""" if __name__ == '__main__': main()