-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGitReD.py
95 lines (84 loc) · 3.34 KB
/
GitReD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
## GitHub Repo Downloader
- Program to download public GitHub repo and store that repo to the local storage.
'''
try:
import sys
import os
import requests
import shutil
import json
class GitReD_downloader:
def __init__(self, owner, repo, save_in_folder=True, keep_zip_saved=False) -> None:
self.__owner = owner
self.__repo = repo
self.__save_in_folder=save_in_folder
self.__keep_zip_saved=keep_zip_saved
self.__branch = None
self.__lock=False
def __need_to_return(self):
if self.__branch==None:
self.__identify_branch()
if self.__lock==True:
return True
return False
def __identify_branch(self):
try:
branch_request = requests.get(f'https://api.github.com/repos/{self.__owner}/{self.__repo}/branches/main')
if branch_request.status_code==200:
self.__branch = json.loads(branch_request.text)['name']
else:
self.__lock=True
except Exception as e:
print(e)
def __download_zip(self):
if self.__need_to_return():
return
try:
zip_file = requests.get(f'https://github.com/{self.__owner}/{self.__repo}/archive/refs/heads/{self.__branch}.zip')
try:
with open(f'{self.__repo}.zip','wb') as file:
file.write(zip_file.content)
except Exception as e:
print(e)
except Exception as e:
print(e)
def __extract_zip(self):
if self.__need_to_return():
return
try:
source = f'{self.__repo}.zip'
if not os.path.exists(source):
return {"status":False,"message":'source not exists'}
shutil.unpack_archive(source)
except Exception as e:
print(e)
if self.__keep_zip_saved==False:
# Delete zip file
os.remove(source)
def __move_to_parent(self):
if self.__need_to_return():
return
if self.__save_in_folder==False:
try:
source = f"{self.__repo}-{self.__branch}"
if os.path.exists(source):
# Code to move the files from sub-folder to main folder.
files = [os.path.join(source, file) for file in os.listdir(source)]
cwd = os.getcwd()
for file in files:
shutil.move(file,f'{cwd}/')
os.rmdir(source)
return True
return {"status":False,"message":'Source not exists'}
except Exception as e:
print(e)
def download(self):
self.__download_zip()
self.__extract_zip()
self.__move_to_parent()
if __name__=="__main__":
repo_downloader = GitReD_downloader(f'{sys.argv[1]}', f'{sys.argv[2]}', json.loads(sys.argv[3].lower()), json.loads(sys.argv[4].lower()))
repo_downloader.download()
except Exception as e:
print(e)