@GET @Path("fill") @Produces({"application/xml", "application/json"}) public String fillDatabase() { try { String COURSE_LIST = "https://www.coursera.org/maestro/api/topic/list2"; String UNIVERSITY_LIST = "https://www.coursera.org/maestro/api/university/list"; ArrayList<University> unis; ArrayList<Course> courses; // <<<<<<< HEAD unis = getUniversityList(UNIVERSITY_LIST); courses = getCourseList(COURSE_LIST); // for (University uni : unis){ // System.out.println("ID: "+uni.getId()); // System.out.println("Name: "+uni.getName()); // System.out.println("Abbr: "+uni.getAbbrName()); // System.out.println("Img Url: "+uni.getImgUrl()); // // System.out.println("----------------------------------------------------------------------------------"); // universityFacadeREST.create(uni); // } for (Course course : courses) { University newuni = course.getIdUniversity(); courseFacadeREST.create(course); } return "Success!"; } catch (Exception ex) { Logger.getLogger(CourseraParser.class.getName()).log(Level.SEVERE, null, ex); // ======= // unis = getUniversityList(UNIVERSITY_LIST); // courses = getCourseList(COURSE_LIST); // for (int i = 0; i < unis.size(); ++i) { // em.persist(unis.get(i)); // // System.out.println(unis.get(i).getName()); // System.out.println(unis.get(i).getAbbrName()); // System.out.println(unis.get(i).getImgUrl()); // System.out.println(unis.get(i).getId()); // // } // // for (int i = 0; i < courses.size(); ++i) { // em.persist(courses.get(i)); //// >>>>>>> branch 'master' of https://github.com/hafidsousa/QuestionLoop.git // } return null; } }
public ArrayList<Course> getCourseList(String COURSE_LIST) throws Exception { ArrayList<Course> courses = new ArrayList<Course>(); UniversityFacadeREST universityREST = new UniversityFacadeREST(); HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(COURSE_LIST); HttpResponse httpResponse = httpClient.execute(httpGet); httpResponse.setEntity(httpResponse.getEntity()); HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { InputStream inputStream = httpEntity.getContent(); String jsonFile = convertStreamToString(inputStream); JSONObject json = new JSONObject(jsonFile); // Courses are called topics for Coursera JSONObject courselist = json.getJSONObject("topics"); Iterator<?> keys = courselist.keys(); // Iterate through each key while (keys.hasNext()) { String key = (String) keys.next(); if (courselist.get(key) instanceof JSONObject) { JSONObject coursecode = (JSONObject) courselist.get(key); Course course = new Course(); // To ensure the id is the same as Coursera, // so that it can be linked later on to the // university course.setId(10 + Integer.parseInt(key)); course.setName(coursecode.getString("name")); course.setCode(coursecode.getString("short_name")); course.setIconUrl(coursecode.getString("large_icon")); course.setLanguage(coursecode.getString("language")); JSONArray universityIdList = coursecode.getJSONArray("unis"); University newuni = new University(10 + universityIdList.getInt(0)); course.setIdUniversity(newuni); courses.add(course); } } } return courses; }