Here's an alternative, more contemporary solution using fsspec
in Python. This example includes grabbing a zip file from github and unzipping it locally.
GitHubFs
uses PyGitHub
under the hood, which is a Python library to access the GitHub REST API.
Goes without saying you will need to keep your token secure.
Requires fsspec
with github
and libarchive
to be installed.
from pathlib import Path
from fsspec import filesystem
def get_remote_github_obj(
org: str = "my-org", repo: str = "my-repo", branch: str = "my-branch", username: str = "my-username", token: str = "my-github-pat"
):
git_fs = filesystem(
"github",
org=org,
repo=repo,
branch=branch,
username=username,
token=token,
)
# Create a new dir
Path("/path/to/desired/local/download/location/").mkdir(parents=True, exist_ok=True)
# Get the zip archive from URL
git_fs.get(
rpath="relative/path/to/remote/zip_file.zip", lpath="/path/to/desired/local/download/location/zip_file.zip"
)
# Use libarchive to unarchive
libarchive_fs = filesystem(
"libarchive",
fo="/path/to/desired/local/download/location/zip_file.zip",
)
libarchive_fs.get(
rpath="/",
lpath="/path/to/desired/local/download/location/example_unarchived_dir",
recursive=True,
)