-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGitGet.java
229 lines (177 loc) · 7.58 KB
/
GitGet.java
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
class GitGet{
private static final Scanner in = new Scanner(System.in);
private static boolean INIT_FLAG=false;
private static int main_repos_index=0;
private static String REPO_NAME=null;
private static String directory_name=null;
public static void main(String args[]){
int choice=0;
String input_url;
while(choice!=2) {
System.out.println("Enter or paste a path you want to download to: ");
directory_name=in.next();
System.out.print("Enter your URL:");
input_url=in.next();
start_engine(input_url,"");
download_files(input_url);
System.out.println("Enter: ");
System.out.println("1 to continue");
System.out.println("2 to Exit");
choice=in.nextInt();
}
System.out.println("Goodbye!");
}
private static void start_engine(String input_url,String p_name){ //FILE TREE CREATION
try {
Document doc = Jsoup.connect(input_url).get();
for (Element table : doc.select("tbody")) {
for(Element row:table.select("tr[class=js-navigation-item]")){
Elements column = row.select("td");
Elements a_tag = column.select("a");
String curr_tag = a_tag.attr("href");
if (curr_tag.matches(".*\\btree\\b.*")) {
create_dir("https://github.com"+curr_tag,p_name); //DOWNLOAD IF TREE
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
private static void download_files(String input_url) { //DOWNLOAD INITIATION
try {
Document doc = Jsoup.connect(input_url).get();
for (Element table : doc.select("tbody")) {
for(Element row:table.select("tr[class=js-navigation-item]")){
Elements column = row.select("td");
Elements a_tag = column.select("a");
String curr_tag = a_tag.attr("href");
if (curr_tag.matches(".*\\btree\\b.*")) {
download_files("https://github.com"+curr_tag);
}
if (curr_tag.matches(".*\\bblob\\b.*")) {
download_stuff("https://github.com"+curr_tag); // DOWNLOAD IF BLOB
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
private static void download_stuff(String input_url){
String user_url;
URL website = null;
ReadableByteChannel rbc = null;
String save_path = null;
int slash_index=0;
String extension=null;
String name=null;
user_url = input_url;
if (user_url.matches(".*\\bblob\\b.*")) {
user_url = user_url.replaceAll("\\bblob\\b", "raw");
}
char url[] = user_url.toCharArray();
for(int i = url.length-1; i>=0; i--){
if(url[i]=='/'){
name=user_url.substring(i+1,user_url.length()); //IF FILE WITHOUT EXTENSION
extension="";
break;
}
if(url[i]=='.'){
int dot_index = i;
for(int j = dot_index+1; j<user_url.length(); j++){ // GETTING THE EXTENSION
if(extension==null){
extension=Character.toString(url[j]);
}else{
extension=extension+Character.toString(url[j]);
}
}
for(int j = dot_index-1; j>=0; j--){ //GETTING THE NAME OF THE FILE
if(url[j]=='/'){
name=user_url.substring(j+1,dot_index);
break;
}
}
break;
}
}
System.out.println("Dowloading...");
try {
website = new URL(user_url); //CREATE URL
} catch (Exception e) {
System.out.println("ERROR: Please check your URL again!");
}
if (website != null) {
try {
rbc = Channels.newChannel(website.openStream()); //CONNECT TO URL
} catch (Exception e) {
System.out.println("ERROR: Please check your internet connection!");
}
}
int path_index=input_url.lastIndexOf(REPO_NAME);
save_path = input_url.substring(path_index+REPO_NAME.length(),input_url.length());
System.out.println("File name : "+name+"."+extension);
System.out.println("save path : "+save_path);
if (rbc != null) {
try {
FileOutputStream fos;
if(name.equals("")){
slash_index=save_path.lastIndexOf(".");
save_path=save_path.substring(0,slash_index-1);
fos = new FileOutputStream(directory_name+"/"+save_path+extension); //FOR NO NAME AND ONLY EXTENSION
}else {
fos = new FileOutputStream(directory_name+"/"+save_path); //NORMAL DOWNLOAD
}
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
System.out.println("Successful! \n");
rbc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void create_dir(String input_url,String path){ //CREATE DIRECTORY
String name=null;
char url[] = input_url.toCharArray();
if(path.equals("")) {
if(REPO_NAME!=null){
int path_index=input_url.lastIndexOf(REPO_NAME);
name = input_url.substring(path_index+REPO_NAME.length(),input_url.length());
}else{
for (int i = url.length - 1; i >= 0; i--) {
if (url[i] == '/') {
name = input_url.substring(i + 1, url.length);
break;
}
}
if(!INIT_FLAG){
main_repos_index=input_url.indexOf(name);
for (int i = main_repos_index - 2; i >= 0; i--) {
if (url[i] == '/') {
REPO_NAME = input_url.substring(i + 1, main_repos_index-1);
System.out.println("REPO NAME: "+REPO_NAME);
break;
}
}
INIT_FLAG=true;
}
}
}else{
int path_index=input_url.lastIndexOf(path);
name = input_url.substring(path_index+path.length(),input_url.length());
}
start_engine(input_url,REPO_NAME);
System.out.println("Directory created: "+directory_name+"/"+name);
new File(directory_name+"/" + name).mkdirs();
}
}