/* * opsu! - an open-source osu! client * Copyright (C) 2014, 2015 Jeffrey Han * * opsu! is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * opsu! is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with opsu!. If not, see . */ package itdelatrisu.opsu.downloads.servers; import itdelatrisu.opsu.ErrorHandler; import itdelatrisu.opsu.Utils; import itdelatrisu.opsu.downloads.DownloadNode; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Download server: http://osu.uu.gl/ */ public class MnetworkServer extends DownloadServer { /** Server name. */ private static final String SERVER_NAME = "Mnetwork"; /** Formatted download URL: {@code beatmapSetID} */ private static final String DOWNLOAD_URL = "http://osu.uu.gl/s/%d"; /** Formatted search URL: {@code query} */ private static final String SEARCH_URL = "http://osu.uu.gl/d/%s"; /** Total result count from the last query. */ private int totalResults = -1; /** Beatmap pattern. */ private Pattern BEATMAP_PATTERN = Pattern.compile("^(\\d+) ([^-]+) - (.+)\\.osz$"); /** Constructor. */ public MnetworkServer() {} @Override public String getName() { return SERVER_NAME; } @Override public String getDownloadURL(int beatmapSetID) { return String.format(DOWNLOAD_URL, beatmapSetID); } @Override public DownloadNode[] resultList(String query, int page, boolean rankedOnly) throws IOException { DownloadNode[] nodes = null; try { // read HTML String queryString = (query.isEmpty()) ? "-" : query; String search = String.format(SEARCH_URL, URLEncoder.encode(queryString, "UTF-8")); String html = Utils.readDataFromUrl(new URL(search)); if (html == null) { this.totalResults = -1; return null; } // parse results // NOTE: Not using a full HTML parser because this is a relatively simple operation. // FORMAT: //
// {{id}} {{artist}} - {{title}}.osz
// BPM: {{bpm}} | Total Time: {{m}}:{{s}}
// Genre: {{genre}} | Updated: {{MMM}} {{d}}, {{yyyy}}
List nodeList = new ArrayList(); final String START_TAG = "