-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCRParser.java
More file actions
144 lines (136 loc) · 5.45 KB
/
PCRParser.java
File metadata and controls
144 lines (136 loc) · 5.45 KB
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
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;
public class PCRParser {
private final HashMap<String, String> codesToNames;
private final HashMap<String, Set<String>> coursesToInstructors;
public PCRParser() {
codesToNames = new HashMap<>();
coursesToInstructors = new HashMap<>();
}
private String getJSONContent(URL url) {
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Cookie", "csrftoken=cQVsFOKKiTWIasPu5P3feclUb8SIWzJTwBvZKRBVgAlUFWGmDhncpu6hGsoVHDzl; sessionid=jia3lyerlrsj21c5a715b74k49332lqv");
if (con.getResponseCode() != 200) {
System.out.println(con.getResponseMessage());
System.out.println(url);
return null;
}
Scanner in = new Scanner(con.getInputStream());
if (in.hasNextLine()) {
return in.nextLine();
}
} catch (IOException e) {
return null;
}
return "";
}
/**
* Finds all of the instructors that have ever taught the given course.
* @param courseCode The course code of the given course.
* @return A set of instructor codes.
* @throws IllegalArgumentException if the course is not found
*/
public Set<String> getCourseInstructors(String courseCode) {
if (coursesToInstructors.containsKey(courseCode)) {
return coursesToInstructors.get(courseCode);
}
try {
URL url = new URL("https://penncoursereview.com/api/review/course/"
+ courseCode + "?format=json");
String jsonContent = getJSONContent(url);
if (jsonContent == null) {
throw new IllegalArgumentException("course not found");
}
JSONObject json = new JSONObject(jsonContent);
JSONObject instructors = json.getJSONObject("instructors");
Set<String> keySet = instructors.keySet();
if (!coursesToInstructors.containsKey(courseCode)) {
coursesToInstructors.put(courseCode, keySet);
}
return keySet;
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
/**
* Finds all of the courses that the given instructor has ever taught.
* @param instructorCode The instructor code of the given instructor.
* @return A set of course codes.
* @throws IllegalArgumentException if the instructor is not found
*/
public Set<String> getInstructorCourses(String instructorCode) {
try {
URL url = new URL("https://penncoursereview.com/api/review/instructor/"
+ instructorCode + "?format=json");
String jsonContent = getJSONContent(url);
if (jsonContent == null) {
throw new IllegalArgumentException("instructor not found");
}
JSONObject json = new JSONObject(jsonContent);
if (!codesToNames.containsKey(instructorCode)) {
codesToNames.put(instructorCode, json.getString("name"));
}
JSONObject instructors = json.getJSONObject("courses");
return instructors.keySet();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
/**
* Returns the name of the instructor associated with the given code.
* @param instructorCode The code to search.
* @return The name of the instructor.
* @throws IllegalArgumentException if the instructor is not found
*/
public String getInstructorName(String instructorCode) {
if (codesToNames.containsKey(instructorCode)) {
return codesToNames.get(instructorCode);
}
try {
URL url = new URL("https://penncoursereview.com/api/review/instructor/"
+ instructorCode + "?format=json");
String jsonContent = getJSONContent(url);
if (jsonContent == null) {
throw new IllegalArgumentException("instructor not found");
}
JSONObject json = new JSONObject(jsonContent);
return json.getString("name");
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
public String getInstructorCode(String instructor) {
try {
File jsonFile = new File("data/autocomplete.json");
Scanner scanner = new Scanner(jsonFile);
if (!scanner.hasNextLine()) {
return null;
}
String jsonContent = scanner.nextLine();
JSONObject json = new JSONObject(jsonContent);
JSONArray instructors = json.getJSONArray("instructors");
for (Object instructorObj : instructors) {
JSONObject instructorDetail = (JSONObject) instructorObj;
if (instructorDetail.getString("title").equals(instructor)) {
String[] urlParts = instructorDetail.getString("url").split("/");
return urlParts[urlParts.length - 1];
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}