package com.mavlushechka.animarfo.parser; import com.mavlushechka.animarfo.anime.Anime; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; public class Parser { private static final Logger LOGGER = Logger.getLogger(Parser.class.getName()); static { LOGGER.setLevel(Level.ALL); } public static Document getPage(String url) throws IOException { return Jsoup.parse(new URL(url), 5000); } public static byte getSizeOfSearchedAnime(String page) throws IOException { LOGGER.info("Getting size of searched anime"); Element animeList = Parser.getPage(page).select("div[id=dle-content]").first(); if (animeList == null) return 0; Elements anime = animeList.select("div[class=th-item]"); LOGGER.fine("Got size of searched anime"); return (byte) anime.size(); } public static Anime[] getAnime(String page) throws IOException { LOGGER.info("Parsing an anime"); String typeOfAnime = Anime.type; Element animeList = Parser.getPage(page).select("div[id=dle-content]").first(); assert animeList != null; Elements namesTemp = animeList.select("div[class=th-title]"); Elements descriptionsTemp = animeList.select("div[class=th-tip-text]"); Elements yearsTemp = null; if (typeOfAnime != null && !typeOfAnime.equals("Ongoing")) yearsTemp = animeList.select("div[class=th-tip-meta fx-row fx-middle fx-start]"); Elements genresTemp= animeList.select("ul[class=th-tip-list]"); Elements seriesTemp = null; if (typeOfAnime != null && !typeOfAnime.equals("Films")) seriesTemp = animeList.select("div[class=th-title]"); Elements ratingsTemp = animeList.select("div[class=th-rating]"); Elements imagesTemp = animeList.select("div[class=th-img img-resp-vert img-fit]"); Elements urlsTemp = animeList.select("div[class=th-itemb]").select("a[class=th-in]"); LOGGER.fine("Got all required data"); byte countOfAnime = (byte) namesTemp.size(); Anime.size = countOfAnime; ArrayList names = new ArrayList<>(); for (Element name : namesTemp) { if (typeOfAnime != null && !typeOfAnime.equals("Films")) { String text = Jsoup.parse(String.valueOf(name)).text(); int start = 0; int end = text.indexOf(" ["); char[] dst = new char[end - start]; text.getChars(start, end, dst, 0); names.add(String.valueOf(dst)); } else { names.add(Jsoup.parse(String.valueOf(name)).text()); } } ArrayList descriptions = new ArrayList<>(); for (Element description : descriptionsTemp) { descriptions.add(Jsoup.parse(String.valueOf(description)).text()); } ArrayList years = new ArrayList<>(); if (typeOfAnime != null && !typeOfAnime.equals("Ongoing") && !Anime.isSearching) { for (Element year : yearsTemp) { Element yearTemp = year.child(0); years.add(Jsoup.parse(String.valueOf(yearTemp)).text()); } } ArrayList genres = new ArrayList<>(); if (!Anime.isSearching) { for (Element oneGenres : genresTemp) { Element genre = oneGenres.child(2); String text = Jsoup.parse(String.valueOf(genre)).text(); int start = 6; int end = text.length(); char[] dst = new char[end - start]; text.getChars(start, end, dst, 0); genres.add(String.valueOf(dst)); } } ArrayList series = new ArrayList<>(); if (typeOfAnime != null && !typeOfAnime.equals("Films") && !Anime.isSearching) { for (Element oneSeries : seriesTemp) { String text = oneSeries.toString(); int start = text.indexOf("[") + 1; int end = text.indexOf("]"); char[] dst = new char[end - start]; text.getChars(start, end, dst, 0); series.add(String.valueOf(dst)); } } ArrayList ratings = new ArrayList<>(); for (Element rating : ratingsTemp) { ratings.add(Jsoup.parse(String.valueOf(rating)).text()); } ArrayList images = new ArrayList<>(); for (Element image : imagesTemp) { String text = Objects.requireNonNull(image.select("img").first()).dataset().get("src"); if (!text.contains("statics")) { images.add("https://online.anidub.com/" + text); } else { images.add(text); } } ArrayList urls = new ArrayList<>(); for (Element url : urlsTemp) { String text = url.toString(); int start = text.indexOf("href=\"") + 6; int end = text.indexOf("\">"); char[] dst = new char[end - start]; text.getChars(start, end, dst, 0); urls.add(String.valueOf(dst)); } LOGGER.fine("Sorted all data"); Anime[] anime = new Anime[countOfAnime]; for (int i = 0; i < countOfAnime; i++) { if (typeOfAnime != null && typeOfAnime.equals("Films")) { anime[i] = new Anime(names.get(i), descriptions.get(i), genres.get(i), years.get(i), null, ratings.get(i), images.get(i), urls.get(i)); } else if (typeOfAnime != null && typeOfAnime.equals("Ongoing")) { anime[i] = new Anime(names.get(i), descriptions.get(i), genres.get(i), null, series.get(i), ratings.get(i), images.get(i), urls.get(i)); } else if (typeOfAnime != null && typeOfAnime.equals("Serials")) { anime[i] = new Anime(names.get(i), descriptions.get(i), genres.get(i), years.get(i), series.get(i), ratings.get(i), images.get(i), urls.get(i)); } else if (Anime.isSearching) { anime[i] = new Anime(names.get(i), descriptions.get(i), null, null, null, ratings.get(i), images.get(i), urls.get(i)); } } return anime; } }