google module

Functions

authenticate_google_app(
credentials_path='gmail_credentials.json',
token_path='gmail_token.json',
SCOPES=None,
service_name=None,
service_version=None)

Authenticates and initializes a Google API service.

Parameters:
  • credentials_path (str) – Path to the credentials file for the Google API.

  • token_path (str) – Path to the token file for storing user authentication.

  • SCOPES (list) – List of scopes for the Google API service.

  • service_name (str) – Name of the Google API service (e.g., ‘gmail’, ‘calendar’).

  • service_version (str) – Version of the Google API service (e.g., ‘v1’, ‘v3’).

Returns:

Authenticated Google API service object.

Return type:

googleapiclient.discovery.Resource

Classes

gmail(
credentials_path='credentials.json',
token_path='token.json',
sender=None)

A class for managing Gmail messages using the Google API.

Parameters:
  • credentials_path (str) – Path to the Google API credentials file. Default is ‘credentials.json’.

  • token_path (str) – Path to the token file for authentication. Default is ‘token.json’.

  • sender (str) – Default sender email address. Default is None.

Methods

add_message_label(
message=None,
message_id=None,
label_names=None,
label_ids=None)

Adds one or more labels to a Gmail message.

Parameters:
  • message (dict) – Message metadata dictionary. Default is None.

  • message_id (str) – ID of the message to label.

  • label_names (str or list) – Name(s) of the label(s) to add.

  • label_ids (str or list) – ID(s) of the label(s) to add.

Returns:

None

delete_message(
message=None,
message_id=None)

Permanently deletes a Gmail message.

Parameters:
  • message (dict) – Message metadata dictionary. Default is None.

  • message_id (str) – ID of the message to delete.

Returns:

None

google.get_all_labels()

Retrieves all Gmail labels for the user.

Returns:

DataFrame containing label names and metadata.

Return type:

pandas.DataFrame

get_from_sender(
sender,
label_name=None,
label_id=None)

Finds all messages from a specific sender.

Parameters:
  • sender (str) – Email address of the sender to search for.

  • label_name (str) – Name of the Gmail label to filter messages by. Default is None.

  • label_id (str) – ID of the Gmail label to filter messages by. Default is None.

Returns:

List of message metadata dictionaries.

Return type:

list

make_message(
sender=None,
to=None,
cc=None,
bcc=None,
subject='No subject',
plain_text=None,
html_text=None,
markdown_text=None,
send=True,
attachments=None)

Creates and optionally sends a Gmail message.

Parameters:
  • sender (str) – Email address of the sender. Default is None.

  • to (str or list) – Recipient email address(es). Default is None.

  • cc (str) – CC email address(es). Default is None.

  • bcc (str) – BCC email address(es). Default is None.

  • subject (str) – Email subject. Default is ‘No subject’.

  • plain_text (str) – Plain text content of the email. Default is None.

  • html_text (str) – HTML content of the email. Default is None.

  • markdown_text (str) – Markdown content for the email. Default is None.

  • send (bool) – Whether to send the email immediately. Default is True.

  • attachments (str or list) – Path(s) to attachment files. Default is None.

Returns:

Sent message metadata if send=True, else draft metadata.

Return type:

dict

trash_message(
message=None,
message_id=None)

Moves a Gmail message to the trash.

Parameters:
  • message (dict) – Message metadata dictionary. Default is None.

  • message_id (str) – ID of the message to delete. Default is None.

Returns:

None

move_message(
message=None,
message_id=None,
old_label_name=None,
new_label_name=None)

Moves a Gmail message from one label to another.

Parameters:
  • message (dict) – Message metadata dictionary. Default is None.

  • message_id (str) – ID of the message to move. Default is None.

  • old_label_name (str) – Name of the current label.

  • new_label_name (str) – Name of the target label.

Returns:

None

calendar(
credentials_path='credentials.json',
token_path='token.json')

Class for interacting with Google Calendar. This class provides methods to manage calendars, add events, import/export events, and parse .ics files.

Parameters:
  • credentials_path (str) – Path to the Google Calendar credentials file. Default is ‘credentials.json’.

  • token_path (str) – Path to the token file for authentication. Default is ‘token.json’.

Methods

add_event(
start,
end=None,
duration=0,
duration_units='H',
title='Event',
description=None,
calendar_name=None,
calendar_id=None,
time_zone=None,
all_day=False)

Adds a single event to a Google Calendar.

Parameters:
  • start (str) – Start time of the event (e.g., “January 4, 2022 4pm”).

  • end (str) – End time of the event. Default is None.

  • duration (float) – Duration of the event in units specified by duration_units. Default is 0.

  • duration_units (str) – Units for event duration (e.g., ‘H’ for hours). Default is ‘H’.

  • title (str) – Title of the event. Default is ‘Event’.

  • description (str) – Description of the event. Default is None.

  • calendar_name (str) – Name of the calendar to add the event to. Default is None.

  • calendar_id (str) – ID of the calendar to add the event to. Default is None.

  • time_zone (str) – Time zone of the event (e.g., “America/New_York”). Default is None.

  • all_day (bool) – If True, creates an all-day event. Default is False.

Raises:

ValueError – If calendar_name and calendar_id do not reference the same calendar.

Returns:

None

delete_events(
events=None,
calendar_name=None,
calendar_id=None)

Deletes events from a Google Calendar.

Parameters:
  • events (list) – List of event dictionaries to delete.

  • calendar_name (str) – Name of the calendar containing the events. Default is None.

  • calendar_id (str) – ID of the calendar containing the events. Default is None.

Raises:

ValueError – If calendar_name and calendar_id do not reference the same calendar.

Returns:

None

export_to_ics(
event,
ics_path='event.ics')

Exports a Google Calendar event to an ICS file format.

Parameters:
  • event (dict) – Dictionary representing a Google Calendar event.

  • ics_path (str) – File path to save the ICS file. Default is ‘event.ics’.

Raises:
  • KeyError – If required keys (‘summary’, ‘start’, ‘end’) are missing in the event.

  • ValueError – If datetime parsing fails or an invalid timezone is specified.

Returns:

None