Created
March 14, 2012 14:27
-
-
Save Eugeny/2036831 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package by.fksis.schedule.parser; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.htmlparser.Node; | |
import org.htmlparser.Parser; | |
import org.htmlparser.filters.CssSelectorNodeFilter; | |
import org.htmlparser.util.NodeList; | |
import org.htmlparser.util.ParserException; | |
public class ScheduleParser { | |
private final static String URL = "http://www.bsuir.by/psched/schedulegroup?group="; | |
private final static int DEFAULT_SUBGROUPS = 3; | |
private final static int DEFAULT_WEEKS = 15; | |
private final static String[] TIMESLOTS = new String[] { "8:00-9:35", | |
"9:45-11:20", "11:40-13:15", "13:25-15:00" }; | |
private List<Class> classes = new ArrayList<Class>(); | |
public static ScheduleParser parse(String group) { | |
ScheduleParser parser = new ScheduleParser(); | |
try { | |
Parser htmlParser = new Parser(URL + group); | |
NodeList nodes = htmlParser | |
.extractAllNodesThatMatch(new CssSelectorNodeFilter("td")); | |
Node[] nodeArray = nodes.toNodeArray(); | |
for (int day = 0; day < nodeArray.length / 8; day++) { | |
Node[] weekNodes = nodeArray[day * 8 + 1].getChildren() | |
.toNodeArray(); | |
Node[] timeNodes = nodeArray[day * 8 + 2].getChildren() | |
.toNodeArray(); | |
Node[] subgrNodes = nodeArray[day * 8 + 3].getChildren() | |
.toNodeArray(); | |
Node[] classNodes = nodeArray[day * 8 + 4].getChildren() | |
.toNodeArray(); | |
Node[] typeNodes = nodeArray[day * 8 + 5].getChildren() | |
.toNodeArray(); | |
Node[] roomNodes = nodeArray[day * 8 + 6].getChildren() | |
.toNodeArray(); | |
Node[] teacherNodes = nodeArray[day * 8 + 7].getChildren() | |
.toNodeArray(); | |
for (int idx = 0; idx < classNodes.length; idx++) { | |
Class cls = new Class(); | |
cls.day = day; | |
cls.name = extractText(classNodes[idx]); | |
cls.teacher = extractText(teacherNodes[idx]); | |
cls.type = extractText(typeNodes[idx]); | |
cls.room = extractText(roomNodes[idx]); | |
cls.weeks = parseMask(extractText(weekNodes[idx]), | |
DEFAULT_WEEKS); | |
cls.subgroups = parseMask(extractText(subgrNodes[idx]), | |
DEFAULT_SUBGROUPS); | |
for (int ts = 0; ts < TIMESLOTS.length; ts++) | |
if (TIMESLOTS[ts].equals(extractText(timeNodes[idx]))) | |
cls.timeslot = ts; | |
parser.classes.add(cls); | |
} | |
} | |
} catch (ParserException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
return parser; | |
} | |
public Class[] getClasses() { | |
return (Class[]) classes.toArray(new Class[] {}); | |
} | |
private static String extractText(Node n) { | |
if (n.getChildren().size() >= 2) { | |
return n.getFirstChild().getText(); | |
} | |
return null; | |
} | |
private static int parseMask(String mask, int def) { | |
int res = 0; | |
if (mask == null) | |
return def; | |
for (String elem : mask.split(",")) | |
res += Math.pow(2, Integer.parseInt(elem) - 1); | |
return (res == 0) ? def : res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment