Skip to content

Commit

Permalink
Added markdown support to google plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Sandro Bonazzola <[email protected]>
  • Loading branch information
sandrobonazzola committed Jun 28, 2024
1 parent 017887f commit 909f34a
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions did/plugins/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ def authorized_http(client_id, client_secret, apps, file=None):
class GoogleCalendar(object):
""" Google Calendar functions """

def __init__(self, http):
def __init__(self, http, parent):
self.service = discovery.build("calendar", "v3", http=http)
self.parent = parent

def events(self, **kwargs):
""" Fetch events meeting specified criteria """
events_result = self.service.events().list(**kwargs).execute()
return [Event(event) for event in events_result.get("items", [])]
return [Event(event, self.parent.options.format)
for event in events_result.get("items", [])]


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -145,13 +147,21 @@ def events(self, **kwargs):
class Event(object):
""" Google Calendar Event """

def __init__(self, dict):
def __init__(self, in_dict, out_format):
""" Create Event object from dict returned by Google API """
self.__dict__ = dict
self.__dict__ = in_dict
self._format = out_format

def __str__(self):
""" String representation """
return self.summary if hasattr(self, "summary") else "(No title)"
date = (
self.start["date"] if "date" in self.start
else self.start["dateTime"][:10]
)
if self._format == "markdown":
return f"{date} - *{self.summary}*"
else:
return self.summary

def __getitem__(self, name):
return self.__dict__.get(name, None)
Expand Down Expand Up @@ -180,13 +190,15 @@ def attended_by(self, email):
class GoogleTasks(object):
""" Google Tasks functions """

def __init__(self, http):
def __init__(self, http, parent):
self.service = discovery.build("tasks", "v1", http=http)
self.parent = parent

def tasks(self, **kwargs):
""" Fetch tasks specified criteria """
tasks_result = self.service.tasks().list(**kwargs).execute()
return [Task(task) for task in tasks_result.get("items", [])]
return [Task(task, self.parent.options.format)
for task in tasks_result.get("items", [])]


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -196,12 +208,15 @@ def tasks(self, **kwargs):
class Task(object):
""" Google Tasks task """

def __init__(self, dict):
def __init__(self, in_dict, out_format):
""" Create Task object from dict returned by Google API """
self.__dict__ = dict
self.__dict__ = in_dict
self._format = out_format

def __str__(self):
""" String representation """
# TODO: decide if there's something different we want
# to return in markdown
return self.title if hasattr(self, "title") else "(No title)"

def __getitem__(self, name):
Expand Down Expand Up @@ -234,7 +249,7 @@ def events(self):
calendarId="primary", singleEvents=True, orderBy="startTime",
timeMin=self.since, timeMax=self.until)
self._events = [event for event in self._events
if str(event) not in self.parent.skip]
if str(event.summary) not in self.parent.skip]
return self._events

@property
Expand Down Expand Up @@ -307,8 +322,8 @@ def __init__(self, option, name=None, parent=None, user=None):
self.skip = config.get("skip", [])

http = authorized_http(client_id, client_secret, apps, storage)
self.calendar = GoogleCalendar(http)
self.tasks = GoogleTasks(http)
self.calendar = GoogleCalendar(http, self)
self.tasks = GoogleTasks(http, self)

self.stats = [
GoogleEventsOrganized(
Expand Down

0 comments on commit 909f34a

Please sign in to comment.