Guest User

Untitled

a guest
Jan 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.29 KB | None | 0 0
  1. package com.apps.ubc.cc.wrapper;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.net.MalformedURLException;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.List;
  9.  
  10. import com.apps.datastore.dao.BookInformationObject;
  11. import com.apps.datastore.dao.CourseInformationObject;
  12. import com.apps.datastore.dao.DepartmentInformationObject;
  13. import com.apps.datastore.dao.SectionInformationObject;
  14. import com.apps.utils.*;
  15. import com.google.appengine.api.datastore.Entity;
  16.  
  17. public class AllUBCCoursesWrapper {
  18.  
  19.  
  20.  
  21.  
  22. public static List<BookInformationObject> getBooks(SectionInformationObject sio){
  23. List<BookInformationObject> biol = new ArrayList<BookInformationObject>();
  24. try {
  25. BufferedReader br = WrapperUtils
  26. .getURLContentReader(WrapperUtils.COURSE_INFO_URL
  27. .replace("<DEPARTMENT>", sio.getDepartmentId())
  28. .replace("<COURSE>", sio.getCourseId())
  29. .replace("<SECTION>", sio.getSectionId()));
  30. String line;
  31.  
  32. line = br.readLine();
  33.  
  34. while (line != null) {
  35. if (line.contains("<b>Book Summary</b>")) {
  36. if (line.contains("<b>ISBN &nbsp;&nbsp; </b></td></tr><tr>")) {
  37.  
  38. String books = line.substring(line
  39. .indexOf("<td class='section"));
  40. while (books.contains("<td class='section")) {
  41. String title = "";
  42. String required = "";
  43. String author = "";
  44. String isbn = "";
  45. for (int i = 0; i < 4; i++) {
  46. String s = books
  47. .substring(
  48. books.indexOf("<td class='section") + 22,
  49. books.indexOf("</td>"));
  50.  
  51. switch (i) {
  52. case 0:
  53. title = s;
  54. break;
  55. case 1:
  56. required = s;
  57. break;
  58. case 2:
  59. author = s;
  60. break;
  61. case 3:
  62. isbn = s;
  63. break;
  64. }
  65. books = books
  66. .substring(books.indexOf("</td>") + 5);
  67. }
  68. biol.add(new BookInformationObject(sio.getDepartmentId(), sio.getCourseId(), sio.getSectionId(), title, required,
  69. author, isbn));
  70. }
  71. }
  72. }
  73. line = br.readLine();
  74. }
  75. } catch (IOException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. }
  79. return biol;
  80. }
  81.  
  82.  
  83. public static BufferedReader loopToString(String toFind, BufferedReader br) {
  84. BufferedReader setreader=br;
  85. String line;
  86. try {
  87. line = br.readLine();
  88. while (line!=null) {
  89. // Just keep looping
  90. System.out.println("THE LINE IS "+line);
  91. if(line.contains(toFind)){
  92.  
  93. System.out.println("OUT");
  94. return setreader;
  95. }else{
  96. line=br.readLine();
  97. System.out.println("setreader :"+setreader.readLine());
  98. }
  99.  
  100.  
  101. }
  102.  
  103. } catch (IOException e) {
  104. // TODO Auto-generated catch block
  105. e.printStackTrace();
  106. }
  107.  
  108. return null;
  109. }
  110.  
  111.  
  112. public static List<SectionInformationObject> getSections(CourseInformationObject cio){
  113. List<SectionInformationObject> siol = new ArrayList<SectionInformationObject>();
  114. try {
  115. BufferedReader br = WrapperUtils.getURLContentReader(WrapperUtils.SECTIONS_URL.replace("<DEPARTMENT>", cio.getDepartmentId()).replace("<COURSE>", cio.getCourseId()));
  116. String line = br.readLine();
  117. String id = "";
  118. String activity = "";
  119. String term = "";
  120. String day = "";
  121. String location = "";
  122. String start = "";
  123. String end = "";
  124. String instructor = "";
  125. String building = "";
  126. String room = "";
  127. boolean cdf = false;
  128. String dropNoWDate = "";
  129. String dropWDate = "";
  130.  
  131. while (!line.contains("<!-- end of TABLE FOR AREA 1 -->")) {
  132. if (line.contains("<b>This course is eligible for Credit/D/Fail grading.</b>")) {
  133. cdf = true;
  134. } else if (line.contains("<tr class=section1>") || line.contains("<tr class=section2>")) {
  135. line = br.readLine();
  136. while (!line.contains("</td>")) {
  137. if (line.contains("<a href=")){
  138. id = WrapperUtils.stringInBwtn("&dept="+cio.getDepartmentId()+"&course="+cio.getCourseId()+"&section=","\"", line);
  139. }
  140. line = br.readLine();
  141. }
  142. line=br.readLine();
  143.  
  144. br=loopToString("<td nowrap>",br);
  145. line=br.readLine();
  146. System.out.println("oO"+line);
  147. activity = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
  148. System.out.println("Activity"+activity);
  149. line = br.readLine();
  150. term = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
  151. System.out.println(term);
  152. line = br.readLine();
  153. day = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
  154. System.out.println(day);
  155. day = WrapperUtils.trimAll(day);
  156. line = br.readLine();
  157. start = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
  158. System.out.println(start);
  159. line = br.readLine();
  160. end = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
  161. System.out.println(end);
  162. line=br.readLine();
  163.  
  164. BufferedReader br2 = WrapperUtils.getURLContentReader(WrapperUtils.COURSE_INFO_URL.replace("<DEPARTMENT>", cio.getDepartmentId()).replace("<COURSE>", cio.getCourseId()).replace("<SECTION>", id));
  165. String line2 = br2.readLine();
  166. while(!line2.contains("<!-- end of TABLE FOR AREA 1 -->")){
  167. if(line2.contains("Last day to withdraw without a W standing : "))
  168. dropNoWDate = WrapperUtils.stringInBwtn("<b>", "</b>", line2);
  169. if(line2.contains("Last day to withdraw with a W standing"))
  170. dropWDate = WrapperUtils.stringInBwtn("<b>", "</b>", line2);
  171. if(line2.contains("<td nowrap>Instructor")) {
  172. while(!line2.contains("</table>")) {
  173. if(line2.contains("<td nowrap><a href=")) {
  174. if(instructor != "")
  175. instructor += " / "+WrapperUtils.stringInBwtn("\">", "</a></td>", line2);
  176. else
  177. instructor = WrapperUtils.stringInBwtn("\">", "</a></td>", line2);
  178. }
  179. line2 = br2.readLine();
  180. }
  181. }
  182. if(line2.contains("<td nowrap><b>Building</b></td>")) {
  183. for(int i = 0; i < 10; i ++)
  184. line2 = br2.readLine();
  185. building = WrapperUtils.stringInBwtn("<td nowrap>", "</td>", line2);
  186. building = WrapperUtils.trimAll(building);
  187. if(building.equals("WAIT"))
  188. building = "";
  189. for(int i = 0; i < 3; i ++)
  190. line2 = br2.readLine();
  191. room = WrapperUtils.stringInBwtn("\">","</a>",line2);
  192. room = WrapperUtils.trimAll(room);
  193. if(room.equals("LIST"))
  194. room = "";
  195. }
  196. line2 = br2.readLine();
  197. }
  198. // System.out.println(id + ", " + activity + ", " + term + ", " + day + ", " + location + ", " + start + ", " + end + ", " + instructor + ", " + building + ", " + room + ", " + cdf + ", " + dropNoWDate + ", " + dropWDate);
  199. SectionInformationObject sio = new SectionInformationObject(cio.getDepartmentId(), cio.getCourseId(), id, activity, term, day, location, start, end, instructor, building, room, cdf, dropNoWDate, dropWDate);
  200. if(id != "" && id != null)
  201. siol.add(sio);
  202. id = "";
  203. activity = "";
  204. term = "";
  205. day = "";
  206. location = "";
  207. start = "";
  208. end = "";
  209. instructor = "";
  210. building = "";
  211. room = "";
  212. cdf = false;
  213. dropNoWDate = "";
  214. dropWDate = "";
  215.  
  216.  
  217. }
  218.  
  219. line = br.readLine();
  220. }
  221. } catch (MalformedURLException e) {
  222. // TODO Auto-generated catch block
  223. e.printStackTrace();
  224. } catch (IOException e) {
  225. // TODO Auto-generated catch block
  226. e.printStackTrace();
  227. }
  228. return siol;
  229. }
  230.  
  231.  
  232.  
  233. public static List<CourseInformationObject> getCourses(DepartmentInformationObject dio){
  234. List<CourseInformationObject> ciol = new ArrayList<CourseInformationObject>();
  235. try {
  236. BufferedReader br = WrapperUtils.getURLContentReader(WrapperUtils.COURSES_URL.replace("<DEPARTMENT>", dio.getDepartmentId()));
  237. String line = br.readLine();
  238. String id = "";
  239. String title = "";
  240. String prereq = "";
  241. String coreq = "";
  242. long credits = 0;
  243. int counter = 0;
  244.  
  245. while (line != null) {
  246.  
  247. // System.out.println(line);
  248. if (line.contains("<tr class=section1>") || line.contains("<tr class=section2>")) {
  249. while (counter < 2) {
  250. if (line.contains("<a href=")){
  251. id = WrapperUtils.stringInBwtn("\">"+dio.getDepartmentId()+" ","</a>", line);
  252. counter++;
  253. }
  254. else if (line.contains("<td nowrap>")) {
  255.  
  256. title = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
  257. counter++;
  258.  
  259. }
  260. line=br.readLine();
  261. }
  262.  
  263. BufferedReader br2 = WrapperUtils.getURLContentReader(WrapperUtils.SECTIONS_URL.replace("<DEPARTMENT>", dio.getDepartmentId()).replace("<COURSE>", id));
  264. String line2 = br2.readLine();
  265. while(!line2.contains("<!-- end of TABLE FOR AREA 1 -->")){
  266. if(line2.contains("Pre-reqs:")) {
  267. prereq = WrapperUtils.stringInBwtn("Pre-reqs:","<br><br>", line2);
  268. prereq = WrapperUtils.trimAll(prereq);
  269. prereq = WrapperUtils.removeHTML(prereq);
  270. }
  271. else if(line2.contains("Co-reqs:")) {
  272. coreq = WrapperUtils.stringInBwtn("Pre-reqs:","<br><br>", line2);
  273. coreq = WrapperUtils.trimAll(coreq);
  274. coreq = WrapperUtils.removeHTML(coreq);
  275. }
  276. else if(line2.contains("Credits:")) {
  277. String parseNum = line2.replaceAll("[^0-9]", "");
  278. if(parseNum != "" && parseNum != null )
  279. credits = Long.parseLong(parseNum);
  280. // credits =Long.parseLong(line.substring(line.indexOf("Credits: ")+("Credits: ".length())));
  281. //Long.parseLong(WrapperUtils.stringInBwtn("Credits: ","\n", line2));
  282. }
  283. line2 = br2.readLine();
  284. }
  285. CourseInformationObject cio = new CourseInformationObject( dio.getDepartmentId(), id, title, prereq, coreq, credits);
  286. if(id != "" && id != null)
  287. ciol.add(cio);
  288. id = "";
  289. title = "";
  290. prereq = "";
  291. coreq = "";
  292. credits = 0;
  293. counter=0;
  294. }
  295. line=br.readLine();
  296. }
  297.  
  298.  
  299. }
  300. catch (MalformedURLException e) {
  301. // TODO Auto-generated catch block
  302. e.printStackTrace();
  303. } catch (IOException e) {
  304. // TODO Auto-generated catch block
  305. e.printStackTrace();
  306. }
  307.  
  308. return ciol;
  309. }
  310.  
  311. public static List<DepartmentInformationObject> getDepartments(){
  312. List<DepartmentInformationObject> diol = new ArrayList<DepartmentInformationObject>();
  313. try {
  314. BufferedReader br = WrapperUtils.getURLContentReader(WrapperUtils.DEPARTMENTS_URL);
  315. String line;
  316. String code = "";
  317. String title = "";
  318. String faculty = "";
  319. int counter = 0;
  320. line=br.readLine();
  321.  
  322. while (line != null) {
  323.  
  324. // System.out.println(line);
  325. if (line.contains("<tr class=section1>") || line.contains("<tr class=section2>")) {
  326. while (counter<2) {
  327. if (line.contains("<a href=")){
  328. code = WrapperUtils.stringInBwtn("\">","</a>", line);
  329. }
  330. if (line.contains("<td nowrap>")) {
  331.  
  332. switch (counter) {
  333. case 0:
  334.  
  335. title = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
  336. ++counter;
  337.  
  338. // System.out.println("The course is "+course);
  339. line=br.readLine();
  340. //Add to database.
  341. break;
  342.  
  343. case 1:
  344. faculty = WrapperUtils.stringInBwtn("<td nowrap>","</td>", line);
  345. ++counter;
  346. // System.out.println("The faculty is "+faculty);
  347. //Add to database.
  348. break;
  349.  
  350. }
  351.  
  352. }else{
  353. line=br.readLine();
  354. }
  355.  
  356. }
  357.  
  358. DepartmentInformationObject dio = new DepartmentInformationObject( title, code, faculty);
  359. if(code != "" && code != null)
  360. diol.add(dio);
  361. code = "";
  362. title = "";
  363. faculty = "";
  364. counter=0;
  365. }
  366. line=br.readLine();
  367. }
  368. } catch (MalformedURLException e) {
  369. // TODO Auto-generated catch block
  370. e.printStackTrace();
  371. } catch (IOException e) {
  372. // TODO Auto-generated catch block
  373. e.printStackTrace();
  374. }
  375. return diol;
  376.  
  377. }
  378.  
  379. public static void main(String[] a){
  380. List<SectionInformationObject> siol = getSections(new CourseInformationObject("CPSC", "121", null, null, null, 0));
  381. System.out.println(siol.size());
  382. }
  383.  
  384. }
Add Comment
Please, Sign In to add comment