from __future__ import print_function
import datetime
import pickle
import os.path
import argparse
import hy
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from dateutil.parser import parse as dateparse

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']

def events_from_ics(calendar_names, start_time, n):
    import recurring_ical_events
    import requests
    from icalendar import Calendar, Event
    
    event_objs=[]

    print(calendar_names)
    for name in calendar_names:
        r=requests.get(name)
        cal=Calendar.from_ical(r.content)
        calname=cal['X-WR-CALNAME']
        print(f'Getting events for calendar with name {calname}...')
        events=recurring_ical_events.of(cal).between(start_time,datetime.datetime.now())
        for event in events:
            start = event['DTSTART'].dt.isoformat()
            end = event['DTEND'].dt.isoformat()
            print(start, event['SUMMARY'])
            event_objs.append({'start': start,
                               'end': end,
                               'summary': event['SUMMARY'],
                               'calendar_name': calname,
                               'fullname': calname + '/' + event['SUMMARY']})
    return event_objs


def events_from_calendars(calendar_names, start_time, n):
    # Much of this function is taken directly from Google Calendar quickstart documentation 
    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('token.pickle'):
        with open('token.pickle', '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(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('calendar', 'v3', credentials=creds)

    event_objs = []

    # Call the Calendar API
    print('Getting calendars ...')
    all_calendars = service.calendarList().list().execute()["items"]
    start_time_formatted = start_time.isoformat() + 'Z'
    print(start_time_formatted)
    for name in calendar_names:
        calendar_id = None
        for cal in all_calendars:
                if cal["summary"] == name:
                    calendar_id = cal["id"]
        print(f'Getting events for calendar with name {name} and id {calendar_id}...')
        events_result = service.events().list(calendarId=calendar_id,
                                              timeMin=start_time_formatted,
                                              maxResults=n,
                                              singleEvents=True,
                                              orderBy='startTime').execute()
        events = events_result.get('items', [])

        for event in events:
            start = event['start'].get('dateTime', event['start'].get('date'))
            end = event['end'].get('dateTime', event['end'].get('date'))
            print(start, event['summary'])
            event_objs.append({'start': start,
                               'end': end,
                               'summary': event['summary'],
                               'calendar_name': name,
                               'fullname': name + '/' + event['summary']})

    return event_objs

from __future__ import print_function
import datetime
import pickle
import os.path
import argparse
import hy
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from dateutil.parser import parse as dateparse

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']

def events_from_ics(calendar_names, start_time, n):
    import recurring_ical_events
    import requests
    from icalendar import Calendar, Event
    
    event_objs=[]

    print(calendar_names)
    for name in calendar_names:
        r=requests.get(name)
        cal=Calendar.from_ical(r.content)
        calname=cal['X-WR-CALNAME']
        print(f'Getting events for calendar with name {calname}...')
        events=recurring_ical_events.of(cal).between(start_time,datetime.datetime.now())
        for event in events:
            start = event['DTSTART'].dt.isoformat()
            end = event['DTEND'].dt.isoformat()
            print(start, event['SUMMARY'])
            event_objs.append({'start': start,
                               'end': end,
                               'summary': event['SUMMARY'],
                               'calendar_name': calname,
                               'fullname': calname + '/' + event['SUMMARY']})
    return event_objs


def events_from_calendars(calendar_names, start_time, n):
    # Much of this function is taken directly from Google Calendar quickstart documentation 
    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('token.pickle'):
        with open('token.pickle', '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(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('calendar', 'v3', credentials=creds)

    event_objs = []

    # Call the Calendar API
    print('Getting calendars ...')
    all_calendars = service.calendarList().list().execute()["items"]
    start_time_formatted = start_time.isoformat() + 'Z'
    print(start_time_formatted)
    for name in calendar_names:
        calendar_id = None
        for cal in all_calendars:
                if cal["summary"] == name:
                    calendar_id = cal["id"]
        print(f'Getting events for calendar with name {name} and id {calendar_id}...')
        events_result = service.events().list(calendarId=calendar_id,
                                              timeMin=start_time_formatted,
                                              maxResults=n,
                                              singleEvents=True,
                                              orderBy='startTime').execute()
        events = events_result.get('items', [])

        for event in events:
            start = event['start'].get('dateTime', event['start'].get('date'))
            end = event['end'].get('dateTime', event['end'].get('date'))
            print(start, event['summary'])
            event_objs.append({'start': start,
                               'end': end,
                               'summary': event['summary'],
                               'calendar_name': name,
                               'fullname': name + '/' + event['summary']})

    return event_objs

# If modifying these scopes, delete the file token.pickle. # Much of this function is taken directly from Google Calendar quickstart documentation # 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 there are no (valid) credentials available, let the user log in. # Save the credentials for the next run # Call the Calendar API