def delete_event(gcalendar: GCalendar, id: str): try: res = gcalendar.delete_event(id) return success_response(res) except Exception as e: message = str(e) return error_response(f"Cannot delete event. Reason: {message}")
def event(gcalendar: GCalendar, id: str): try: event = gcalendar.get_event(id) return success_response(event) except Exception as e: message = str(e) return error_response(f"Cannot get event. Reason: {message}")
def events(gcalendar: GCalendar): args = request.args if "summary" in args or "description" in args or "range_days" in args: summary = args.get("summary") if "summary" in args else None description = args.get( "description") if "description" in args else None if "days_range" in args: try: days_range = int(args.get("days_range")) except: return error_response("Cannot parse days_range") else: days_range = 30 events = gcalendar.find_events_with(summary=summary, description=description, not_before_days=days_range) else: events = gcalendar.get_events() return success_response(events)
def configure(gcalendar: GCalendar = None) -> Flask: if not gcalendar: logging.warn("Trying to import gcalendar...") load_dotenv() GCALENDAR_ID = getenv("GCALENDAR_ID") GTIMEZONE = getenv("GTIMEZONE") gcalendar = GCalendar(GCALENDAR_ID, timezone=GTIMEZONE) if gcalendar: api.config["gcalendar"] = gcalendar else: raise Exception( "Cannot configure gcalendar. (Probably missing some env)") return api
def main(): """ Retrieve Google Calendar events. """ parser = argparse.ArgumentParser( prog='gcalendar', description="Read your Google Calendar events from terminal.") group = parser.add_mutually_exclusive_group() group.add_argument("--list-calendars", action="store_true", help="list all calendars from the Google account") group.add_argument("--list-accounts", action="store_true", help="list the id of gcalendar accounts") group.add_argument("--status", action="store_true", help="print the status of the gcalendar account") group.add_argument("--reset", action="store_true", help="reset the the account") parser.add_argument("--calendar", type=str, default=["*"], nargs="*", help="calendars to list events from") parser.add_argument("--no-of-days", type=str, default="7", help="number of days to include") parser.add_argument( "--account", type=validate_account_id, default="default", help="an alphanumeric name to uniquely identify the account") parser.add_argument("--output", choices=["txt", "json"], default="txt", help="output format") parser.add_argument("--client-id", type=str, help="the Google client id") parser.add_argument("--client-secret", type=str, help="the Google client secret") parser.add_argument('--version', action='version', version='%(prog)s ' + VERSION) parser.add_argument("--debug", action="store_true", help="run gcalendar in debug mode") args = parser.parse_args() # Create the config folder if not exists if not os.path.exists(CONFIG_DIRECTORY): os.mkdir(CONFIG_DIRECTORY) account_id = args.account storage_path = join(CONFIG_DIRECTORY, account_id + TOKEN_FILE_SUFFIX) if args.list_accounts: # --list-accounts print_list(list_accounts(), args.output) return 0 elif args.reset: # --reset status = reset_account(account_id, storage_path) print_status(status, args.output) return 0 elif args.status: # --status if os.path.exists(storage_path): if GCalendar.is_authorized(storage_path): status = "Authorized" else: status = "Token Expired" else: status = "Not authenticated" print_status(status, args.output) return 0 else: # Extract arguments no_of_days = int(args.no_of_days) client_id = args.client_id client_secret = args.client_secret selected_calendars = [x.lower() for x in args.calendar] current_time = datetime.now(timezone.utc).astimezone() time_zone = current_time.tzinfo start_time = str(current_time.isoformat()) end_time = str( (current_time + relativedelta(days=no_of_days)).isoformat()) if not client_id or not client_secret: client_id = DEFAULT_CLIENT_ID client_secret = DEFAULT_CLIENT_SECRET try: g_calendar = GCalendar(client_id, client_secret, account_id, storage_path) if args.list_calendars: print_list(g_calendar.list_calendars(), args.output) else: calendar_events = g_calendar.list_events( selected_calendars, start_time, end_time, time_zone) print_events(calendar_events, args.output) return 0 except clientsecrets.InvalidClientSecretsError as ex: handle_error(ex, "Invalid Client Secrets", args.output, args.debug) except client.AccessTokenRefreshError as ex: handle_error(ex, "Failed to refresh access token", args.output, args.debug) except HttpLib2Error as ex: if "Unable to find the server at" in str(ex): msg = "Unable to find the Google Calendar server. Please check your connection." else: msg = "Failed to connect Google Calendar" handle_error(ex, msg, args.output, args.debug) except HttpError as ex: if "Too Many Requests" in str(ex): msg = "You have reached your request quota limit. Please try gcalendar after a few minutes." else: msg = "Failed to connect Google Calendar" handle_error(ex, msg, args.output, args.debug) except BaseException as ex: handle_error(ex, "Failed to connect Google Calendar", args.output, args.debug) return -1
from os import getenv from datetime import datetime, date, tzinfo from gcalendar.gcalendar import GCalendar # %% load_dotenv() CALENDAR_ID = getenv("CALENDAR_ID") PICKLE_PATH = getenv("GTOKEN_PICKLE_PATH") TIMEZONE = getenv("GTIMEZONE") CREDENTIALS_PATH = getenv("GCREDENTIALS_PATH") assert CALENDAR_ID is not None # %% calendar = GCalendar( CALENDAR_ID, pickle_path=PICKLE_PATH, credentials_path=CREDENTIALS_PATH, timezone=TIMEZONE, ) # %% # Get list events dates = [] for event in calendar.get_events(): dates.append(event["created"]) if "summary" in event: print(event["summary"]) else: print(event) print(sorted(dates))
#!/usr/bin/env python3 import logging from os import getenv from dotenv import load_dotenv from gcalendar.api import api, configure from gcalendar.gcalendar import GCalendar if __name__ == "__main__": load_dotenv() GCALENDAR_ID = getenv("GCALENDAR_ID") GTIMEZONE = getenv("GTIMEZONE") app = api if GCALENDAR_ID: gcalendar = GCalendar(GCALENDAR_ID, timezone=GTIMEZONE) app = configure(gcalendar) else: logging.warn("[!] Missing envs") # In production use WGSI! app.run()