-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopPack.java
216 lines (195 loc) · 5.59 KB
/
TopPack.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
import java.io.*;
import java.util.*;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
class ValueComparator implements Comparator<String> {
Map<String, Integer> base;
public ValueComparator(Map<String, Integer> base) {
this.base = base;
}
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return 1;
} else {
return -1;
}
}
}
public class TopPack {
static JSONArray jsonarr = new JSONArray();
static JSONArray jsonarr_repo = new JSONArray();
static int index, choice;
static InputStream is = null;
static Map<String, Integer> packages = new HashMap<String, Integer>();
static OkHttpClient client = new OkHttpClient();
public static String run(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static void Search(String key_word) {
String inputStr = null;
try {
inputStr = run("https://api.github.com/search/repositories?q="
+ key_word);
if (inputStr.contains("API rate Limit exceeded")) {
System.out.println("Error: API rate limit exceeded ");
System.exit(0);
}
} catch (IOException e1) {
e1.printStackTrace();
}
JSONParser parse = new JSONParser();
JSONObject obj = null;
try {
obj = (JSONObject) parse.parse(inputStr);
} catch (ParseException e) {
e.printStackTrace();
}
jsonarr = (JSONArray) obj.get("items");
if (choice == 1) {
for (int i = 0; i < jsonarr.size(); i++) {
JSONObject ob = (JSONObject) jsonarr.get(i);
System.out.println("\nrepo_id: " + ob.get("id"));
System.out.println("name: " + ob.get("name"));
String s = (String) ob.get("full_name");
for (index = 0; index < s.length(); index++) {
if (s.charAt(index) == '/')
break;
}
s = s.substring(0, index);
System.out.println("owner_name:" + s);
System.out.println("forks:" + ob.get("forks"));
System.out.println("stars: " + ob.get("stargazers_count"));
}
}
}
public static void Import(String N) {
String inputStr = null;
try {
inputStr = run("https://api.github.com/repositories/" + N);
if (inputStr.contains("API rate Limit exceeded")) {
System.out.println("Error: API rate limit exceeded ");
System.exit(0);
}
} catch (IOException e2) {
e2.printStackTrace();
}
JSONParser parse = new JSONParser();
JSONObject obj = null;
try {
obj = (JSONObject) parse.parse(inputStr);
} catch (ParseException e) {
e.printStackTrace();
}
String name, download_url;
name = (String) obj.get("full_name");
String input1 = null;
try {
input1 = run("https://api.github.com/repos/" + name + "/contents");
if (input1.contains("API rate Limit exceeded")) {
System.out.println("Error: API rate limit exceeded ");
System.exit(0);
}
} catch (IOException e2) {
e2.printStackTrace();
}
parse = new JSONParser();
Object xyz = null;
try {
xyz = parse.parse(input1);
} catch (ParseException e2) {
e2.printStackTrace();
}
jsonarr_repo = (JSONArray) xyz;
for (int i = 0; i < jsonarr_repo.size(); i++) {
obj = (JSONObject) jsonarr_repo.get(i);
name = (String) obj.get("name");
if (name.equals("package.json")) {
System.out.println("Yes");
download_url = (String) obj.get("download_url");
try {
inputStr = run(download_url);
} catch (IOException e1) {
e1.printStackTrace();
}
parse = new JSONParser();
JSONObject job = new JSONObject();
try {
job = (JSONObject) parse.parse(inputStr);
} catch (ParseException e) {
e.printStackTrace();
}
JSONObject dev = (JSONObject) job.get("dependencies");
JSONObject devDep = (JSONObject) job.get("devDependencies");
for (Object key : dev.keySet()) {
String keyStr = (String) key;
if (packages.containsKey(keyStr)) {
packages.replace(keyStr, packages.get(keyStr) + 1);
} else {
packages.put(keyStr, 1);
}
}
for (Object key : devDep.keySet()) {
String keyStr = (String) key;
if (packages.containsKey(keyStr)) {
packages.replace(keyStr, packages.get(keyStr) + 1);
} else {
packages.put(keyStr, 1);
}
}
if (choice == 2) {
System.out.println("Packages Used: ");
for (Object key : packages.keySet()) {
String keyStr = (String) key;
System.out.println(keyStr);
}
}
break;
}
}
}
public static void Toppack(String key_word) {
Search(key_word);
for (int i = 0; i < jsonarr.size(); i++) {
JSONObject ob = (JSONObject) jsonarr.get(i);
Import(ob.get("id").toString());
System.out.println(ob.get("id").toString());
}
ValueComparator bvc = new ValueComparator(packages);
TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc);
sorted_map.putAll(packages);
int z = 0;
for (Object key : sorted_map.keySet()) {
while (z < 10) {
z++;
String keyStr = (String) key;
int keyValue = sorted_map.get(keyStr);
System.out.println(keyStr + " : " + keyValue);
}
}
}
public static void main(String[] args) {
System.out.println("1.Search");
System.out.println("2.Import");
System.out.println("3.Toppack");
Scanner scan = new Scanner(System.in);
choice = scan.nextInt();
if (choice == 1) {
String key_word = scan.next();
Search(key_word);
} else if (choice == 2) {
String id = scan.next();
Import(id);
} else if (choice == 3) {
String key_word = scan.next();
Toppack(key_word);
}
scan.close();
}
}