def __init__(self, config, **kwargs): Feed.__init__(self, config, **kwargs) self.url = 'https://api.fitbit.com' self.uri = '/1/user/-/activities/date/%Y-%m-%d.json' self.steps_uri = '/1/user/-/activities/steps/date/%Y-%m-%d/1d.json' self.refresh_token_uri = '/oauth2/token' self.devices_uri = '/1/user/-/devices.json' # OAuth self._CLIENT_ID = self.config['oauth2_client_id'] self._CLIENT_SECRET = self.config['oauth2_client_secret'] self._ACCESS_TOKEN = self.config['oauth2_access_token'] self._REFRESH_TOKEN = self.config['oauth2_refresh_token'] self._SIGNUP_DATE = self.config['signup_date'] self._EMBED_INTRADAY_STEPS = self.config.get( 'embed_intraday_steps', True ) self._DEVICE_ID = self.config['device_id'] # convert SIGNUP_DATE to naive UTC datetime self.signup_date = local_datetime_to_utc( datetime.datetime.strptime(self._SIGNUP_DATE, "%Y-%m-%d"), self.timezone ).replace(tzinfo=None) self.last_sync_time = None self.next_date = None self._headers = { 'Authorization': 'Bearer %s' % (self._ACCESS_TOKEN) } self._last_status = None
def get_events_by_timerange(self, before=None, after=None, pagesize=10, feeds=None, flattened=False, timezone=None, embed_related=True): basequery = Query("select {events}.* from events {events}") if feeds is not None: basequery += ", feeds {feeds} where {events}.feed_id={feeds}.id" if flattened: embed_related = False eq = EventQuery( basequery, embed_feeds=True, embed_related=embed_related ) if before is not None: eq.add_clause( "{events}.occurred < %s", (local_datetime_to_utc(before, timezone),) ) if after is not None: eq.add_clause( "{events}.occurred > %s", (local_datetime_to_utc(after, timezone),) ) if feeds is not None: eq.add_clause("{feeds}.short_name in %s", (tuple(feeds),)) if not flattened: eq.add_clause("{events}.is_related=false") return EventSetByQuery(self._pool, eq, pagesize, timezone=timezone)
def fetch_last_sync_time(self): h = httplib2.Http() url = self.url + self.devices_uri success = False while not success: resp, content = h.request(url, "GET", headers=self._headers) # retry request if required (using the same url and headers) retry, _, _ = self.parse_status(resp, content, url, self._headers) success = not retry data = json.loads(content.decode('utf-8')) _LOG.warn('Devices response: %s', content.decode('utf-8')) device_found = False for device in data: if device['id'] == self._DEVICE_ID: device_found = True self.last_sync_time = local_datetime_to_utc( datetime.datetime.strptime( device['lastSyncTime'], '%Y-%m-%dT%H:%M:%S.%f' ), self.timezone ) break # can't proceed if we didn't find a last sync time if not device_found: raise DeviceIDNotFound( 'No sync data for device with ID: ' + self._DEVICE_ID )
def __init__(self, event_occurred, event_id, timezone=None): self.occurred = local_datetime_to_utc(event_occurred, timezone) self.id = event_id
def __init__(self, index, pool, query, eventquery, pagesize, timezone=None, to_mask=None, to_filter=None, before=None, after=None): super().__init__(pool, eventquery, pagesize, timezone=timezone) self._index = index self._filter_terms = None self._mask_terms = None self._metadata = SearchMetadata(0, None) self.query = query self.before = before self.after = after # convert any provided timestamps to UTC if self.before is not None: self.before = local_datetime_to_utc(self.before, self.timezone) if self.after is not None: self.after = local_datetime_to_utc(self.after, self.timezone) parser = MultifieldParser(["title", "text"], self._index.schema) self._parsed_query = parser.parse(self.query) # build feed filters and masks if to_filter is not None: self._filter_terms = whoosh.query.Or( [whoosh.query.Term("feed", str(feed)) for feed in to_filter]) if to_mask is not None: self._mask_terms = whoosh.query.Or( [whoosh.query.Term("feed", str(feed)) for feed in to_mask]) # build daterange filter if self.before is not None or self.after is not None: # default after is a very early timestamp after = (self.after if self.after is not None else datetime.datetime.utcfromtimestamp(1)) # default before is the current timestamp before = (self.before if self.before is not None else datetime.datetime.utcnow()) filter_term = whoosh.query.DateRange("occurred", after, before, startexcl=True, endexcl=True) if self._filter_terms is None: self._filter_terms = filter_term else: self._filter_terms &= filter_term # determine metadata self._search_metadata()