summaryrefslogtreecommitdiff
path: root/src/main/java/com/mavlushechka/animarfo/parser/Parser.java
blob: a82ce0fcad245bf9444aca54def388531f7ffdcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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<String> 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<String> descriptions = new ArrayList<>();
        for (Element description : descriptionsTemp) {
            descriptions.add(Jsoup.parse(String.valueOf(description)).text());
        }

        ArrayList<String> 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<String> 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<String> 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<String> ratings = new ArrayList<>();
        for (Element rating : ratingsTemp) {
            ratings.add(Jsoup.parse(String.valueOf(rating)).text());
        }

        ArrayList<String> 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<String> 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;
    }
}