Java tutorial
/* * Copyright 2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.github.carlomicieli.footballdb.starter.parsers; import io.github.carlomicieli.footballdb.starter.documents.DocumentDownloader; import io.github.carlomicieli.footballdb.starter.documents.PathBuilder; import io.github.carlomicieli.footballdb.starter.domain.Draft; import io.github.carlomicieli.footballdb.starter.domain.DraftedPlayer; import io.github.carlomicieli.footballdb.starter.utils.TryConvert; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.time.Year; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @author Carlo Micieli */ @Component public class DraftParser extends Parser<Draft> { @Autowired public DraftParser(DocumentDownloader docs) { super(docs); } @Override protected PathBuilder pathBuilder() { return PathBuilder.proFootballReference(); } @Override protected Draft parseDocument(Document doc) { Draft.Builder builder = Draft.builder().year(extractYear(doc)); picks(doc).forEach(builder::pick); return builder.build(); } protected Year extractYear(Document doc) { String title = doc.title(); return Year.parse(title.substring(0, 4)); } protected Optional<Element> picksTable(Document doc) { return Optional.ofNullable(doc.getElementById("drafts")); } protected List<DraftedPlayer> picks(Document doc) { Stream<Element> rows = picksTable(doc).flatMap(this::extractTableBody).map(this::extractTableRows) .orElseThrow(() -> new NoSuchElementException("Drafts table not found")); return rows.map(this::mapToDraftedPlayer).collect(Collectors.toList()); } private Stream<Element> extractTableRows(Element table) { Predicate<Element> withoutCssClass = e -> e.className().equals(""); return table.children().stream().filter(withoutCssClass); } private Optional<Element> extractTableBody(Element tab) { return tab.children().stream().filter(e -> e.tagName().equals("tbody")).findFirst(); } private DraftedPlayer mapToDraftedPlayer(Element e) { if (!e.tagName().equals("tr")) { throw new IllegalArgumentException("Invalid tag"); } int round = TryConvert.toIntegerOrGet(e.child(0).text(), -1); int pick = TryConvert.toIntegerOrGet(e.child(1).text(), -1); String team = e.child(2).text(); String name = e.child(3).text(); String pos = e.child(4).text(); String college = e.child(27).text(); return Draft.newPick().college(college).name(name).round(round).number(pick).position(pos).team(team) .build(); } }