| 1 | import json |
| 2 | import os |
| 3 | import shutil |
| 4 | import requests |
| 5 | from urllib.parse import urlparse |
| 6 | from sys import argv |
| 7 | import tempfile |
| 8 | |
| 9 | |
| 10 | def download(url: str, dest=None): |
| 11 | split = url.split("/") |
| 12 | try: |
| 13 | s1, s2, s3, owner, repo, _, branch, *path = split |
| 14 | path = "/".join(path) |
| 15 | except: |
| 16 | try: |
| 17 | s1, s2, s3, owner, repo = split |
| 18 | path = None |
| 19 | except: |
| 20 | print("Invalid URL") |
| 21 | gh_base = "/".join([s1, s2, s3, owner, repo]) |
| 22 | if not dest: |
| 23 | if path: |
| 24 | dest = path |
| 25 | else: |
| 26 | dest = repo |
| 27 | print(f"Owner: {owner}") |
| 28 | print(f"Repo: {repo}") |
| 29 | print(f"Branch: {branch}") |
| 30 | print(f"Path: {path}") |
| 31 | print(f"Destination: {dest}") |
| 32 | |
| 33 | |
| 34 | with tempfile.TemporaryDirectory() as tmpdir: |
| 35 | os.system(f"git clone {gh_base} {tmpdir}/{repo}") |
| 36 | s = os.path.join(tmpdir, repo) |
| 37 | shutil.rmtree(os.path.join(s, ".git")) |
| 38 | if path: |
| 39 | s = os.path.join(s, path) |
| 40 | d = os.path.join(os.getcwd(), dest) |
| 41 | shutil.copytree(s, d, dirs_exist_ok=True) |
| 42 | |
| 43 | |
| 44 | def main(): |
| 45 | download(argv[1]) |
| 46 | |
| 47 | |
| 48 | if __name__ == "__main__": |
| 49 | download(argv[1]) |