def login_handler(username, password, path): overleaf_client = OverleafClient() store = overleaf_client.login(username, password) if store is None: return False with open(path, 'wb+') as f: pickle.dump(store, f) return True
def main(ctx, local, remote, cookie_path, sync_path, olignore_path): if ctx.invoked_subcommand is None: if not os.path.isfile(cookie_path): raise click.ClickException( "Persisted Overleaf cookie not found. Please login or check store path." ) with open(cookie_path, 'rb') as f: store = pickle.load(f) overleaf_client = OverleafClient(store["cookie"], store["csrf"]) project = execute_action( lambda: overleaf_client.get_project( os.path.basename(os.path.join(sync_path, os.getcwd()))), "Querying project", "Project queried successfully.", "Project could not be queried.") zip_file = execute_action( lambda: zipfile.ZipFile( io.BytesIO(overleaf_client.download_project(project["id"]))), "Downloading project", "Project downloaded successfully.", "Project could not be downloaded.") sync = not (local or remote) if remote or sync: sync_func(files_from=zip_file.namelist(), create_file_at_to=lambda name: write_file( os.path.join(sync_path, name), zip_file.read(name)), from_exists_in_to=lambda name: os.path.isfile( os.path.join(sync_path, name)), from_equal_to_to=lambda name: open( os.path.join(sync_path, name), 'rb').read() == zip_file.read(name), from_newer_than_to=lambda name: dateutil.parser.isoparse( project["lastUpdated"]).timestamp( ) > os.path.getmtime(os.path.join(sync_path, name)), from_name="remote", to_name="local") if local or sync: sync_func( files_from=olignore_keep_list(sync_path, olignore_path), create_file_at_to=lambda name: overleaf_client.upload_file( project["id"], name, os.path.getsize(os.path.join(sync_path, name)), open(os.path.join(sync_path, name), 'rb')), from_exists_in_to=lambda name: name in zip_file.namelist(), from_equal_to_to=lambda name: open( os.path.join(sync_path, name), 'rb').read( ) == zip_file.read(name), from_newer_than_to=lambda name: os.path.getmtime( os.path.join(sync_path, name)) > dateutil.parser.isoparse( project["lastUpdated"]).timestamp(), from_name="local", to_name="remote")
def list_projects(cookie_path, verbose): def query_projects(): for index, p in enumerate(sorted(overleaf_client.all_projects(), key=lambda x: x['lastUpdated'], reverse=True)): if not index: click.echo("\n") click.echo(f"{dateutil.parser.isoparse(p['lastUpdated']).strftime('%m/%d/%Y, %H:%M:%S')} - {p['name']}") return True if not os.path.isfile(cookie_path): raise click.ClickException( "Persisted Overleaf cookie not found. Please login or check store path.") with open(cookie_path, 'rb') as f: store = pickle.load(f) overleaf_client = OverleafClient(store["cookie"], store["csrf"]) click.clear() execute_action(query_projects, "Querying all projects", "Querying all projects successful.", "Querying all projects failed. Please try again.", verbose)