Search in sources :

Example 1 with RankFilter

use of com.github.mob41.osums.search.RankFilter in project osumer by mob41.

the class OsumsOldParser method searchOnlineMaps.

@Override
public SearchResult searchOnlineMaps(String keywords, SearchFilter[] filters, int page) throws WithDumpException {
    if (keywords == null) {
        keywords = "";
    }
    String url = SEARCH_URL + keywords;
    String rank = null;
    if (filters != null) {
        for (SearchFilter filter : filters) {
            if (filter instanceof RankFilter) {
                rank = ((RankFilter) filter).getRank();
            }
            url = filter.handleUrl(url);
        }
    }
    if (rank == null) {
        rank = RANK_ANY;
    }
    if (rank.equals(RANK_RANKED)) {
        url += "&r=" + RANK_RANKED_INT;
    } else if (rank.equals(RANK_PENDING)) {
        url += "&r=" + RANK_PENDING_INT;
    } else if (rank.equals(RANK_QUALIFIED)) {
        url += "&r=" + RANK_QUALIFIED_INT;
    } else if (rank.equals(RANK_LOVED)) {
        url += "&r=" + RANK_LOVED_INT;
    } else if (rank.equals(RANK_ANY)) {
        url += "&r=" + RANK_ALL_INT;
    } else {
        System.out.println("Unknown beatmap rank: " + rank);
    }
    try {
        String data = getHttpCookiedContent(url + "&page=" + page);
        Document doc = Jsoup.parse(data);
        Element pagination = doc.getElementsByClass("pagination").first();
        String paginationText = pagination.html();
        int totalPages = 1;
        int brIndex = paginationText.indexOf("<br>");
        if (brIndex == -1) {
            throw new WithDumpException(paginationText, "Get br index in pagination", "Validate brIndex != -1", "Extract pagination text before br", "Could not find at least one br element!", false);
        }
        String pageText = paginationText.substring(0, brIndex);
        // 11
        int displayingIndex = pageText.indexOf("Displaying ");
        // 4
        int toIndex = pageText.indexOf(" to ");
        // 4
        int ofIndex = pageText.indexOf(" of ");
        // 9
        int resultIndex = pageText.indexOf(" results.");
        if (displayingIndex == -1 || toIndex == -1 || ofIndex == -1 || resultIndex == -1) {
            throw new WithDumpException(paginationText, "Get all text indexes", "Validate all text indexes != -1", "Extract result pages data", "The pagination is invalid or unregonized.", false);
        }
        // int nowPage = -1;
        int currPageMaps = -1;
        int totalResultMaps = -1;
        try {
            // nowPage = Integer.parseInt(pageText.substring(displayingIndex + 11, toIndex));
            String currPageMapsStr = pageText.substring(toIndex + 4, ofIndex);
            if (!currPageMapsStr.equals("many")) {
                currPageMaps = Integer.parseInt(currPageMapsStr);
            }
            String totalResultMapsStr = pageText.substring(ofIndex + 4, resultIndex);
            if (!totalResultMapsStr.equals("many")) {
                totalResultMaps = Integer.parseInt(totalResultMapsStr);
            }
        } catch (NumberFormatException e) {
            throw new WithDumpException(pageText, "Validate all text indexes != -1", "Extract result pages data", "Get page data", "Pagination number-text data cannot be decoded as number.", false, e);
        }
        if (totalResultMaps != -1 && currPageMaps != -1 && currPageMaps != totalResultMaps) {
            currPageMaps -= 40 * (page - 1);
            // System.out.println("Using method 1 to identify Total Pages");
            // System.out.println("TRM/CPM: " + totalResultMaps + " / " + currPageMaps);
            float calc = ((float) totalResultMaps / currPageMaps);
            totalPages = (int) calc;
            if (calc != totalPages) {
                // System.out.println("Calc != totalPages: " + calc + " != " + totalPages);
                totalPages++;
            }
        // System.out.println("Now total pages: " + totalPages);
        } else {
            // System.out.println("Using method 2 to identify Total Pages");
            Elements pageLinkEls = pagination.children();
            int size = pageLinkEls.size();
            if (size < 2) {
                totalPages = 1;
            /*
                    throw new WithDumpException(pageText, "Get page links size",
                            "Validate children size >= 2", "Get last page link element",
                            "Invalid page! The page has less than 2 page links!", false);
                    */
            } else {
                Element lastPageLinkEl = pageLinkEls.get(size - 2);
                if (lastPageLinkEl != null) {
                    int lastPageNum = -1;
                    try {
                        lastPageNum = Integer.parseInt(lastPageLinkEl.html());
                    } catch (NumberFormatException e) {
                        throw new WithDumpException(pageText, "Get last page link element", "Parse last page number String to number", "Set as total page", "Pagination last page number-text data cannot be decoded as number.", false, e);
                    }
                    if (lastPageNum > totalPages) {
                        // System.out.println("Last page num is bigger than total pages: " + lastPageNum + " > " + totalPages);
                        totalPages = lastPageNum;
                    } else {
                    // System.out.println("Last page num is sammler than total pages: " + lastPageNum + " < " + totalPages);
                    }
                }
            }
        }
        SongResult[] results = parseSearch(rank, doc);
        return new SearchResult(results, page, totalPages);
    } catch (Exception e) {
        throw new WithDumpException(url, "(Try&catch try) getting search result links", "Throw debuggable exception on catch", "(End of function)", "Error occurred when getting search result links", false, e);
    }
}
Also used : WithDumpException(com.github.mob41.osumer.debug.WithDumpException) SongResult(com.github.mob41.osums.search.SongResult) Element(org.jsoup.nodes.Element) SearchFilter(com.github.mob41.osums.search.SearchFilter) SearchResult(com.github.mob41.osums.search.SearchResult) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) IOException(java.io.IOException) WithDumpException(com.github.mob41.osumer.debug.WithDumpException) RankFilter(com.github.mob41.osums.search.RankFilter)

Aggregations

WithDumpException (com.github.mob41.osumer.debug.WithDumpException)1 RankFilter (com.github.mob41.osums.search.RankFilter)1 SearchFilter (com.github.mob41.osums.search.SearchFilter)1 SearchResult (com.github.mob41.osums.search.SearchResult)1 SongResult (com.github.mob41.osums.search.SongResult)1 IOException (java.io.IOException)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1