ops_utils.google_calendar

Module to interact with Google Calendar API.

 1"""Module to interact with Google Calendar API."""
 2from datetime import datetime, timedelta
 3from typing import List, Dict
 4from googleapiclient.discovery import build
 5from google.oauth2 import service_account
 6
 7
 8class GoogleCalendar:
 9    """Class to interact with Google Calendar API."""
10
11    _SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
12
13    def __init__(self, service_account_info: dict):
14        """
15        Initialize the GoogleCalendar instance using the user's credentials.
16
17        **Args:**
18        - service_account_info (dict): A dictionary containing the service account credentials.
19        """
20        credentials = service_account.Credentials.from_service_account_info(
21            service_account_info, scopes=self._SCOPES
22        )
23        self._service = build(
24            serviceName="calendar",
25            version="v3",
26            credentials=credentials,
27            cache_discovery=False
28        )
29
30    @staticmethod
31    def _create_calendar_string_from_datetime(dt: datetime) -> str:
32        """
33        Create a string representation of a datetime object.
34
35        **Args:**
36        - dt (datetime): A datetime object.
37        """
38        return (datetime(dt.year, dt.month, dt.day, 00, 00)).isoformat() + 'Z'
39
40    def get_events(self, calendar_id: str, days_back: int, days_ahead: int) -> List[Dict]:
41        """
42        Get all events from the specified calendar for the last `x` days.
43
44        **Args:**
45        - calendar_id (str): The ID of the Google Calendar.
46        - days_back (int): Number of days in the past to retrieve events for.
47        - days_ahead (int): Number of days in the future to retrieve events for.
48
49        **Returns:**
50        - list[dict]: A list of events with their details.
51        """
52        now = datetime.now()
53        time_min = self._create_calendar_string_from_datetime(now - timedelta(days=days_back))
54        time_max = self._create_calendar_string_from_datetime(now + timedelta(days=days_ahead))
55
56        events_result = self._service.events().list(
57            calendarId=calendar_id,
58            timeMin=time_min,
59            timeMax=time_max,
60            maxResults=2500,
61            singleEvents=True,
62            orderBy='startTime').execute()
63        # Return list of dict if results, otherwise empty list
64        return events_result.get('items', [])
class GoogleCalendar:
 9class GoogleCalendar:
10    """Class to interact with Google Calendar API."""
11
12    _SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
13
14    def __init__(self, service_account_info: dict):
15        """
16        Initialize the GoogleCalendar instance using the user's credentials.
17
18        **Args:**
19        - service_account_info (dict): A dictionary containing the service account credentials.
20        """
21        credentials = service_account.Credentials.from_service_account_info(
22            service_account_info, scopes=self._SCOPES
23        )
24        self._service = build(
25            serviceName="calendar",
26            version="v3",
27            credentials=credentials,
28            cache_discovery=False
29        )
30
31    @staticmethod
32    def _create_calendar_string_from_datetime(dt: datetime) -> str:
33        """
34        Create a string representation of a datetime object.
35
36        **Args:**
37        - dt (datetime): A datetime object.
38        """
39        return (datetime(dt.year, dt.month, dt.day, 00, 00)).isoformat() + 'Z'
40
41    def get_events(self, calendar_id: str, days_back: int, days_ahead: int) -> List[Dict]:
42        """
43        Get all events from the specified calendar for the last `x` days.
44
45        **Args:**
46        - calendar_id (str): The ID of the Google Calendar.
47        - days_back (int): Number of days in the past to retrieve events for.
48        - days_ahead (int): Number of days in the future to retrieve events for.
49
50        **Returns:**
51        - list[dict]: A list of events with their details.
52        """
53        now = datetime.now()
54        time_min = self._create_calendar_string_from_datetime(now - timedelta(days=days_back))
55        time_max = self._create_calendar_string_from_datetime(now + timedelta(days=days_ahead))
56
57        events_result = self._service.events().list(
58            calendarId=calendar_id,
59            timeMin=time_min,
60            timeMax=time_max,
61            maxResults=2500,
62            singleEvents=True,
63            orderBy='startTime').execute()
64        # Return list of dict if results, otherwise empty list
65        return events_result.get('items', [])

Class to interact with Google Calendar API.

GoogleCalendar(service_account_info: dict)
14    def __init__(self, service_account_info: dict):
15        """
16        Initialize the GoogleCalendar instance using the user's credentials.
17
18        **Args:**
19        - service_account_info (dict): A dictionary containing the service account credentials.
20        """
21        credentials = service_account.Credentials.from_service_account_info(
22            service_account_info, scopes=self._SCOPES
23        )
24        self._service = build(
25            serviceName="calendar",
26            version="v3",
27            credentials=credentials,
28            cache_discovery=False
29        )

Initialize the GoogleCalendar instance using the user's credentials.

Args:

  • service_account_info (dict): A dictionary containing the service account credentials.
def get_events(self, calendar_id: str, days_back: int, days_ahead: int) -> List[Dict]:
41    def get_events(self, calendar_id: str, days_back: int, days_ahead: int) -> List[Dict]:
42        """
43        Get all events from the specified calendar for the last `x` days.
44
45        **Args:**
46        - calendar_id (str): The ID of the Google Calendar.
47        - days_back (int): Number of days in the past to retrieve events for.
48        - days_ahead (int): Number of days in the future to retrieve events for.
49
50        **Returns:**
51        - list[dict]: A list of events with their details.
52        """
53        now = datetime.now()
54        time_min = self._create_calendar_string_from_datetime(now - timedelta(days=days_back))
55        time_max = self._create_calendar_string_from_datetime(now + timedelta(days=days_ahead))
56
57        events_result = self._service.events().list(
58            calendarId=calendar_id,
59            timeMin=time_min,
60            timeMax=time_max,
61            maxResults=2500,
62            singleEvents=True,
63            orderBy='startTime').execute()
64        # Return list of dict if results, otherwise empty list
65        return events_result.get('items', [])

Get all events from the specified calendar for the last x days.

Args:

  • calendar_id (str): The ID of the Google Calendar.
  • days_back (int): Number of days in the past to retrieve events for.
  • days_ahead (int): Number of days in the future to retrieve events for.

Returns:

  • list[dict]: A list of events with their details.