Skip to content

Instantly share code, notes, and snippets.

@kumarsoumya
Created November 17, 2013 07:57
Show Gist options
  • Save kumarsoumya/7510623 to your computer and use it in GitHub Desktop.
Save kumarsoumya/7510623 to your computer and use it in GitHub Desktop.
Read the comments section at top.
/*
* @author: Soumya Kumar
*
* The following program scrapes the webpage "https://lslab.ics.purdue.edu/icsWeb/AvailableStations"
* and parses it get the current lab availability information.
*
* Dependency: Jsoup Parser
*
* */
/************************************************************************************/
/* Sample Output - Taken at 2:54 AM on 11/17/2013
* BCC204=-1.0
* MRDH146S=-1.0
* BRES201=-1.0
* RHPH272=-1.0
* RHPH316=-1.0
* HAMP3144=-1.0
* HAMP2215=-1.0
* PHYS026=-1.0
* PHYS022=-1.0
* PHYS117=-1.0
* PHYS116=-1.0
* PHYS014=-1.0
* PHYS290=-1.0
* MATHB010=73.52941176470588
* MATH311=-1.0
* SC283=-1.0
* SCG046=-1.0
* SC189=-1.0
* SC183=-1.0
* SC246=-1.0
* SC277=-1.0
* SC179=-1.0
* SC231=-1.0
* SC289=-1.0
* WTHR214=-1.0
* WTHR212=-1.0
* WTHR114=-1.0
* WTHR307=-1.0
* WTHR305=-1.0
* HEAV227=-1.0
* GRIS121=-1.0
* GRIS120=-1.0
* BRNGB286=-1.0
* BRNGB282=-1.0
* BRNGB280=-1.0
* BRNGB275=-1.0
* BRNGB274=-1.0
* BRNGB291=-1.0
* MTHW116=-1.0
* MTHW301=-1.0
* STEW135=-1.0
* STEW102=-1.0
* HIKSG950=71.1864406779661
* HIKSG980D=71.42857142857143
* HIKSG980C=100.0
* LILYG428=-1.0
* LILY2400=-1.0
* LYNN2133=-1.0
* MCUTC216=-1.0
*/
/*******************************************************************************************************/
import org.jsoup.nodes.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HTMLLabScrapper {
/*
* -1.0 means closed
* -2.0 means class in session
* else return percentage available computers
*/
/*
* @return percentage of lab availibity or 1.0 or 2.0
*
*/
public static double getLabInfo(String labUrl) {
Document doc=null;
try {
doc = Jsoup.connect(labUrl).get();
} catch (Exception e) {
System.out.println("Error connecting using Jsoup");
}
int count = 0;
String temp[];
double avail=0; //number of available computers
double inUse=0; //number of computer in use
Elements links = doc.select("span");
for (Element link : links) {
count++;
if (count==2) {
temp=link.text().split(" ");
if (temp[3].equals("OPEN."))
;
else if(temp[3].equals("CLOSED."))
return -1.0;
else
return -2.0;
}
if (count==3) {
temp=link.text().split(" ");
inUse=Double.parseDouble(temp[2]);
}
if (count==4) {
temp=link.text().split(" ");
avail=Double.parseDouble(temp[2]);
}
}
return (avail-inUse)/avail*100;
}
public static void main(String[] args) {
String[] labUrls =
{
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=BCC&room=204",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=MRDH&room=146S",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=BRES&room=201",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=RHPH&room=272",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=RHPH&room=316",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=HAMP&room=3144",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=HAMP&room=2215",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=PHYS&room=026",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=PHYS&room=022",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=PHYS&room=117",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=PHYS&room=116",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=PHYS&room=014",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=PHYS&room=290",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=MATH&room=B010",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=MATH&room=311",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=SC&room=283",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=SC&room=G046",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=SC&room=189",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=SC&room=183",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=SC&room=246",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=SC&room=277",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=SC&room=179",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=SC&room=231",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=SC&room=289",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=WTHR&room=214",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=WTHR&room=212",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=WTHR&room=114",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=WTHR&room=307",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=WTHR&room=305",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=HEAV&room=227",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=GRIS&room=121",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=GRIS&room=120",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=BRNG&room=B286",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=BRNG&room=B282",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=BRNG&room=B280",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=BRNG&room=B275",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=BRNG&room=B274",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=BRNG&room=B291",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=MTHW&room=116",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=MTHW&room=301",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=STEW&room=135",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=STEW&room=102",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=HIKS&room=G950",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=HIKS&room=G980D",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=HIKS&room=G980C",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=LILY&room=G428",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=LILY&room=2400",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=LYNN&room=2133",
"https://lslab.ics.purdue.edu/icsWeb/LabInfo?building=MCUT&room=C216"
};
String labName;
String[] temp;
for (int i=0; i<labUrls.length; i++) {
labName = labUrls[i].substring(53, labUrls[i].length());
temp = labName.split("&room=");
System.out.print(temp[0]);
System.out.print(temp[1]);
System.out.println("="+getLabInfo(labUrls[i]));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment