Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.commons.codec.binary.Base64;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- import javax.print.Doc;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.Authenticator;
- import java.net.HttpURLConnection;
- import java.net.PasswordAuthentication;
- import java.net.URL;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * Created by musyrif on 30-Oct-17.
- */
- public class main {
- public static void main(String[] args) throws Exception {
- SimpleDateFormat test = new SimpleDateFormat("HH:mm");
- Calendar t = Calendar.getInstance();
- //t.setTime(test.parse("08:00"));
- //t.add(Calendar.MINUTE, 30);
- //System.out.println(test.format(t.getTime()));
- String tableSel = "http://info.uniten.edu.my/info/Ticketing.ASP?WCI=TimeTable";
- String domain = "";
- String userName = "";
- String password = "";
- Document selDoc = Jsoup.parse(getAuthenticatedResponse(tableSel, domain, userName, password));
- Element link = selDoc.select("a").first();
- String urlStr = "http://info.uniten.edu.my/info/"+link.attr("href");
- System.out.println(link.text());
- String responseText = getAuthenticatedResponse(urlStr, domain, userName, password);
- Document doc = Jsoup.parse(responseText);
- Elements table = doc.getElementsByTag("tbody");
- Subject subject[] = new Subject[20];
- for (Element tr: table){//2 tables, lecturers information and timetable
- int day = 0;
- Elements trow = tr.getElementsByTag("tr");
- int l = 0;//index for subject array
- for (Element td: trow){//table row, mon to sun
- t.setTime(test.parse("08:00"));
- Elements tdata = td.getElementsByTag("td");
- //int x =0;
- int startTime = 0;
- for (Element td1:tdata) {//every td in each trow
- //if (x==0){System.out.print(td1.text().charAt(0)+ " : ");}
- if (td1.text().equals(" ")) {//empty table
- t.add(Calendar.MINUTE, 30);
- startTime++;
- }else{ //table with subjects
- String word = td1.attr("colspan");
- if (!word.equals("")) {//SUBJECT
- int length = Integer.parseInt(word);//get subject duration
- //System.out.print(l+". ("+td1.text() + ") ");//subject details
- //System.out.print(test.format(t.getTime()));//startTime
- for (int i = 0; i < length; i++) {
- t.add(Calendar.MINUTE, 30);
- }
- //System.out.print(" - "+ test.format(t.getTime()));//endTime
- //System.out.print(" | ");
- //System.out.print(startTime+" ");
- subject[l] = new Subject(startTime,length,day,td1.text());
- startTime+=length;
- l++;
- }
- }
- //x++;
- }
- day++;
- //System.out.println();
- }
- //System.out.println();
- }
- subject[0].toString();
- subject[1].toString();
- subject[2].toString();
- subject[3].toString();
- subject[4].toString();
- }
- private static String getAuthenticatedResponse(final String urlStr, final String domain, final String userName, final String password) throws IOException {
- StringBuilder response = new StringBuilder();
- Authenticator.setDefault(new Authenticator() {
- @Override
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(domain + "\\" + userName, password.toCharArray());
- }
- });
- URL urlRequest = new URL(urlStr);
- HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
- conn.setDoOutput(true);
- conn.setDoInput(true);
- conn.setRequestMethod("GET");
- InputStream stream = conn.getInputStream();
- BufferedReader in = new BufferedReader(new InputStreamReader(stream));
- String str = "";
- while ((str = in.readLine()) != null) {
- response.append(str);
- }
- in.close();
- return response.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement