@@ -16,21 +16,21 @@ def create_url(url):
16
16
we do a GET requests later in this script
17
17
"""
18
18
19
- # extract the dirs name from the given url (e.g master)
20
- download_dirs = re .findall (r"\/tree\/master\/(.*)" , url )[0 ]
21
-
22
19
# 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 ]
24
24
25
25
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 )
27
27
28
28
api_url = api_url + "?ref=" + branch
29
29
30
30
return (api_url , download_dirs )
31
31
32
32
33
- def download (repo_url ):
33
+ def download (repo_url , flatten ):
34
34
'''
35
35
recursive download
36
36
'''
@@ -40,9 +40,10 @@ def download(repo_url):
40
40
41
41
r = urllib .request .urlretrieve (api_url )
42
42
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 )
46
47
47
48
# total files count
48
49
total_files = 0
@@ -59,7 +60,11 @@ def download(repo_url):
59
60
for index , file in enumerate (data ):
60
61
file_url = file ["download_url" ]
61
62
fname = file ["name" ]
62
- path = file ["path" ]
63
+
64
+ if flatten :
65
+ path = os .path .basename (file ["path" ])
66
+ else :
67
+ path = file ["path" ]
63
68
64
69
if not file_url is None :
65
70
try :
@@ -68,7 +73,7 @@ def download(repo_url):
68
73
69
74
# bring the cursor to the beginning, erase the current line, and dont make a new line
70
75
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 )
72
77
73
78
except KeyboardInterrupt :
74
79
# when CTRL+C is pressed during the execution of this script,
@@ -78,7 +83,7 @@ def download(repo_url):
78
83
print ("\033 [1;92m✔ Got interupted\033 [0m" )
79
84
exit ()
80
85
else :
81
- download (file ["html_url" ])
86
+ download (file ["html_url" ], flatten )
82
87
83
88
return total_files
84
89
@@ -90,16 +95,18 @@ def main():
90
95
parser = argparse .ArgumentParser (description = "Download directories/folders from GitHub" )
91
96
parser .add_argument ('url' , action = "store" )
92
97
98
+ parser .add_argument ('--flatten' , '-f' , action = "store_true" , help = 'flatten directory structures' )
99
+
93
100
args = parser .parse_args ()
94
101
95
102
repo_url = args .url
103
+ flatten = args .flatten
96
104
97
- total_files = download (repo_url )
105
+ total_files = download (repo_url , flatten )
98
106
99
107
print ("\r " + ERASE_LINE , end = "" )
100
108
print ("\033 [1;92m✔ Download complete\033 [0m" )
101
109
102
110
103
111
if __name__ == "__main__" :
104
112
main ()
105
-
0 commit comments