<jupyter_start><jupyter_text>[View in Colaboratory](https://colab.research.google.com/github/hlecuanda/jupyter-notebooks-of-all-kinds/blob/master/CalendarEvents.ipynb) Auth<jupyter_code>from google.colab import auth
from googleapiclient.discovery import build
from apiclient.http import MediaIoBaseDownload
from google.oauth2 import service_account
import datetime

try:
    from google.cloud import storage
    from googleapiclient import discovery
    
except:
    !pip install --upgrade google-cloud-storage google-api-client
    from google.cloud import storage
    from googleapiclient import discovery

def download_blob(bucket_name, source_blob_name, destination_file_name):
    """Downloads a blob from the bucket."""        
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)

    print('Blob {} downloaded to {}.'.format(
        source_blob_name,
        destination_file_name))

def reset_auth():
    !rm -v = *.json
    
STORAGE_BUCKET="archive.h-lo.me"
STORAGE_PATH = "credentials/"
PROJECT="colab-datalab-nbooks"
SA_FILE="colab-datalab-nbooks-0b23b6ee2449.json"

auth.authenticate_user()
storage_client = storage.Client(PROJECT)       

SCOPES = ['https://www.googleapis.com/auth/calendar']

!rm '$SA_FILE'

download_blob(STORAGE_BUCKET, STORAGE_PATH+SA_FILE, SA_FILE)

credentials = service_account.Credentials.from_service_account_file(
        SA_FILE, scopes=SCOPES).with_subject('<EMAIL>')

# drive  = discovery.build('drive', 'v3', credentials=credentials)
# sheets = discovery.build('sheets','v4', credentials=credentials)
calendar = discovery.build('calendar','v3', credentials=credentials)<jupyter_output><empty_output><jupyter_text>Calendar<jupyter_code>now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
hago= datetime.datetime(tsnow.year,
                        tsnow.month,
                        1,
                        0).isoformat() + 'Z'

CAL='<EMAIL>'

events_result = calendar.events().list(calendarId=CAL, 
                                       timeMin=hago,
                                     # maxResults=10, 
                                       singleEvents=True,
                                       orderBy='startTime').execute()
events = events_result.get('items', [])

if not events:
    print('No upcoming events found.')
for event in events:
    start = event['start'].get('dateTime', event['start'].get('date'))
    print(start, event['summary'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1158966327329679, 'fraction_numerical': 0.006264682850430697, 'mean_word_length': 3.412780656303972, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '550430', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 800, 'n_tokens_neox': 780, 'n_words': 136, 'path': 'CalendarEvents.ipynb', 'repo_name': 'hlecuanda/jupyter-notebooks-of-all-kinds', 'size': '2624'}