Added osu!Mirror download server.
Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
@@ -26,6 +26,7 @@ import itdelatrisu.opsu.UI;
|
||||
import itdelatrisu.opsu.Utils;
|
||||
import itdelatrisu.opsu.downloads.Download.DownloadListener;
|
||||
import itdelatrisu.opsu.downloads.Download.Status;
|
||||
import itdelatrisu.opsu.downloads.servers.DownloadServer;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -239,22 +240,26 @@ public class DownloadNode {
|
||||
* @see #getDownload()
|
||||
*/
|
||||
public void createDownload(DownloadServer server) {
|
||||
if (download == null) {
|
||||
String path = String.format("%s%c%d", Options.getOSZDir(), File.separatorChar, beatmapSetID);
|
||||
String rename = String.format("%d %s - %s.osz", beatmapSetID, artist, title);
|
||||
this.download = new Download(server.getURL(beatmapSetID), path, rename);
|
||||
download.setListener(new DownloadListener() {
|
||||
@Override
|
||||
public void completed() {
|
||||
UI.sendBarNotification(String.format("Download complete: %s", getTitle()));
|
||||
}
|
||||
if (download != null)
|
||||
return;
|
||||
|
||||
@Override
|
||||
public void error() {
|
||||
UI.sendBarNotification("Download failed due to a connection error.");
|
||||
}
|
||||
});
|
||||
}
|
||||
String url = server.getDownloadURL(beatmapSetID);
|
||||
if (url == null)
|
||||
return;
|
||||
String path = String.format("%s%c%d", Options.getOSZDir(), File.separatorChar, beatmapSetID);
|
||||
String rename = String.format("%d %s - %s.osz", beatmapSetID, artist, title);
|
||||
this.download = new Download(url, path, rename);
|
||||
download.setListener(new DownloadListener() {
|
||||
@Override
|
||||
public void completed() {
|
||||
UI.sendBarNotification(String.format("Download complete: %s", getTitle()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error() {
|
||||
UI.sendBarNotification("Download failed due to a connection error.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,10 +16,11 @@
|
||||
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package itdelatrisu.opsu.downloads;
|
||||
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;
|
||||
@@ -28,13 +29,15 @@ import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Download server: http://bloodcat.com/osu/
|
||||
*/
|
||||
public class BloodcatServer extends DownloadServer {
|
||||
/** Server name. */
|
||||
private static final String SERVER_NAME = "Bloodcat";
|
||||
|
||||
/** Formatted download URL: {@code beatmapSetID} */
|
||||
private static final String DOWNLOAD_URL = "http://bloodcat.com/osu/s/%d";
|
||||
|
||||
@@ -48,7 +51,10 @@ public class BloodcatServer extends DownloadServer {
|
||||
public BloodcatServer() {}
|
||||
|
||||
@Override
|
||||
public String getURL(int beatmapSetID) {
|
||||
public String getName() { return SERVER_NAME; }
|
||||
|
||||
@Override
|
||||
public String getDownloadURL(int beatmapSetID) {
|
||||
return String.format(DOWNLOAD_URL, beatmapSetID);
|
||||
}
|
||||
|
||||
@@ -58,7 +64,7 @@ public class BloodcatServer extends DownloadServer {
|
||||
try {
|
||||
// read JSON
|
||||
String search = String.format(SEARCH_URL, URLEncoder.encode(query, "UTF-8"), rankedOnly ? "0" : "", page);
|
||||
JSONObject json = readJsonFromUrl(new URL(search));
|
||||
JSONObject json = Utils.readJsonFromUrl(new URL(search));
|
||||
if (json == null) {
|
||||
this.totalResults = -1;
|
||||
return null;
|
||||
@@ -86,24 +92,8 @@ public class BloodcatServer extends DownloadServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int totalResults() { return totalResults; }
|
||||
public int minQueryLength() { return 0; }
|
||||
|
||||
/**
|
||||
* Returns a JSON object from a URL.
|
||||
* @param url the remote URL
|
||||
* @return the JSON object
|
||||
* @author Roland Illig (http://stackoverflow.com/a/4308662)
|
||||
*/
|
||||
private static JSONObject readJsonFromUrl(URL url) throws IOException {
|
||||
String s = Utils.readDataFromUrl(url);
|
||||
JSONObject json = null;
|
||||
if (s != null) {
|
||||
try {
|
||||
json = new JSONObject(s);
|
||||
} catch (JSONException e) {
|
||||
ErrorHandler.error("Failed to create JSON object.", e, true);
|
||||
}
|
||||
}
|
||||
return json;
|
||||
}
|
||||
@Override
|
||||
public int totalResults() { return totalResults; }
|
||||
}
|
||||
@@ -16,7 +16,9 @@
|
||||
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package itdelatrisu.opsu.downloads;
|
||||
package itdelatrisu.opsu.downloads.servers;
|
||||
|
||||
import itdelatrisu.opsu.downloads.DownloadNode;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -27,12 +29,18 @@ public abstract class DownloadServer {
|
||||
/** Track preview URL. */
|
||||
private static final String PREVIEW_URL = "http://b.ppy.sh/preview/%d.mp3";
|
||||
|
||||
/**
|
||||
* Returns the name of the download server.
|
||||
* @return the server name
|
||||
*/
|
||||
public abstract String getName();
|
||||
|
||||
/**
|
||||
* Returns a web address to download the given beatmap.
|
||||
* @param beatmapSetID the beatmap set ID
|
||||
* @return the URL string
|
||||
* @return the URL string, or null if the address could not be determined
|
||||
*/
|
||||
public abstract String getURL(int beatmapSetID);
|
||||
public abstract String getDownloadURL(int beatmapSetID);
|
||||
|
||||
/**
|
||||
* Returns a list of results for a given search query, or null if the
|
||||
@@ -45,6 +53,12 @@ public abstract class DownloadServer {
|
||||
*/
|
||||
public abstract DownloadNode[] resultList(String query, int page, boolean rankedOnly) throws IOException;
|
||||
|
||||
/**
|
||||
* Returns the minimum allowable length of a search query.
|
||||
* @return the minimum length, or 0 if none
|
||||
*/
|
||||
public abstract int minQueryLength();
|
||||
|
||||
/**
|
||||
* Returns the total number of results for the last search query.
|
||||
* This will differ from the the size of the array returned by
|
||||
129
src/itdelatrisu/opsu/downloads/servers/OsuMirrorServer.java
Normal file
129
src/itdelatrisu/opsu/downloads/servers/OsuMirrorServer.java
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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.HashMap;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Download server: http://loli.al/
|
||||
*/
|
||||
public class OsuMirrorServer extends DownloadServer {
|
||||
/** Server name. */
|
||||
private static final String SERVER_NAME = "osu!Mirror";
|
||||
|
||||
/** Formatted download URL: {@code beatmapSetID} */
|
||||
private static final String DOWNLOAD_URL = "http://loli.al/d/%d/";
|
||||
|
||||
/** Formatted search URL: {@code page,query} */
|
||||
private static final String SEARCH_URL = "http://loli.al/mirror/search/%d.json?keyword=%s";
|
||||
|
||||
/** Formatted home URL: {@code page} */
|
||||
private static final String HOME_URL = "http://loli.al/mirror/home/%d.json";
|
||||
|
||||
/** Minimum allowable length of a search query. */
|
||||
private static final int MIN_QUERY_LENGTH = 3;
|
||||
|
||||
/** Total result count from the last query. */
|
||||
private int totalResults = -1;
|
||||
|
||||
/** Max server download ID seen (for approximating total pages). */
|
||||
private int maxServerID = 0;
|
||||
|
||||
/** Lookup table from beatmap set ID -> server download ID. */
|
||||
private HashMap<Integer, Integer> idTable = new HashMap<Integer, Integer>();
|
||||
|
||||
/** Constructor. */
|
||||
public OsuMirrorServer() {}
|
||||
|
||||
@Override
|
||||
public String getName() { return SERVER_NAME; }
|
||||
|
||||
@Override
|
||||
public String getDownloadURL(int beatmapSetID) {
|
||||
return (idTable.containsKey(beatmapSetID)) ? String.format(DOWNLOAD_URL, idTable.get(beatmapSetID)) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DownloadNode[] resultList(String query, int page, boolean rankedOnly) throws IOException {
|
||||
// NOTE: ignores 'rankedOnly' flag.
|
||||
DownloadNode[] nodes = null;
|
||||
try {
|
||||
// read JSON
|
||||
String search;
|
||||
boolean isSearch;
|
||||
if (query.isEmpty()) {
|
||||
isSearch = false;
|
||||
search = String.format(HOME_URL, page);
|
||||
} else {
|
||||
isSearch = true;
|
||||
search = String.format(SEARCH_URL, page, URLEncoder.encode(query, "UTF-8"));
|
||||
}
|
||||
JSONObject json = Utils.readJsonFromUrl(new URL(search));
|
||||
if (json == null || json.getInt("code") != 0) {
|
||||
this.totalResults = -1;
|
||||
return null;
|
||||
}
|
||||
|
||||
// parse result list
|
||||
JSONArray arr = json.getJSONArray("maplist");
|
||||
nodes = new DownloadNode[arr.length()];
|
||||
for (int i = 0; i < nodes.length; i++) {
|
||||
JSONObject item = arr.getJSONObject(i);
|
||||
int beatmapSetID = item.getInt("OSUSetid");
|
||||
int serverID = item.getInt("id");
|
||||
nodes[i] = new DownloadNode(
|
||||
beatmapSetID, item.getString("ModifyDate"),
|
||||
item.getString("Title"), null,
|
||||
item.getString("Artist"), null,
|
||||
item.getString("Mapper")
|
||||
);
|
||||
idTable.put(beatmapSetID, serverID);
|
||||
if (serverID > maxServerID)
|
||||
maxServerID = serverID;
|
||||
}
|
||||
|
||||
// store total result count
|
||||
if (isSearch)
|
||||
this.totalResults = json.getInt("totalRows");
|
||||
else
|
||||
this.totalResults = maxServerID;
|
||||
} catch (MalformedURLException | UnsupportedEncodingException e) {
|
||||
ErrorHandler.error(String.format("Problem loading result list for query '%s'.", query), e, true);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int minQueryLength() { return MIN_QUERY_LENGTH; }
|
||||
|
||||
@Override
|
||||
public int totalResults() { return totalResults; }
|
||||
}
|
||||
Reference in New Issue
Block a user