Search in sources :

Example 1 with OsuBeatmap

use of com.github.mob41.osums.beatmap.OsuBeatmap in project osumer by mob41.

the class OsumsOldParser method parseSong.

@Override
public OsuSong parseSong(String originalUrl, Document doc) throws WithDumpException {
    boolean pageBeatmap = originalUrl.contains("/b/");
    Elements dwnLinks = doc.getElementsByClass("beatmap_download_link");
    if (dwnLinks.size() == 0) {
        throw new WithDumpException("", "Get elements by class \"beatmap_download_link\"", "Validate download link element. Throw exception if size() equal to 0", "Get the first element of the download links elements", "No element with class \"beatmap_download_link\", which should be at least one in a beatmap info page. Invalid beatmap URL?", true);
    }
    Element alnk = dwnLinks.get(0);
    String dwnLnk = alnk.attr("href");
    Element contentWithBgEl = doc.select("div.content-with-bg").first();
    if (contentWithBgEl == null) {
        throw new WithDumpException("", "Select \"div.content-with-bg\" and get the first element, assign to contentWithBgEl", "Validate contentWithBgEl is null or not", "Select \"div#tablist ul\" and get the first element, assign to tabList", "No div element with class \"content-with-bg\"", true);
    }
    // Element header1 = contentWithBgEl.select("h1").first();
    Element tabList = contentWithBgEl.select("div#tablist ul").first();
    if (tabList == null) {
        throw new WithDumpException("", "Select \"div#tablist ul\" and get the first element, assign to tabList", "Validate tabList is null or not", "Get tabList children and assign to tabListLi", "No div element with class \"tablist\" with ul", true);
    }
    Elements tabListLi = tabList.children();
    /*
		String[] tabStr = new String[tabListLi.size()];
		for (int i = 0; i < tabListLi.size(); i++){
			//<a> tag should be inside the <li> according to online code
			Element el = tabListLi.get(i).select(".beatmapTab span").first();
			
			if (el == null){
				throw new WithDumpException(
						tabListLi.get(i).html(),
						"(Loop of element tabList children) Select \".beatmapTab span\" from children and get the first element, assign to el",
						"Validate el is null or not",
						"Get the inner HTML of <span>",
						"No <a>/<span> tag in the difficulties tab index " + i + ". Page broke?",
						false);
			}
			
			tabStr[i] = el.html();
		}
		*/
    Element songInfo = contentWithBgEl.select("div table#songinfo tbody").first();
    if (songInfo == null) {
        throw new WithDumpException("", "Select \"div table#songinfo tbody# in contentWithBgEl and get the first element, assign to songInfo", "Validate songInfo is null or not", "Get songInfo children", "No songinfo element in contentWithBgEl. Page broke?", true);
    }
    Elements songInfoTrs = songInfo.children();
    if (songInfoTrs.size() < 7) {
        throw new WithDumpException(songInfoTrs.html(), "Get songInfo children", "Validate songInfo is enough <tr> elements (more than 7)", "(Lots of code) Get song info data from table", "No song info table <tr> / not enough <tr>. Only " + songInfoTrs.size() + "/7 Page broke?", false);
    }
    String[] tags = null;
    String genre = null;
    String source = null;
    String creator = null;
    String artist = null;
    String title = null;
    float circleSize = -1;
    float approachRate = -1;
    float hpDrain = -1;
    float accuracy = -1;
    float starDifficulty = -1;
    float bpm = -1;
    int good_rating = -1;
    int bad_rating = -1;
    float success_rate = -1;
    for (int i = 0; i < songInfoTrs.size(); i++) {
        Elements tr = songInfoTrs.get(i).children();
        Element el;
        Element el_a;
        switch(// even number index <td> elements are labels
        i) {
            case // [1]Artist[3]Circle size (starfield)[5]Approach rate(starfield)
            0:
                for (int j = 0; j < tr.size(); j++) {
                    // tr.size() should be 6
                    el = tr.get(j);
                    switch(j) {
                        case 0:
                            break;
                        case 1:
                            el_a = el.select("a[href]").first();
                            if (el_a == null) {
                                throw new WithDumpException(el.html(), "Select \"a[href]\" from el and assign to el_a", "Validate el_a is null or not", "Get inner HTML from <a> and assign to artist", "No artist related data. Page broke?", false);
                            }
                            artist = el_a.html();
                            break;
                        case 2:
                            break;
                        case // Circle size (Starfield)
                        3:
                            if (pageBeatmap) {
                                circleSize = parseStarfield("Circle Size", el);
                            }
                            break;
                        case 4:
                            break;
                        case // Approach rate (Starfield)
                        5:
                            if (pageBeatmap) {
                                approachRate = parseStarfield("Approach Rate", el);
                            }
                            break;
                    }
                }
                break;
            case // [1]Title[3]HP Drain(starfield)[5]Star difficulty(starfield)
            1:
                for (int j = 0; j < tr.size(); j++) {
                    // tr.size() should be 6
                    el = tr.get(j);
                    switch(j) {
                        case 0:
                            break;
                        case 1:
                            el_a = el.select("a[href]").first();
                            if (el_a == null) {
                                throw new WithDumpException(el.html(), "Select \"a[href]\" from el and assign to el_a", "Validate el_a is null or not", "Get inner HTML from <a> and assign to title", "No title related data. Page broke?", false);
                            }
                            title = el_a.html();
                            break;
                        case 2:
                            break;
                        case // HP Drain (Starfield)
                        3:
                            if (pageBeatmap) {
                                hpDrain = parseStarfield("HP Drain", el);
                            }
                            break;
                        case 4:
                            break;
                        case // Star Diff (Starfield)
                        5:
                            if (pageBeatmap) {
                                starDifficulty = parseStarfield("Star Difficulty", el);
                            }
                            break;
                    }
                }
                break;
            case // [1]Creator[3]Accuracy(starfield)[5]Length (format: x:xx (x:xx drain))
            2:
                for (int j = 0; j < tr.size(); j++) {
                    // tr.size() should be 6
                    el = tr.get(j);
                    switch(j) {
                        case 0:
                            break;
                        case 1:
                            el_a = el.select("a[href]").first();
                            if (el_a == null) {
                                throw new WithDumpException(el.html(), "Select \"a[href]\" from el and assign to el_a", "Validate el_a is null or not", "Get inner HTML from <a> and assign to creator", "No creator related data. Page broke?", false);
                            }
                            creator = el_a.html();
                            break;
                        case 2:
                            break;
                        case // Accuracy (Starfield)
                        3:
                            if (pageBeatmap) {
                                accuracy = parseStarfield("Accuracy", el);
                            }
                            break;
                        case 4:
                            break;
                        case // Length (in format: x:xx (x:xx drain)) (figuring to get)
                        5:
                            break;
                    }
                }
                break;
            case // [1]Source[3]Genre[5]BPM
            3:
                for (int j = 0; j < tr.size(); j++) {
                    // tr.size() should be 6
                    el = tr.get(j);
                    switch(j) {
                        case 0:
                            break;
                        case 1:
                            el_a = el.select("a[href]").first();
                            if (el_a == null) {
                                throw new WithDumpException(el.html(), "Select \"a[href]\" from el and assign to el_a", "Validate el_a is null or not", "Get inner HTML from <a> and assign to source", "No source related data. Page broke?", false);
                            }
                            source = el_a.html();
                            break;
                        case 2:
                            break;
                        case // Format: xx (xx), however we are not getting (xx)
                        3:
                            el_a = el.select("a[href]").first();
                            if (el_a == null) {
                                throw new WithDumpException(el.html(), "Select \"a[href]\" from el and assign to el_a", "Validate el_a is null or not", "Get inner HTML from <a> and assign to genre", "No genre related data. Page broke?", false);
                            }
                            genre = el_a.html();
                            break;
                        case 4:
                            break;
                        case // Floating BPM
                        5:
                            if (el == null) {
                                throw new WithDumpException(null, "(Loop, switch)", "Validate el is null or not", "Parse float from el inner HTML", "No bpm related data. Page broke?", false);
                            }
                            try {
                                bpm = Float.parseFloat(el.html().replaceAll(",", ""));
                            } catch (NumberFormatException e) {
                                throw new WithDumpException(el.html(), "Validate el is null or not", "Parse float from el inner HTML", "(Break switch, Loop)", "Cannot parse float. BPM is not a floating number", false, e);
                            }
                            break;
                    }
                }
                break;
            case 4:
                for (int j = 0; j < tr.size(); j++) {
                    // tr.size() should be 6
                    el = tr.get(j);
                    switch(j) {
                        case 0:
                            break;
                        case 1:
                            Elements tagsEls = el.select("a[href]");
                            if (tagsEls.size() == 0) {
                                throw new WithDumpException(el.html(), "Select \"a[href]\" from el and assign to el_a", "Validate el_a is null or not", "Get inner HTML from <a> and assign to tags", "No tags related data. Page broke?", false);
                            }
                            tags = new String[tagsEls.size()];
                            for (int x = 0; x < tags.length; x++) {
                                tags[x] = tagsEls.get(x).html();
                            }
                            break;
                        case 2:
                            break;
                        case // Well this is tricky, a bad rating in the first <td>, and the good one in the second <td>
                        3:
                            Elements ratingEls = el.select("table tbody tr td");
                            if (ratingEls.size() < 2) {
                                throw new WithDumpException(el.html(), "Select \"table tbody tr td\" from el and assign to ratingEls", "Validate ratingEls elements is enough (more than 2)", "Get the first element in ratingEls and assign to el_a", "Not enough " + ratingEls.size() + "/2 / no rating related data. Page broke?", false);
                            }
                            el_a = ratingEls.get(0);
                            try {
                                bad_rating = Integer.parseInt(el_a.html().replaceAll(",", ""));
                            } catch (NumberFormatException e) {
                                throw new WithDumpException(el_a.html(), "Get the first element in ratingEls and assign to el_a", "Parse integer from el_a inner HTML", "Get the second element in ratingEls and assign to el_a", "Cannot parse integer. Bad rating is not an integer", false, e);
                            }
                            el_a = ratingEls.get(1);
                            try {
                                good_rating = Integer.parseInt(el_a.html().replaceAll(",", ""));
                            } catch (NumberFormatException e) {
                                throw new WithDumpException(el_a.html(), "Get the second element in ratingEls and assign to el_a", "Parse integer from el_a inner HTML", "(Break switch, Loop)", "Cannot parse integer. Good rating is not an integer", false, e);
                            }
                            break;
                        case 4:
                            break;
                        case // Floating success rate
                        5:
                            el_a = el.select("td").first();
                            if (el_a != null && el_a.html().contains("Not yet played!")) {
                                success_rate = -1;
                                break;
                            }
                            el_a = el.select("td b").first();
                            if (el_a == null) {
                                throw new WithDumpException(el.html(), "Select \"td b\" from el and assign to el_a", "Validate el_a is null or not", "(Try&catch scope) Get inner HTML from <a> and assign to str", "No success rate related data. Page broke?", false);
                            }
                            try {
                                String str = el_a.html();
                                int index = str.indexOf("%");
                                success_rate = Float.parseFloat(str.substring(0, index));
                            } catch (NumberFormatException e) {
                                throw new WithDumpException(el_a.html(), "Validate el_a is null or not", "Parse float from el_a inner HTML", "(Break switch, Loop)", "Cannot parse float. Success rate is not a floating number", false, e);
                            }
                            break;
                    }
                }
                break;
            case // figuring out
            5:
                break;
            case // figuring out
            6:
                break;
        }
    }
    Element thumbEl = contentWithBgEl.select("div.paddingboth div.posttext a[href] img[src].bmt").first();
    if (thumbEl == null) {
        throw new WithDumpException("", "Select \"div.paddingboth div.posttext a[href] img[src].bmt\" from contentWithBgEl and assign to thumbEl", "Validate thumbEl is null or not", "Get attribute \"src\" from thumbEl", "No thumb image related data. Page broke?", true);
    }
    String thumbUrl = thumbEl.attr("src");
    if (pageBeatmap) {
        return new OsuBeatmap(originalUrl, artist + " - " + title, title, artist, creator, source, genre, dwnLnk, thumbUrl, bad_rating, good_rating, bpm, circleSize, approachRate, hpDrain, accuracy, starDifficulty, success_rate, pageBeatmap);
    } else {
        return new OsuSong(originalUrl, artist + " - " + title, title, artist, creator, source, genre, dwnLnk, thumbUrl, bad_rating, good_rating, bpm, pageBeatmap);
    }
}
Also used : WithDumpException(com.github.mob41.osumer.debug.WithDumpException) OsuBeatmap(com.github.mob41.osums.beatmap.OsuBeatmap) Element(org.jsoup.nodes.Element) OsuSong(com.github.mob41.osums.beatmap.OsuSong) Elements(org.jsoup.select.Elements)

Example 2 with OsuBeatmap

use of com.github.mob41.osums.beatmap.OsuBeatmap in project osumer by mob41.

the class MainController method addQueue.

private void addQueue(String beatmapUrlId, boolean showPreview, boolean changeTab) {
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(AppMain.class.getResource("/view/ProgressDialogLayout.fxml"));
    DialogPane progressPane = null;
    try {
        progressPane = (DialogPane) loader.load();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    ProgressDialogController progressController = loader.getController();
    progressController.getHeaderText().setText("Beatmap Download");
    progressController.getStatusText().setText("Status: Initializing");
    progressController.getProgressBar().setProgress(-1);
    Alert progressDialog = new Alert(AlertType.NONE);
    progressDialog.initStyle(StageStyle.UTILITY);
    progressDialog.initModality(Modality.APPLICATION_MODAL);
    progressDialog.setTitle("Beatmap Download");
    progressDialog.setDialogPane(progressPane);
    progressDialog.getButtonTypes().add(ButtonType.CANCEL);
    progressDialog.show();
    String url = null;
    boolean isBeatmap = false;
    if (beatmapUrlId.matches("^[sb]\\d+") && beatmapUrlId.length() > 1) {
        if (beatmapUrlId.startsWith("b")) {
            isBeatmap = true;
            url = "https://osu.ppy.sh/b/" + beatmapUrlId.substring(1);
        } else {
            url = "https://osu.ppy.sh/s/" + beatmapUrlId.substring(1);
        }
    } else {
        if (osums.isVaildBeatmapUrl(beatmapUrlId)) {
            url = beatmapUrlId;
            if (url.contains("b/")) {
                isBeatmap = true;
            }
        } else {
            Alert alert = new Alert(AlertType.WARNING, "Please enter a valid osu! beatmap link.", ButtonType.OK);
            alert.showAndWait();
            progressDialog.close();
            return;
        }
    }
    if (showPreview) {
        progressController.getStatusText().setText("Status: Getting Configuration...");
        String user = config.getUser();
        String pass = config.getPass();
        if (user == null || user.isEmpty() || pass == null || pass.isEmpty()) {
            FXMLLoader loader0 = new FXMLLoader();
            loader0.setLocation(AppMain.class.getResource("/view/LoginDialogLayout.fxml"));
            DialogPane loginPane = null;
            try {
                loginPane = (DialogPane) loader0.load();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            LoginDialogController loginController = loader0.getController();
            Alert loginDialog = new Alert(AlertType.NONE);
            loginDialog.initStyle(StageStyle.UTILITY);
            loginDialog.initModality(Modality.APPLICATION_MODAL);
            loginDialog.setTitle("");
            loginDialog.setDialogPane(loginPane);
            loginDialog.getButtonTypes().add(ButtonType.OK);
            loginDialog.getButtonTypes().add(ButtonType.CANCEL);
            loginDialog.showAndWait();
            String usr = loginController.getUser();
            String pwd = loginController.getPwd();
            if (usr == null || pwd == null || usr.isEmpty() || pwd.isEmpty()) {
                Alert alert = new Alert(AlertType.WARNING, "Username or password must not be empty.", ButtonType.OK);
                alert.showAndWait();
                progressDialog.close();
                return;
            }
            user = usr;
            pass = pwd;
        }
        final String _url = url;
        // OsuBeatmap map = null;
        final String _user = user;
        final String _pass = pass;
        final boolean _isBeatmap = isBeatmap;
        Thread thread = new Thread() {

            public void run() {
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        progressController.getStatusText().setText("Status: Logging in...");
                    }
                });
                try {
                    osums.login(_user, _pass);
                } catch (WithDumpException e) {
                    e.printStackTrace();
                    Platform.runLater(new Runnable() {

                        @Override
                        public void run() {
                            Alert alert = new Alert(AlertType.INFORMATION, "Error logging in:\n" + e.getDump().getStacktrace(), ButtonType.OK);
                            alert.showAndWait();
                            progressDialog.close();
                        }
                    });
                    return;
                }
                OsuSong map = null;
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        progressController.getStatusText().setText("Status: Obtaining song/beatmap information...");
                    }
                });
                try {
                    if (_isBeatmap) {
                        map = osums.getBeatmapInfo(_url);
                    } else {
                        map = osums.getSongInfo(_url);
                    }
                } catch (WithDumpException e) {
                    e.printStackTrace();
                    Platform.runLater(new Runnable() {

                        @Override
                        public void run() {
                            Alert alert = new Alert(AlertType.INFORMATION, "Error getting song/beatmap info:\n" + e.getDump().getStacktrace(), ButtonType.OK);
                            alert.showAndWait();
                            progressDialog.close();
                        }
                    });
                    return;
                }
                final OsuSong _map = map;
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        progressDialog.hide();
                        FXMLLoader loader = new FXMLLoader();
                        loader.setLocation(AppMain.class.getResource("/view/BeatmapPreviewDialogLayout.fxml"));
                        DialogPane dialogPane = null;
                        ;
                        try {
                            dialogPane = (DialogPane) loader.load();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        BeatmapPreviewDialogController controller = loader.getController();
                        controller.preview(_map);
                        Alert previewDialog = new Alert(AlertType.NONE);
                        previewDialog.initStyle(StageStyle.UTILITY);
                        previewDialog.initModality(Modality.APPLICATION_MODAL);
                        previewDialog.setTitle("Beatmap Preview");
                        previewDialog.setDialogPane(dialogPane);
                        List<ButtonType> btnTypes = previewDialog.getButtonTypes();
                        btnTypes.add(ButtonType.YES);
                        btnTypes.add(ButtonType.NO);
                        previewDialog.showAndWait();
                        ButtonType result = previewDialog.getResult();
                        if (result != ButtonType.YES) {
                            progressDialog.close();
                            return;
                        }
                        progressDialog.show();
                        uiRequestQueue(progressDialog, progressController, _url, changeTab);
                    }
                });
            }
        };
        thread.start();
    } else {
        uiRequestQueue(progressDialog, progressController, url, changeTab);
    }
}
Also used : WithDumpException(com.github.mob41.osumer.debug.WithDumpException) OsuSong(com.github.mob41.osums.beatmap.OsuSong) IOException(java.io.IOException) FXMLLoader(javafx.fxml.FXMLLoader) DialogPane(javafx.scene.control.DialogPane) Alert(javafx.scene.control.Alert) List(java.util.List) ArrayList(java.util.ArrayList) ButtonType(javafx.scene.control.ButtonType)

Example 3 with OsuBeatmap

use of com.github.mob41.osums.beatmap.OsuBeatmap in project osumer by mob41.

the class BeatmapPreviewDialogController method preview.

protected void preview(OsuSong map) {
    this.map = map;
    DecimalFormat df = new DecimalFormat("#.##");
    List<Node> songNodes = songBox.getChildren();
    songNodes.add(new Label("Name: " + map.getName()));
    songNodes.add(new Label("Artist: " + map.getArtist()));
    songNodes.add(new Label("Title: " + map.getTitle()));
    songNodes.add(new Label("BPM: " + map.getBpm()));
    songNodes.add(new Label("Creator: " + map.getCreator()));
    songNodes.add(new Label("Source: " + map.getSource()));
    songNodes.add(new Label("Genre: " + map.getGenre()));
    songNodes.add(new Label("Rating: " + df.format(map.getRating()) + "%"));
    songNodes.add(new Label("Good/Bad Votes: " + map.getGoodRating() + "/" + map.getBadRating()));
    if (map instanceof OsuBeatmap) {
        // TODO Future use
        OsuBeatmap bmap = (OsuBeatmap) map;
        List<Node> beatmapNodes = beatmapBox.getChildren();
        beatmapNodes.add(new Label("Circle Size: " + df.format(bmap.getCircleSize())));
        beatmapNodes.add(new Label("Approach Rate: " + df.format(bmap.getApproachRate())));
        beatmapNodes.add(new Label("Accuracy: " + df.format(bmap.getAccuracy())));
        beatmapNodes.add(new Label("HP Drain: " + df.format(bmap.getHpDrain())));
        beatmapNodes.add(new Label("Star Difficulty: " + df.format(bmap.getStarDifficulty())));
        beatmapNodes.add(new Label("Success Rate: " + df.format(bmap.getSuccessRate()) + "%"));
    } else {
        beatmapTab.setDisable(true);
    }
    image.setImage(new Image("http:" + map.getThumbUrl()));
}
Also used : OsuBeatmap(com.github.mob41.osums.beatmap.OsuBeatmap) DecimalFormat(java.text.DecimalFormat) Node(javafx.scene.Node) Label(javafx.scene.control.Label) Image(javafx.scene.image.Image)

Aggregations

WithDumpException (com.github.mob41.osumer.debug.WithDumpException)2 OsuBeatmap (com.github.mob41.osums.beatmap.OsuBeatmap)2 OsuSong (com.github.mob41.osums.beatmap.OsuSong)2 IOException (java.io.IOException)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FXMLLoader (javafx.fxml.FXMLLoader)1 Node (javafx.scene.Node)1 Alert (javafx.scene.control.Alert)1 ButtonType (javafx.scene.control.ButtonType)1 DialogPane (javafx.scene.control.DialogPane)1 Label (javafx.scene.control.Label)1 Image (javafx.scene.image.Image)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1