use of me.vertretungsplan.objects.SubstitutionSchedule in project substitution-schedule-parser by vertretungsplanme.
the class ParserUtil method parseSubstitutionSchedule.
/**
* Parses a substitution schedule.
*
* @param data A <code>SubstitutionScheduleData</code> instance containing information about the schedule you
* want to parse.
* @param credential A <code>Credential</code> subclass for authentication. If the schedule requires no
* authentication, use <code>null</code>.
* @param cp An optional <code>CookieProvider</code> implementation. This can be used if you want to reuse
* session cookies the next time you load the schedule. If you don't need it, pass
* <code>null</code>.
* @param handler An optional <code>DebuggingDataHandler</code> implementation. If you don't need it, pass
* <code>null</code>.
* @return The parsed substitution schedule.
* @throws JSONException When there's an error with your JSON configuration
* @throws CredentialInvalidException When the <code>Credential</code> you supplied was invalid
* @throws IOException When there was another error while loading or parsing the schedule
*/
public static SubstitutionSchedule parseSubstitutionSchedule(SubstitutionScheduleData data, Credential credential, CookieProvider cp, DebuggingDataHandler handler) throws JSONException, CredentialInvalidException, IOException {
SubstitutionScheduleParser parser = BaseParser.getInstance(data, cp);
if (credential != null)
parser.setCredential(credential);
if (handler != null)
((BaseParser) parser).setDebuggingDataHandler(handler);
SubstitutionSchedule schedule = parser.getSubstitutionSchedule();
for (String a : data.getAdditionalInfos()) {
BaseAdditionalInfoParser aParser = BaseAdditionalInfoParser.getInstance(a);
schedule.addAdditionalInfo(aParser.getAdditionalInfo());
}
return schedule;
}
use of me.vertretungsplan.objects.SubstitutionSchedule in project substitution-schedule-parser by vertretungsplanme.
the class UntisInfoHeadlessParser method getSubstitutionSchedule.
@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
Document doc = Jsoup.parse(httpGet(url, data.optString(PARAM_ENCODING, null)));
doc.setBaseUri(url);
Elements dayElems = doc.select("#vertretung > p > b, #vertretung > b");
Elements frames = doc.select("frame[src*=w00]");
if (dayElems.size() == 0 && frames.size() > 0) {
// doc is embedded in frame
doc = Jsoup.parse(httpGet(frames.get(0).absUrl("src"), data.optString(PARAM_ENCODING, null)));
dayElems = doc.select("#vertretung > p > b, #vertretung > b");
}
final List<String> allClasses = getAllClasses();
if (dayElems.size() > 0) {
// untis-info days
for (Element dayElem : dayElems) {
SubstitutionScheduleDay day = new SubstitutionScheduleDay();
day.setLastChangeString("");
String date = dayElem.text();
day.setDateString(date);
day.setDate(ParserUtils.parseDate(date));
Element next;
if (dayElem.parent().tagName().equals("p")) {
next = dayElem.parent().nextElementSibling().nextElementSibling();
} else {
next = dayElem.parent().select("p").first().nextElementSibling();
}
parseDay(day, next, v, null, allClasses);
}
} else if (doc.select("tr:has(td[align=center]):gt(0)").size() > 0) {
// untis-subst table
parseSubstitutionTable(v, null, doc);
}
v.setClasses(allClasses);
v.setTeachers(getAllTeachers());
return v;
}
use of me.vertretungsplan.objects.SubstitutionSchedule in project substitution-schedule-parser by vertretungsplanme.
the class UntisInfoParser method getSubstitutionSchedule.
@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
Document navbarDoc = Jsoup.parse(getNavbarDoc().replace(" ", ""));
Element select = navbarDoc.select("select[name=week]").first();
SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
String info = navbarDoc.select(".description").text();
String lastChange;
try {
lastChange = info.substring(info.indexOf("Stand:") + "Stand:".length()).trim();
} catch (Exception e) {
try {
String infoHtml = httpGet(baseUrl + "/frames/title.htm", data.optString(PARAM_ENCODING, null));
Document infoDoc = Jsoup.parse(infoHtml);
String info2 = infoDoc.select(".description").text();
lastChange = info2.substring(info2.indexOf("Stand:") + "Stand:".length()).trim();
} catch (Exception e1) {
lastChange = "";
}
}
List<String> allClasses = new ArrayList<>();
try {
allClasses = getAllClasses();
} catch (IOException e) {
// we only really need this for teacher schedules
if (!getLetter(data).equals("v")) {
throw e;
}
}
int successfulWeeks = 0;
HttpResponseException lastException = null;
for (Element option : select.children()) {
String week = option.attr("value");
String weekName = option.text();
if (data.optBoolean(PARAM_SINGLE_CLASSES, // backwards compatibility
data.optBoolean("single_classes", false)) || data.optString(PARAM_SCHEDULE_TYPE, "substitution").equals("timetable")) {
int classNumber = 1;
List<String> classesToSelect;
if (getLetter(data).equals("v")) {
classesToSelect = parseTeachers(getNavbarDoc(), data);
} else {
classesToSelect = parseClasses(getNavbarDoc(), data);
}
for (String klasse : classesToSelect) {
String url = getScheduleUrl(week, classNumber, data);
try {
parsePage(v, lastChange, klasse, url, weekName, allClasses);
} catch (HttpResponseException e) {
if (e.getStatusCode() == 500) {
// occurs in Hannover_MMBS
classNumber++;
continue;
} else {
throw e;
}
}
classNumber++;
}
successfulWeeks++;
} else {
String url = getScheduleUrl(week, 0, data);
try {
parsePage(v, lastChange, null, url, weekName, allClasses);
successfulWeeks++;
} catch (HttpResponseException e) {
lastException = e;
}
}
}
if (successfulWeeks == 0 && lastException != null) {
throw lastException;
}
v.setClasses(allClasses);
v.setTeachers(getAllTeachers());
v.setWebsite(data.optString("website", baseUrl + "/default.htm"));
return v;
}
use of me.vertretungsplan.objects.SubstitutionSchedule in project substitution-schedule-parser by vertretungsplanme.
the class UntisMonitorParser method getSubstitutionSchedule.
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
loginResponse = new LoginHandler(scheduleData, credential, cookieProvider).handleLoginWithResponse(executor, cookieStore);
SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
JSONArray urls = scheduleData.getData().getJSONArray(PARAM_URLS);
String encoding = scheduleData.getData().optString(PARAM_ENCODING, null);
List<Document> docs = new ArrayList<>();
for (int i = 0; i < urls.length(); i++) {
JSONObject url = urls.getJSONObject(i);
final String urlStr = url.getString(SUBPARAM_URL);
for (String dateUrl : ParserUtils.handleUrlWithDateFormat(urlStr)) {
loadUrl(dateUrl, encoding, url.getBoolean(SUBPARAM_FOLLOWING), docs);
}
}
for (Document doc : docs) {
if (scheduleData.getData().has(PARAM_EMBEDDED_CONTENT_SELECTOR)) {
for (Element part : doc.select(scheduleData.getData().getString(PARAM_EMBEDDED_CONTENT_SELECTOR))) {
SubstitutionScheduleDay day = parseMonitorDay(part, scheduleData.getData());
v.addDay(day);
}
} else if (doc.title().contains("Untis") || doc.html().contains("<!--<title>Untis")) {
SubstitutionScheduleDay day = parseMonitorDay(doc, scheduleData.getData());
v.addDay(day);
}
if (scheduleData.getData().has(PARAM_LAST_CHANGE_SELECTOR) && doc.select(scheduleData.getData().getString(PARAM_LAST_CHANGE_SELECTOR)).size() > 0) {
String text = doc.select(scheduleData.getData().getString(PARAM_LAST_CHANGE_SELECTOR)).first().text();
String lastChange;
Pattern pattern = Pattern.compile("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d,? \\d\\d:\\d\\d");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
lastChange = matcher.group();
} else {
lastChange = text;
}
v.setLastChangeString(lastChange);
v.setLastChange(ParserUtils.parseDateTime(lastChange));
}
}
if (scheduleData.getData().has(PARAM_WEBSITE)) {
v.setWebsite(scheduleData.getData().getString(PARAM_WEBSITE));
} else if (urls.length() == 1) {
v.setWebsite(urls.getJSONObject(0).getString("url"));
}
v.setClasses(getAllClasses());
v.setTeachers(getAllTeachers());
return v;
}
use of me.vertretungsplan.objects.SubstitutionSchedule in project substitution-schedule-parser by vertretungsplanme.
the class IphisDemoTest method demoTest.
@Test
public void demoTest() throws IOException, JSONException {
SubstitutionSchedule schedule = new SubstitutionSchedule();
parser.parseIphis(schedule, changes, courses, teachers, messages);
assertEquals(2, schedule.getDays().size());
SubstitutionScheduleDay firstDay = schedule.getDays().get(0);
assertEquals(new LocalDate(2017, 9, 29), firstDay.getDate());
assertEquals(4, firstDay.getSubstitutions().size());
}
Aggregations