Untitled
unknown
java
10 months ago
3.5 kB
6
Indexable
@Service
public class LectureParserImpl implements ILectureParser
{
private static final Logger LOG = LoggerFactory.getLogger(LectureParserImpl.class);
private static final String DATE_PATTERN = "dd.MM.yyyy";
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_PATTERN);
public Lecture parseLectureDetails(String html)
{
Document document = Jsoup.parse(html);
return Lecture.builder().studentCount(parseStudentCount(document))
.itnStudentCount(parseItnStudentCount(document))
.lectureName(parseElementText(document, "#ctl06_NazwaPrzedmiotyLabel"))
.lectureCode(parseElementText(document, "#ctl06_KodPrzedmiotuLabel"))
.lectureType(parseLectureType(document)).professor(parseElementText(document, "#ctl06_DydaktycyLabel"))
.building(parseElementText(document, "#ctl06_BudynekLabel"))
.classRoomNumber(parseElementText(document, "#ctl06_SalaLabel")).lectureDate(parseLectureDate(document))
.startTime(parseElementText(document, "#ctl06_GodzRozpLabel"))
.endTime(parseElementText(document, "#ctl06_GodzZakonLabel"))
.duration(parseElementText(document, "#ctl06_CzasTrwaniaLabel")).build();
}
private int parseStudentCount(Document document)
{
try
{
String studentCountText = parseElementText(document, "#ctl06_LiczbaStudentowLabel");
if (studentCountText.isEmpty())
{
return 0;
}
return Integer.parseInt(studentCountText.split(" ")[0]);
}
catch (Exception e)
{
LOG.error("Failed to parse student count:", e);
return 0;
}
}
private int parseItnStudentCount(Document document)
{
try
{
String studentCountText = parseElementText(document, "#ctl06_LiczbaStudentowLabel");
String[] parts = studentCountText.split(" ");
return parts.length > 2 && parts[2].equals("ITN") ? 1 : 0;
}
catch (Exception e)
{
LOG.error("Failed to parse ITN student count:", e);
return 0;
}
}
private LectureType parseLectureType(Document document)
{
try
{
String typeText = parseElementText(document, "#ctl06_TypZajecLabel");
return LectureType.fromString(typeText);
}
catch (Exception e)
{
LOG.error("Failed to parse lecture type:", e);
return LectureType.UNKNOWN;
}
}
private Date parseLectureDate(Document document)
{
try
{
String dateText = parseElementText(document, "#ctl06_DataZajecLabel");
LocalDate localDate = LocalDate.parse(dateText.trim(), DATE_FORMATTER);
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
catch (Exception e)
{
LOG.error("Failed to parse lecture date", e);
return new Date();
}
}
private String parseElementText(Document document, String cssSelector)
{
try
{
Element element = document.select(cssSelector).first();
return element != null ? element.text().trim() : "";
}
catch (Exception e)
{
LOG.error("Failed to parse element text", e);
return "";
}
}
}Editor is loading...
Leave a Comment