Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.apps.ubc.cc.wrapper;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import com.apps.datastore.dao.BookInformationObject;
- import com.apps.datastore.dao.CourseInformationObject;
- import com.apps.datastore.dao.DepartmentInformationObject;
- import com.apps.datastore.dao.SectionInformationObject;
- import com.apps.utils.*;
- import com.google.appengine.api.datastore.Entity;
- public class AllUBCCoursesWrapper {
- public static List<BookInformationObject> getBooks(SectionInformationObject sio){
- List<BookInformationObject> biol = new ArrayList<BookInformationObject>();
- try {
- BufferedReader br = WrapperUtils
- .getURLContentReader(WrapperUtils.COURSE_INFO_URL
- .replace("<DEPARTMENT>", sio.getDepartmentId())
- .replace("<COURSE>", sio.getCourseId())
- .replace("<SECTION>", sio.getSectionId()));
- String line;
- line = br.readLine();
- while (line != null) {
- if (line.contains("<b>Book Summary</b>")) {
- if (line.contains("<b>ISBN </b></td></tr><tr>")) {
- String books = line.substring(line
- .indexOf("<td class='section"));
- while (books.contains("<td class='section")) {
- String title = "";
- String required = "";
- String author = "";
- String isbn = "";
- for (int i = 0; i < 4; i++) {
- String s = books
- .substring(
- books.indexOf("<td class='section") + 22,
- books.indexOf("</td>"));
- switch (i) {
- case 0:
- title = s;
- break;
- case 1:
- required = s;
- break;
- case 2:
- author = s;
- break;
- case 3:
- isbn = s;
- break;
- }
- books = books
- .substring(books.indexOf("</td>") + 5);
- }
- biol.add(new BookInformationObject(sio.getDepartmentId(), sio.getCourseId(), sio.getSectionId(), title, required,
- author, isbn));
- }
- }
- }
- line = br.readLine();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return biol;
- }
- public static BufferedReader loopToString(String toFind, BufferedReader br) {
- BufferedReader setreader=br;
- String line;
- try {
- line = br.readLine();
- while (line!=null) {
- // Just keep looping
- System.out.println("THE LINE IS "+line);
- if(line.contains(toFind)){
- System.out.println("OUT");
- return setreader;
- }else{
- line=br.readLine();
- System.out.println("setreader :"+setreader.readLine());
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- public static List<SectionInformationObject> getSections(CourseInformationObject cio){
- List<SectionInformationObject> siol = new ArrayList<SectionInformationObject>();
- try {
- BufferedReader br = WrapperUtils.getURLContentReader(WrapperUtils.SECTIONS_URL.replace("<DEPARTMENT>", cio.getDepartmentId()).replace("<COURSE>", cio.getCourseId()));
- String line = br.readLine();
- String id = "";
- String activity = "";
- String term = "";
- String day = "";
- String location = "";
- String start = "";
- String end = "";
- String instructor = "";
- String building = "";
- String room = "";
- boolean cdf = false;
- String dropNoWDate = "";
- String dropWDate = "";
- while (!line.contains("<!-- end of TABLE FOR AREA 1 -->")) {
- if (line.contains("<b>This course is eligible for Credit/D/Fail grading.</b>")) {
- cdf = true;
- } else if (line.contains("<tr class=section1>") || line.contains("<tr class=section2>")) {
- line = br.readLine();
- while (!line.contains("</td>")) {
- if (line.contains("<a href=")){
- id = WrapperUtils.stringInBwtn("&dept="+cio.getDepartmentId()+"&course="+cio.getCourseId()+"§ion=","\"", line);
- }
- line = br.readLine();
- }
- line=br.readLine();
- br=loopToString("<td nowrap>",br);
- line=br.readLine();
- System.out.println("oO"+line);
- activity = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
- System.out.println("Activity"+activity);
- line = br.readLine();
- term = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
- System.out.println(term);
- line = br.readLine();
- day = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
- System.out.println(day);
- day = WrapperUtils.trimAll(day);
- line = br.readLine();
- start = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
- System.out.println(start);
- line = br.readLine();
- end = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
- System.out.println(end);
- line=br.readLine();
- BufferedReader br2 = WrapperUtils.getURLContentReader(WrapperUtils.COURSE_INFO_URL.replace("<DEPARTMENT>", cio.getDepartmentId()).replace("<COURSE>", cio.getCourseId()).replace("<SECTION>", id));
- String line2 = br2.readLine();
- while(!line2.contains("<!-- end of TABLE FOR AREA 1 -->")){
- if(line2.contains("Last day to withdraw without a W standing : "))
- dropNoWDate = WrapperUtils.stringInBwtn("<b>", "</b>", line2);
- if(line2.contains("Last day to withdraw with a W standing"))
- dropWDate = WrapperUtils.stringInBwtn("<b>", "</b>", line2);
- if(line2.contains("<td nowrap>Instructor")) {
- while(!line2.contains("</table>")) {
- if(line2.contains("<td nowrap><a href=")) {
- if(instructor != "")
- instructor += " / "+WrapperUtils.stringInBwtn("\">", "</a></td>", line2);
- else
- instructor = WrapperUtils.stringInBwtn("\">", "</a></td>", line2);
- }
- line2 = br2.readLine();
- }
- }
- if(line2.contains("<td nowrap><b>Building</b></td>")) {
- for(int i = 0; i < 10; i ++)
- line2 = br2.readLine();
- building = WrapperUtils.stringInBwtn("<td nowrap>", "</td>", line2);
- building = WrapperUtils.trimAll(building);
- if(building.equals("WAIT"))
- building = "";
- for(int i = 0; i < 3; i ++)
- line2 = br2.readLine();
- room = WrapperUtils.stringInBwtn("\">","</a>",line2);
- room = WrapperUtils.trimAll(room);
- if(room.equals("LIST"))
- room = "";
- }
- line2 = br2.readLine();
- }
- // System.out.println(id + ", " + activity + ", " + term + ", " + day + ", " + location + ", " + start + ", " + end + ", " + instructor + ", " + building + ", " + room + ", " + cdf + ", " + dropNoWDate + ", " + dropWDate);
- SectionInformationObject sio = new SectionInformationObject(cio.getDepartmentId(), cio.getCourseId(), id, activity, term, day, location, start, end, instructor, building, room, cdf, dropNoWDate, dropWDate);
- if(id != "" && id != null)
- siol.add(sio);
- id = "";
- activity = "";
- term = "";
- day = "";
- location = "";
- start = "";
- end = "";
- instructor = "";
- building = "";
- room = "";
- cdf = false;
- dropNoWDate = "";
- dropWDate = "";
- }
- line = br.readLine();
- }
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return siol;
- }
- public static List<CourseInformationObject> getCourses(DepartmentInformationObject dio){
- List<CourseInformationObject> ciol = new ArrayList<CourseInformationObject>();
- try {
- BufferedReader br = WrapperUtils.getURLContentReader(WrapperUtils.COURSES_URL.replace("<DEPARTMENT>", dio.getDepartmentId()));
- String line = br.readLine();
- String id = "";
- String title = "";
- String prereq = "";
- String coreq = "";
- long credits = 0;
- int counter = 0;
- while (line != null) {
- // System.out.println(line);
- if (line.contains("<tr class=section1>") || line.contains("<tr class=section2>")) {
- while (counter < 2) {
- if (line.contains("<a href=")){
- id = WrapperUtils.stringInBwtn("\">"+dio.getDepartmentId()+" ","</a>", line);
- counter++;
- }
- else if (line.contains("<td nowrap>")) {
- title = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
- counter++;
- }
- line=br.readLine();
- }
- BufferedReader br2 = WrapperUtils.getURLContentReader(WrapperUtils.SECTIONS_URL.replace("<DEPARTMENT>", dio.getDepartmentId()).replace("<COURSE>", id));
- String line2 = br2.readLine();
- while(!line2.contains("<!-- end of TABLE FOR AREA 1 -->")){
- if(line2.contains("Pre-reqs:")) {
- prereq = WrapperUtils.stringInBwtn("Pre-reqs:","<br><br>", line2);
- prereq = WrapperUtils.trimAll(prereq);
- prereq = WrapperUtils.removeHTML(prereq);
- }
- else if(line2.contains("Co-reqs:")) {
- coreq = WrapperUtils.stringInBwtn("Pre-reqs:","<br><br>", line2);
- coreq = WrapperUtils.trimAll(coreq);
- coreq = WrapperUtils.removeHTML(coreq);
- }
- else if(line2.contains("Credits:")) {
- String parseNum = line2.replaceAll("[^0-9]", "");
- if(parseNum != "" && parseNum != null )
- credits = Long.parseLong(parseNum);
- // credits =Long.parseLong(line.substring(line.indexOf("Credits: ")+("Credits: ".length())));
- //Long.parseLong(WrapperUtils.stringInBwtn("Credits: ","\n", line2));
- }
- line2 = br2.readLine();
- }
- CourseInformationObject cio = new CourseInformationObject( dio.getDepartmentId(), id, title, prereq, coreq, credits);
- if(id != "" && id != null)
- ciol.add(cio);
- id = "";
- title = "";
- prereq = "";
- coreq = "";
- credits = 0;
- counter=0;
- }
- line=br.readLine();
- }
- }
- catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return ciol;
- }
- public static List<DepartmentInformationObject> getDepartments(){
- List<DepartmentInformationObject> diol = new ArrayList<DepartmentInformationObject>();
- try {
- BufferedReader br = WrapperUtils.getURLContentReader(WrapperUtils.DEPARTMENTS_URL);
- String line;
- String code = "";
- String title = "";
- String faculty = "";
- int counter = 0;
- line=br.readLine();
- while (line != null) {
- // System.out.println(line);
- if (line.contains("<tr class=section1>") || line.contains("<tr class=section2>")) {
- while (counter<2) {
- if (line.contains("<a href=")){
- code = WrapperUtils.stringInBwtn("\">","</a>", line);
- }
- if (line.contains("<td nowrap>")) {
- switch (counter) {
- case 0:
- title = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
- ++counter;
- // System.out.println("The course is "+course);
- line=br.readLine();
- //Add to database.
- break;
- case 1:
- faculty = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
- ++counter;
- // System.out.println("The faculty is "+faculty);
- //Add to database.
- break;
- }
- }else{
- line=br.readLine();
- }
- }
- DepartmentInformationObject dio = new DepartmentInformationObject( title, code, faculty);
- if(code != "" && code != null)
- diol.add(dio);
- code = "";
- title = "";
- faculty = "";
- counter=0;
- }
- line=br.readLine();
- }
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return diol;
- }
- public static void main(String[] a){
- List<SectionInformationObject> siol = getSections(new CourseInformationObject("CPSC", "121", null, null, null, 0));
- System.out.println(siol.size());
- }
- }
Add Comment
Please, Sign In to add comment