Skip to content

Commit 00ac011

Browse files
committed
added flatten option. Fixed bug downloading from non-master
1 parent d0fc0b5 commit 00ac011

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
dist
22
*.egg-info
3+
4+
# jetbrains IDE files
5+
.idea
6+
7+
# virtual environment
8+
venv/

gitdir/gitdir

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ def create_url(url):
1616
we do a GET requests later in this script
1717
"""
1818

19-
# extract the dirs name from the given url (e.g master)
20-
download_dirs = re.findall(r"\/tree\/master\/(.*)", url)[0]
21-
2219
# extract the branch name from the given url (e.g master)
23-
branch = re.findall(r"\/tree\/(.*?)\/", url)[0]
20+
branch = re.findall(r"/tree/(.*?)/", url)[0]
21+
22+
# extract the dirs name from the given url (e.g master)
23+
download_dirs = re.findall(r"/tree/" + branch + r"/(.*)", url)[0]
2424

2525
api_url = url.replace("https://github.com", "https://api.github.com/repos")
26-
api_url = re.sub(r"\/tree\/.*?\/", "/contents/", api_url)
26+
api_url = re.sub(r"/tree/.*?/", "/contents/", api_url)
2727

2828
api_url = api_url + "?ref=" + branch
2929

3030
return (api_url, download_dirs)
3131

3232

33-
def download(repo_url):
33+
def download(repo_url, flatten):
3434
'''
3535
recursive download
3636
'''
@@ -40,9 +40,10 @@ def download(repo_url):
4040

4141
r = urllib.request.urlretrieve(api_url)
4242

43-
# make a directory with the name which is taken from
44-
# the actual repo
45-
os.makedirs(download_dirs, exist_ok=True)
43+
if not flatten:
44+
# make a directory with the name which is taken from
45+
# the actual repo
46+
os.makedirs(download_dirs, exist_ok=True)
4647

4748
# total files count
4849
total_files = 0
@@ -59,7 +60,11 @@ def download(repo_url):
5960
for index, file in enumerate(data):
6061
file_url = file["download_url"]
6162
fname = file["name"]
62-
path = file["path"]
63+
64+
if flatten:
65+
path = os.path.basename(file["path"])
66+
else:
67+
path = file["path"]
6368

6469
if not file_url is None:
6570
try:
@@ -68,7 +73,7 @@ def download(repo_url):
6873

6974
# bring the cursor to the beginning, erase the current line, and dont make a new line
7075
print("\r" + ERASE_LINE, end="")
71-
print("\033[1;92m▶ Downloaded: \033[0m{}".format(fname),end="",flush=True)
76+
print("\033[1;92m▶ Downloaded: \033[0m{}".format(fname), end="", flush=True)
7277

7378
except KeyboardInterrupt:
7479
# when CTRL+C is pressed during the execution of this script,
@@ -78,7 +83,7 @@ def download(repo_url):
7883
print("\033[1;92m✔ Got interupted\033[0m")
7984
exit()
8085
else:
81-
download(file["html_url"])
86+
download(file["html_url"], flatten)
8287

8388
return total_files
8489

@@ -90,16 +95,18 @@ def main():
9095
parser = argparse.ArgumentParser(description="Download directories/folders from GitHub")
9196
parser.add_argument('url', action="store")
9297

98+
parser.add_argument('--flatten', '-f', action="store_true", help='flatten directory structures')
99+
93100
args = parser.parse_args()
94101

95102
repo_url = args.url
103+
flatten = args.flatten
96104

97-
total_files = download(repo_url)
105+
total_files = download(repo_url, flatten)
98106

99107
print("\r" + ERASE_LINE, end="")
100108
print("\033[1;92m✔ Download complete\033[0m")
101109

102110

103111
if __name__ == "__main__":
104112
main()
105-

0 commit comments

Comments
 (0)