Added a max_chars parameter to be able to control the widget width.
Also moved the try block a bit further up to catch network errors.
This commit is contained in:
parent
8eb2545eed
commit
b327162f3b
1 changed files with 32 additions and 25 deletions
|
@ -3,7 +3,9 @@
|
|||
Events that are set as 'all-day' will not be shown.
|
||||
|
||||
Requires credentials.json from a google api application where the google calendar api is installed.
|
||||
On first time run the browser will open and google will ask for permission for this app to access the google calendar and then save a .gcalendar_token.json file to the credentials_path directory which stores this permission.
|
||||
On first time run the browser will open and google will ask for permission for this app to access
|
||||
the google calendar and then save a .gcalendar_token.json file to the credentials_path directory
|
||||
which stores this permission.
|
||||
|
||||
A refresh is done every 15 minutes.
|
||||
|
||||
|
@ -12,6 +14,7 @@ Parameters:
|
|||
* gcalendar.date_format: Format date output. Defaults to "%d.%m.%y".
|
||||
* gcalendar.credentials_path: Path to credentials.json. Defaults to "~/".
|
||||
* gcalendar.locale: locale to use rather than the system default.
|
||||
* gcalendar.max_chars: Maximum of characters to display.
|
||||
|
||||
Requires these pip packages:
|
||||
* google-api-python-client >= 1.8.0
|
||||
|
@ -51,6 +54,13 @@ class Module(core.module.Module):
|
|||
self.__credentials = os.path.join(self.__credentials_path, "credentials.json")
|
||||
self.__token = os.path.join(self.__credentials_path, ".gcalendar_token.json")
|
||||
|
||||
self.__max_chars = self.parameter("max_chars", None)
|
||||
try:
|
||||
if self.__max_chars:
|
||||
self.__max_chars = int(self.__max_chars)
|
||||
except ValueError:
|
||||
self.__max_chars = None
|
||||
|
||||
l = locale.getdefaultlocale()
|
||||
if not l or l == (None, None):
|
||||
l = ("en_US", "UTF-8")
|
||||
|
@ -63,10 +73,8 @@ class Module(core.module.Module):
|
|||
def first_event(self, widget):
|
||||
SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"]
|
||||
|
||||
"""Shows basic usage of the Google Calendar API.
|
||||
Prints the start and name of the next 10 events on the user's calendar.
|
||||
"""
|
||||
creds = None
|
||||
try:
|
||||
# The file token.json stores the user's access and refresh tokens, and is
|
||||
# created automatically when the authorization flow completes for the first
|
||||
# time.
|
||||
|
@ -85,7 +93,6 @@ class Module(core.module.Module):
|
|||
with open(self.__token, "w") as token:
|
||||
token.write(creds.to_json())
|
||||
|
||||
try:
|
||||
service = build("calendar", "v3", credentials=creds)
|
||||
|
||||
# Call the Calendar API
|
||||
|
@ -137,7 +144,7 @@ class Module(core.module.Module):
|
|||
.strftime(f"{self.__time_format}"),
|
||||
gevent["summary"],
|
||||
)
|
||||
)
|
||||
)[: self.__max_chars]
|
||||
else:
|
||||
return str(
|
||||
"%s %s"
|
||||
|
@ -147,7 +154,7 @@ class Module(core.module.Module):
|
|||
.strftime(f"{self.__date_format} {self.__time_format}"),
|
||||
gevent["summary"],
|
||||
)
|
||||
)
|
||||
)[: self.__max_chars]
|
||||
return "No upcoming events found."
|
||||
|
||||
except:
|
||||
|
|
Loading…
Reference in a new issue