def _save(self, fname_or_obj, file_path_prefix, allow_overwrite=False, compression=None, **save_kwargs): if isinstance(fname_or_obj, str): fname = os.path.expanduser(fname_or_obj) if os.path.isdir(fname): raise ValueError("Cannot compress directory") elif os.path.isfile(fname): obj = just.bytes.read(fname) else: obj = fname_or_obj else: obj = fname_or_obj path = file_path_prefix if compression is not None: path += "." + compression if not allow_overwrite and just.exists(path): raise ValueError("Path exists, cannot save {!r}".format(path)) if isinstance(obj, str): obj = bytes(obj, encoding="utf8") just.write(obj, path, unknown_type="bytes") return path
def load_from_download(ingest_glob, vendor, recent_only=True, delete_existing=True): ingest_files = just.glob(ingest_glob) if not ingest_files: raise ValueError(f"Nothing to extract using {ingest_glob} - Aborting") nostalgia_input = "~/nostalgia_data/input/{}".format(vendor) if delete_existing: just.remove(nostalgia_input, allow_recursive=True) elif just.exists(nostalgia_input): raise ValueError(f"Cannot overwrite path {nostalgia_input}, pass delete_existing=True") fnames = sorted(ingest_files, key=os.path.getctime) if recent_only: fnames = fnames[-1:] for fname in fnames: with zipfile.ZipFile(fname, 'r') as zip_ref: out = os.path.expanduser(nostalgia_input) print("unpacking from", fname, "to", out) zip_ref.extractall(out)
def ensure_access_token(self): if self.access_token: return now = int(time.time()) if just.exists(self.ACCESS_TOKEN_FILE): access_token = just.read(self.ACCESS_TOKEN_FILE, unknown_type="json") if now > access_token['time'] + access_token['expires_in']: log.info('Cached access token is expired') os.unlink(self.ACCESS_TOKEN_FILE) else: self.access_token = access_token return self.access_token = self.get_access_token() self.access_token['time'] = now just.write(self.access_token, self.ACCESS_TOKEN_FILE, unknown_type="json")
def _save(self, obj, file_path_prefix, allow_overwrite=False, compression=None, **save_kwargs): path = file_path_prefix if not path.endswith(".json"): path += ".json" if compression is not None: path += "." + compression if not allow_overwrite and just.exists(path): raise ValueError("Path exists, cannot save {!r}".format(path)) try: just.write(obj, path) except AttributeError as e: print(e) raise ValueError("Ensure that file_path_prefix ends with .json") return path
def main(): conf_path = "~/nostalgia_data/config/fitbit/config.json" if not just.exists(conf_path): webbrowser.open("https://dev.fitbit.com/apps/new") webbrowser.open( "https://raw.githubusercontent.com/nostalgia-dev/nostalgia_fitbit/master/docs/fitbit_app.png" ) client_id = getpass.getpass("Client ID: ") client_secret = getpass.getpass("Client Secret: ") info = {"client_id": client_id, "client_secret": client_secret} just.write(info, conf_path) print("Saved in:", conf_path) config = just.read(conf_path) if not config["client_id"] or not config["client_secret"]: msg = "Fill in a value for client_id and client_secret in '{}'".format( conf_path) raise ValueError(msg) fa = FitbitAuth(client_id=config['client_id'], client_secret=config['client_secret']) fa.ensure_access_token() try: f = Fitbit(access_token=fa.access_token['access_token']) print(json.dumps(f.profile, indent=2)) except requests.exceptions.HTTPError as e: print(e.response.status_code) if e.response.status_code == 429: print(e.response.headers) return raise export = FitbitExport(f, profile=f.profile) export.sync_sleep() export.sync_heartrate_intraday()
import just ENTRY = "~/nostalgia_data/nostalgia_entry.py" if not just.exists("~/nostalgia_data/nostalgia_entry.py"): just.write("", ENTRY) from nostalgia.ndf import NDF