Created
October 13, 2022 13:27
-
-
Save Argus-Khan/50c719f077875065a1e1dd9383f90241 to your computer and use it in GitHub Desktop.
A small python script to download github directories/files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# A small python script to download github directories/files. | |
# Author: Argus Khan | |
# Dependencies: subversion | |
import sys, os, re, argparse, subprocess | |
try: | |
subprocess.check_output(['which', 'svn']) | |
except subprocess.CalledProcessError as err: | |
print("Error: Dependency missing 'subversion', please install it.") | |
sys.exit() | |
def download(opt): | |
parsed = opt.link | |
regex = re.compile(r'https://github.com/.+/.+/(blob|tree)/(main|master)/.+') | |
# print( re.match(regex,opt.link)) | |
if not re.match(regex,opt.link): | |
print("Error: Invalid link.") | |
sys.exit() | |
if (opt.path): | |
if not os.path.isdir(opt.path): | |
file_chk = opt.path.split('/') | |
if '.' in file_chk[-1] or os.path.isfile(opt.path): | |
if os.path.isfile(opt.path): | |
print(f"""Error: File path passed instead of a directory.\nNote: An extensionless file '{file_chk[-1]}' exists at the path.""" ) | |
else: | |
print("Error: File path passed instead of a directory.") | |
sys.exit() | |
elif(opt.dir): | |
os.makedirs(opt.path) | |
else: | |
print("Error: Invalid path, directory/directories missing. Use the -f flag to make missing directories.") | |
sys.exit() | |
os.chdir(opt.path) | |
parsed = parsed.split('/') | |
fin_link = f"""https://github.com/{parsed[3]}/{parsed[4]}/trunk""" | |
for i in range(7,len(parsed)): | |
fin_link += f"""/{parsed[i]}""" | |
cmd = f"""svn export """ | |
if (opt.quite): | |
cmd += "-q " | |
print("Please wait, downloading files...") | |
else: | |
print("Files Downloaded:\n") | |
cmd += fin_link | |
os.system(cmd) | |
print("Done!\n") | |
parser = argparse.ArgumentParser( | |
description="A small python script to download github directories." | |
) | |
parser.add_argument( | |
"link", | |
type=str, | |
help="Link to the github directory/file." | |
) | |
parser.add_argument( | |
"-p", "--path", | |
type=str, | |
help="custom download location.") | |
parser.add_argument( | |
"-v", "--verbose", | |
action="store_false", | |
dest="quite", | |
help="whether or not to list downloaded content.") | |
parser.add_argument( | |
"-f", "--force", | |
action="store_true", | |
dest="dir", | |
help="whether or not to make missing directories.") | |
parsed_args = parser.parse_args() | |
download(parsed_args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment