-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParse.java
129 lines (116 loc) · 4.12 KB
/
Parse.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
package calhacks;
import java.lang.String;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.*;
import java.net.URL;
import java.net.URLConnection;
/*@ author Alexander Tran
October 9, 2015
Parses the source code from DownloadPage.java to get the specific date. */
public class Parse {
public static void run() throws IOException {
departments = makeDepartments();
String url;
for (int i = 0; i < departments.size(); i++) {
Department caldepartment = new Department(departments.get(i));
Parse.classlist.add(caldepartment);
}
}
public static ArrayList<ArrayList<String>> createArray(String input){
ArrayList<ArrayList<String>> L = new ArrayList<ArrayList<String>>();
Scanner s = new Scanner(input);
while (s.hasNext()) {
String elem = s.next();
if (elem.contains("courseblocktitle")) {
ArrayList<String> temp = new ArrayList<String>();
// adds Course Number
s.next();
elem = s.next();
while (!elem.contains("\"")) {
elem = s.next();
}
String coursenumber = elem;
coursenumber = coursenumber.substring(0, coursenumber.length() - 1);
temp.add(coursenumber);
for (int i = 0; i < 5; i++){
s.next();
}
// adds Course Title
String elem2 = s.next();
String coursetitle = "";
while (!elem2.contains("<")) {
coursetitle += " " + elem2;
elem2 = s.next();
}
coursetitle += " " + elem2;
coursetitle = coursetitle.substring(15, coursetitle.length() - 7);
coursetitle = "{" + coursetitle + "}";
temp.add(coursetitle);
// adds Course Units
s.next();
String courseunits = "";
String elem3 = s.next();
while (!elem3.contains("<")) {
courseunits += " " + elem3;
elem3 = s.next();
}
courseunits += " " + elem3;
courseunits = courseunits.substring(15, courseunits.length() - 24);
if (courseunits.endsWith(" ")) {
courseunits = courseunits.substring(0, courseunits.length() - 1);
}
temp.add(courseunits);
L.add(temp);
}
}
return L;
}
private static ArrayList<Department> classlist = new ArrayList<Department>();
private static ArrayList<String> departments;
public static ArrayList<Department> getClassList() {
return Parse.classlist;
}
public static ArrayList<String> getDepartments() {
return Parse.departments;
}
public static String makeConnectionToCourses() throws IOException {
URL url = new URL("http://guide.berkeley.edu/courses/");
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
String source = "";
while ((line = br.readLine()) != null) {
source += line;
}
return source;
}
public static ArrayList<String> makeDepartments() throws IOException {
ArrayList<String> L = new ArrayList<String>();
String source = makeConnectionToCourses();
Scanner s = new Scanner(source);
String temp;
while (s.hasNext()) {
String elem = s.next();
if (elem.contains("courses")) {
L.add(elem);
}
}
for (int i = 0; i < 3; i++) {
L.remove(0);
}
L.remove(L.size() - 1);
L.remove(L.size() - 1);
int deleteindex = 0;
for (int j = 0; j < L.size(); j++) {
L.set(j,L.get(j).substring(15));
deleteindex = L.get(j).indexOf("/");
L.set(j,L.get(j).substring(0, deleteindex));
}
return L;
}
}