Example #1
0
 def events(self):
     store = CalCalendarStore.defaultCalendarStore()
     # Pull all events starting now from all calendars in the CalendarStore
     allEventsPredicate = (
         CalCalendarStore.eventPredicateWithStartDate_endDate_calendars_(
             NSDate.date(), NSDate.distantFuture(), store.calendars()))
     return store.eventsWithPredicate_(allEventsPredicate)
Example #2
0
def fetch_events(request_from_server=False):
    global memory_cache
    logger.info('fetch_events: called')

    if memory_cache:
        logger.info('fetch_events: checking memory cache')
    else:
        logger.info('fetch_events: checking disk cache')
        memory_cache = cache.fetch(config.CALENDAR_CACHE_PATH)

    content = cache.content(memory_cache, config.CALENDAR_CACHE_LIFETIME,
                            request_from_server)
    if content:
        return content

    store = CalCalendarStore.defaultCalendarStore()
    cals = []
    for cal in store.calendars():
        if cal.title() in config.CALENDAR_CALENDARS:
            cals.append(cal)
        logger.info(cal.title())

    cst = tz.gettz('America/Chicago')
    today = datetime.now().date()
    start_dt = datetime(today.year, today.month, today.day, tzinfo=cst)
    end_dt = start_dt + timedelta(180)

    start_int = int(start_dt.strftime("%s"))
    end_int = int(end_dt.strftime("%s"))
    start = NSDate.dateWithTimeIntervalSince1970_(start_int)
    end = NSDate.dateWithTimeIntervalSince1970_(end_int)

    formatted_results = {}

    for cal in cals:
        events = []
        pred = CalCalendarStore.eventPredicateWithStartDate_endDate_calendars_(
            start, end, [cal])
        for event in store.eventsWithPredicate_(pred):
            s = event._.startDate.timeIntervalSince1970()
            e = event._.endDate.timeIntervalSince1970()
            events.append({'name': event._.title, 'start': s, 'end': e})
        formatted_results[cal.title()] = events

    memory_cache = cache.save(formatted_results, config.CALENDAR_CACHE_PATH)
    return formatted_results
Example #3
0
def main():
    startDate = date(year=now.year, month=now.month, day=now.day - BEFORE)
    endDate = date(year=now.year, month=now.month, day=now.day + AFTER)
    #Getting events is a teired system in CalCalndar
    #instead of onelining it I am breaking it out to show process
    store = CalCalendarStore.defaultCalendarStore()
    calendars = store.calendars()
    predicate = \
        CalCalendarStore.eventPredicateWithStartDate_endDate_calendars_(
        startDate, endDate, calendars)
    events = store.eventsWithPredicate_(predicate)

    dayCache = None

    print("AGENDA:")

    for event in events:
        startedDate = datetime.fromtimestamp(
            event.startDate().timeIntervalSince1970())
        endsDate = datetime.fromtimestamp(
            event.endDate().timeIntervalSince1970())
        #Display DAY MONTH and DATE then each event
        if dayCache != None and startedDate.day != dayCache:
            print("")
        if startedDate.day != dayCache:
            print(startedDate.strftime("- %A %B %d").upper())
        #Lets give it some cute unicode status marks
        #Check for has started
        if endsDate.date() < now:
            print("  ✓", end="")
        #Star for happening now
        if startedDate.date() < now and endsDate.date() > now:
            print("  ☆", end="")
        #Indent all events
        print("\t", end="")
        #Print the time if it's not an all day event
        if not event.isAllDay():
            print(startedDate.strftime("%R") + "-" + endsDate.strftime("%R") +
                " - ", end="")
        #Manually encode to ascii with backslash and replace with apostrophy
        # because python 2.6 and ObjC fight during the encoding.
        #Python3 should obsolete this
        print(event.title().encode('ascii', 'backslashreplace').replace(
            '\u2019', "'"))

        dayCache = startedDate.day