Skip to content

Latest commit

 

History

History
76 lines (50 loc) · 2.18 KB

GitHub_Download_repository_from_URL.md

File metadata and controls

76 lines (50 loc) · 2.18 KB



Template request | Bug report | Generate Data Product

Tags: #github #download #repository #url #api #zip

Author: Florent Ravenel

Description: This notebook explains how to download a repository from a URL.

References:

Input

Import libraries

import requests
import urllib
import os
import zipfile

Setup Variables

  • url: URL of the repository to be downloaded
  • branch_master: Name of the master branch
  • output_dir: Path of the directory as output
# Inputs
url = "https://github.com/jupyter-naas/data-product-framework"
branch_master = "master"

# Outputs
output_dir = "."

Model

Download repository

repo_name = url.split("/")[-1].split("/")[0]
zip_url = f"{url}/archive/refs/heads/{branch_master}.zip"
output_path = os.path.join(output_dir, f"{repo_name}.zip")
urllib.request.urlretrieve(zip_url, output_path)
print("✅ Repo archive downloaded:", output_path)

Unzip and rename reposistory

with zipfile.ZipFile(output_path, 'r') as zip_ref:
    # extract files on root dir
    zip_ref.extractall(output_dir)
print("✅ Folder unzip")

Remove ZIP

os.remove(output_path)
print("✅ ZIP removed from root folder")

Output

Display result

print("✅ Repo downloaded available in dir:", output_dir)