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.PlayerCareer; 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.util.*; import java.util.stream.Collectors; /** * @author Carlo Micieli */ @Component public class PlayerCareerParser extends Parser<PlayerCareer> { @Autowired public PlayerCareerParser(DocumentDownloader docs) { super(docs); } @Override protected PathBuilder pathBuilder() { return PathBuilder.nflDotCom(); } @Override protected PlayerCareer parseDocument(Document doc) { return PlayerCareer.builder().seasons(seasonRows(doc)).build(); } protected static Optional<Element> careerTable(Document doc) { Optional<Element> table = doc.getElementsByClass("data-table1").stream() .filter(e -> e.attr("summary").startsWith("Career Stats")).findFirst(); return table; } protected static List<Map<String, String>> seasonRows(Document doc) { Elements rows = careerTable(doc).map(e -> e.getElementsByTag("tr")) .orElseThrow(() -> new RuntimeException("")); List<Map<String, String>> seasons = rows.stream().filter(e -> e.className().equals("")) .filter(e -> e.children().size() > 1).map(PlayerCareerParser::extractValues) .filter(mv -> !mv.get("SeasonStats").equals("TOTAL")).collect(Collectors.toList()); return seasons; } private static Map<String, String> extractValues(Element el) { Map<String, String> values = new HashMap<>(); values.put("SeasonStats", el.child(0).text()); values.put("Team", el.child(1).text()); values.put("G", el.child(2).text()); values.put("GS", el.child(3).text()); return Collections.unmodifiableMap(values); } }