Exemplo n.º 1
0
 def get_calendars(self):
     if not os.path.exists(
             self.TEMP_STORAGE_CALENDARS) or not t.lastmodified(
                 self.TEMP_STORAGE_CALENDARS, 60):
         t.writeLog('establish online connection for getting calendars')
         self.establish()
         cal_list = self.service.calendarList().list().execute()
         cals = cal_list.get('items', [])
         with open(self.TEMP_STORAGE_CALENDARS, 'w') as filehandle:
             json.dump(cals, filehandle)
         return cals
     else:
         t.writeLog('getting calendars from local storage')
         with open(self.TEMP_STORAGE_CALENDARS, 'r') as filehandle:
             return json.load(filehandle)
Exemplo n.º 2
0
    def get_events(self,
                   storage,
                   timeMin,
                   timeMax,
                   maxResult=30,
                   calendars='primary'):
        if not os.path.exists(storage) or not t.lastmodified(storage, 60):
            t.writeLog('establish online connection for getting events')
            self.establish()

            events = []
            for cal in calendars:
                cal_events = self.service.events().list(
                    calendarId=cal,
                    timeMin=timeMin,
                    timeMax=timeMax,
                    maxResults=maxResult,
                    singleEvents=True,
                    orderBy='startTime').execute()
                _evs = cal_events.get('items', [])
                if _evs:
                    # set additional attributes
                    calColor = self.get_calendarBGcolorImage(cal)
                    for _ev in _evs:
                        _ts = parser.parse(_ev['start'].get(
                            'dateTime', _ev['start'].get('date',
                                                         ''))).timetuple()

                        _ev.update({'timestamp': int(time.mktime(_ts))})
                        _ev.update({'icon': calColor})

                        gadget = _ev.get('gadget', None)
                        if gadget:
                            if gadget.get('preferences').get(
                                    'goo.contactsEventType') == 'BIRTHDAY':
                                _ev.update({'specialicon': __cake__})
                    events.extend(_evs)

            events.sort(key=operator.itemgetter('timestamp'))
            with open(storage, 'w') as filehandle:
                json.dump(events, filehandle)
        else:
            t.writeLog('getting events from local storage')
            with open(storage, 'r') as filehandle:
                events = json.load(filehandle)
        return events
Exemplo n.º 3
0
    def get_events(self,
                   storage,
                   timeMin,
                   timeMax,
                   maxResult=30,
                   calendars='primary',
                   evtype='default'):
        if not os.path.exists(storage) or not t.lastmodified(storage, 60):
            t.writeLog('establish online connection for getting events')
            self.establish()

            events = []
            for cal in calendars:
                cal_items = self.service.events().list(
                    calendarId=cal,
                    timeMin=timeMin,
                    timeMax=timeMax,
                    maxResults=maxResult,
                    singleEvents=True,
                    orderBy='startTime').execute()
                cal_set = cal_items.get('items', [])

                # set additional attributes

                icon = self.get_calendarBGcolorImage(cal)
                for _record in cal_set:
                    _item = {}
                    _ts = parser.parse(_record['start'].get(
                        'dateTime', _record['start'].get('date', '')))
                    _end = parser.parse(_record['end'].get(
                        'dateTime', _record['end'].get('date', '')))
                    _tdelta = relativedelta.relativedelta(
                        _end.date(), _ts.date())

                    _item.update({
                        'date':
                        datetime.isoformat(_ts),
                        'shortdate':
                        _ts.strftime('%d.%m'),
                        'allday':
                        0
                        if _record['start'].get('dateTime') else _tdelta.days,
                        'timestamp':
                        int(time.mktime(_ts.timetuple())),
                        'icon':
                        icon,
                        'id':
                        _record.get('id', ''),
                        'summary':
                        _record.get('summary', ''),
                        'description':
                        _record.get('description', None),
                        'location':
                        _record.get('location', None)
                    })

                    if _record['start'].get('dateTime', False):
                        _item.update(
                            {'start': {
                                'dateTime': datetime.isoformat(_ts)
                            }})
                    else:
                        _item.update(
                            {'start': {
                                'date': datetime.isoformat(_ts)
                            }})

                    if _record['end'].get('dateTime', False):
                        _item.update(
                            {'end': {
                                'dateTime': datetime.isoformat(_end)
                            }})
                    else:
                        _item.update(
                            {'end': {
                                'date': datetime.isoformat(_end)
                            }})

                    gadget = _record.get('gadget', None)
                    if gadget:
                        if gadget.get('preferences').get(
                                'goo.contactsEventType') == 'BIRTHDAY':
                            _item.update({'specialicon': __cake__})

                    events.append(_item)

            # get additional calendars, glotz.info

            if t.getAddonSetting(
                    'glotz_enabled',
                    sType=t.BOOL) and t.getAddonSetting('glotz_apikey') != '':
                if evtype == 'default' or (evtype == 'notification'
                                           and t.getAddonSetting(
                                               'glotz_notify', sType=t.BOOL)):
                    t.writeLog('getting events from glotz.info')
                    try:
                        cal_set = json.loads(
                            urllib.urlopen(self.GLOTZ_URL).read())
                        icon = self.get_calendarBGcolorImage('glotz_color')
                        for _record in cal_set:
                            _item = {}
                            _show = _record.get('show')
                            _time_fmt = 'dateTime'
                            if len(_show.get('airs_time', '')) == 5:
                                _hour = int(_show.get('airs_time')[0:2])
                                _minute = int(_show.get('airs_time')[3:5])
                            else:
                                _hour = 0
                                _minute = 0
                                _time_fmt = 'date'

                            _ts = datetime.fromtimestamp(
                                int(_record.get('first_aired',
                                                '0'))).replace(hour=_hour,
                                                               minute=_minute)
                            _end = _ts + timedelta(
                                minutes=int(_show.get('runtime', '0'))
                            ) if _time_fmt == 'dateTime' else _ts

                            _item.update({
                                'timestamp':
                                int(time.mktime(_ts.timetuple())),
                                'date':
                                datetime.isoformat(_ts),
                                'shortdate':
                                _ts.strftime('%d.%m'),
                                'start': {
                                    _time_fmt: datetime.isoformat(_ts)
                                },
                                'end': {
                                    _time_fmt: datetime.isoformat(_end)
                                },
                                'id':
                                '%s-%s-%s' % (_record.get('first_aired', ''),
                                              _record.get('season', '0'),
                                              _record.get('number', '0')),
                                'summary':
                                _show.get('network', ''),
                                'description':
                                '%s - S%02iE%02i: %s' %
                                (_show.get('title', ''),
                                 int(_record.get('season', '0')),
                                 int(_record.get('number', '0')),
                                 _record.get('title', '')),
                                'icon':
                                icon,
                                'banner':
                                _show['images'].get('banner', ''),
                                'allday':
                                1 if _time_fmt == 'date' else 0
                            })

                            events.append(_item)
                    except Exception as e:
                        t.writeLog('Error on line {}'.format(
                            sys.exc_info()[-1].tb_lineno),
                                   level=xbmc.LOGERROR)
                        t.writeLog(type(e).__name__, level=xbmc.LOGERROR)
                        t.writeLog(e.message, level=xbmc.LOGERROR)

            events.sort(key=operator.itemgetter('timestamp'))

            with open(storage, 'w') as filehandle:
                json.dump(events, filehandle)
        else:
            t.writeLog('getting events from local storage')
            with open(storage, 'r') as filehandle:
                events = json.load(filehandle)
        return events