Skip to content

Commit

Permalink
fix(GoogleDriveFile): Add remote file helper for GDrive
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Feb 8, 2022
1 parent 60502f7 commit c6b41e0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
3 changes: 2 additions & 1 deletion trame/__init__.py
@@ -1,7 +1,7 @@
from trame.internal import (
change, Controller, flush_state, get_cli_parser, get_state, get_version,
is_dirty, is_dirty_all, port, setup_dev, start, State, stop, trigger,
update_state, RemoteFile, Singleton
update_state, RemoteFile, Singleton, GoogleDriveFile
)
from trame.layouts import update_layout

Expand Down Expand Up @@ -86,6 +86,7 @@

# Utils/helpers
"RemoteFile",
"GoogleDriveFile",

# These are not exposed in the docs
"__version__",
Expand Down
2 changes: 1 addition & 1 deletion trame/internal/__init__.py
Expand Up @@ -8,5 +8,5 @@
from .utils import (
AppServerThread, base_directory, ClientWindowProcess, compose_callbacks,
get_cli_parser, get_version, is_dunder, log_js_error, print_server_info,
validate_key_names, RemoteFile, Singleton
validate_key_names, RemoteFile, Singleton, GoogleDriveFile
)
3 changes: 2 additions & 1 deletion trame/internal/utils/__init__.py
Expand Up @@ -5,7 +5,7 @@
from .logging import log_js_error, print_server_info, validate_key_names
from .string import is_dunder
from .version import get_version
from .remote_data import RemoteFile
from .remote_data import RemoteFile, GoogleDriveFile
from .singleton import Singleton

__all__ = [
Expand All @@ -20,5 +20,6 @@
"print_server_info",
"validate_key_names",
"RemoteFile",
"GoogleDriveFile",
"Singleton",
]
70 changes: 62 additions & 8 deletions trame/internal/utils/remote_data.py
Expand Up @@ -3,9 +3,41 @@
from urllib.request import urlretrieve


class RemoteFile:
def __init__(self, local_path=None, remote_url=None, local_base=None):
self._url = remote_url
def download_file_from_google_drive(id, destination):
import requests

URL = "https://docs.google.com/uc?export=download"

session = requests.Session()
response = session.get(URL, params={"id": id}, stream=True)
token = get_confirm_token(response)

if token:
params = {"id": id, "confirm": token}
response = session.get(URL, params=params, stream=True)

save_response_content(response, destination)


def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith("download_warning"):
return value

return None


def save_response_content(response, destination):
CHUNK_SIZE = 32768

with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)


class AbstractRemoteFile:
def __init__(self, local_path=None, local_base=None):
# setup base path
self._base = os.getcwd()
if local_base is not None:
Expand All @@ -32,15 +64,37 @@ def local(self):
return os.path.exists(self._file_path)

def fetch(self):
try:
print(f"Downloading:\n - {self._url}\n - to {self._file_path}")
urlretrieve(self._url, self._file_path)
except HTTPError as e:
print(RuntimeError(f"Failed to download {self._url}. {e.reason}"))
pass

@property
def path(self):
if not self.local:
self.fetch()

return self._file_path


class GoogleDriveFile(AbstractRemoteFile):
def __init__(self, local_path=None, google_id=None, local_base=None):
super().__init__(local_path, local_base)
self._gid = google_id

def fetch(self):
try:
print(f"Downloading:\n - {self._gid}\n - to {self._file_path}")
download_file_from_google_drive(self._gid, self._file_path)
except HTTPError as e:
print(RuntimeError(f"Failed to download {self._gid}. {e.reason}"))


class RemoteFile(AbstractRemoteFile):
def __init__(self, local_path=None, remote_url=None, local_base=None):
super().__init__(local_path, local_base)
self._url = remote_url

def fetch(self):
try:
print(f"Downloading:\n - {self._url}\n - to {self._file_path}")
urlretrieve(self._url, self._file_path)
except HTTPError as e:
print(RuntimeError(f"Failed to download {self._url}. {e.reason}"))

0 comments on commit c6b41e0

Please sign in to comment.