Skip to content

Commit

Permalink
Download speed smoothing; add referer to fix MengSky download issues.
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
  • Loading branch information
itdelatrisu committed Mar 17, 2017
1 parent 8dd92de commit 06370bd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/itdelatrisu/opsu/downloads/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Map;

import org.newdawn.slick.util.Log;

Expand Down Expand Up @@ -98,6 +99,9 @@ public interface DownloadListener {
/** The download listener. */
private DownloadListener listener;

/** The additional HTTP request headers. */
private Map<String, String> requestHeaders;

/** The readable byte channel. */
private ReadableByteChannelWrapper rbc;

Expand All @@ -122,6 +126,12 @@ public interface DownloadListener {
/** Last calculated ETA string. */
private String lastTimeRemaining;

/** EWMA download speed. */
private long avgDownloadSpeed = 0;

/** EWMA smoothing factor (alpha) for computing average download speed. */
private static final double DOWNLOAD_SPEED_SMOOTHING = 0.25;

/**
* Constructor.
* @param remoteURL the download URL
Expand Down Expand Up @@ -165,6 +175,12 @@ public Download(String remoteURL, String localPath, String rename) {
*/
public void setListener(DownloadListener listener) { this.listener = listener; }

/**
* Sets additional HTTP headers to use in the download request.
* @param headers the map of headers (key -> value)
*/
public void setRequestHeaders(Map<String, String> headers) { this.requestHeaders = headers; }

/**
* Starts the download from the "waiting" status.
* @return the started download thread, or {@code null} if none started
Expand Down Expand Up @@ -194,6 +210,10 @@ public void run() {
// http://download.java.net/jdk7u2/docs/technotes/guides/deployment/deployment-guide/upgrade-guide/article-17.html
conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("User-Agent", "Mozilla/5.0...");
if (requestHeaders != null) {
for (Map.Entry<String, String> entry : requestHeaders.entrySet())
conn.setRequestProperty(entry.getKey(), entry.getValue());
}

// check for redirect
int status = conn.getResponseCode();
Expand Down Expand Up @@ -369,6 +389,7 @@ private void updateReadSoFar() {
if (status != Status.DOWNLOADING) {
this.lastDownloadSpeed = null;
this.lastTimeRemaining = null;
this.avgDownloadSpeed = 0;
return;
}

Expand All @@ -378,13 +399,16 @@ private void updateReadSoFar() {
long readSoFarTime = System.currentTimeMillis();
long dlspeed = (readSoFar - lastReadSoFar) * 1000 / (readSoFarTime - lastReadSoFarTime);
if (dlspeed > 0) {
this.lastDownloadSpeed = String.format("%s/s", Utils.bytesToString(dlspeed));
long t = (contentLength - readSoFar) / dlspeed;
this.avgDownloadSpeed = (avgDownloadSpeed == 0) ? dlspeed :
(long) (DOWNLOAD_SPEED_SMOOTHING * dlspeed + (1 - DOWNLOAD_SPEED_SMOOTHING) * avgDownloadSpeed);
this.lastDownloadSpeed = String.format("%s/s", Utils.bytesToString(avgDownloadSpeed));
long t = (contentLength - readSoFar) / avgDownloadSpeed;
if (t >= 3600)
this.lastTimeRemaining = String.format("%dh%dm%ds", t / 3600, (t / 60) % 60, t % 60);
else
this.lastTimeRemaining = String.format("%dm%ds", t / 60, t % 60);
} else {
this.avgDownloadSpeed = 0;
this.lastDownloadSpeed = String.format("%s/s", Utils.bytesToString(0));
this.lastTimeRemaining = "?";
}
Expand Down
1 change: 1 addition & 0 deletions src/itdelatrisu/opsu/downloads/DownloadNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ public void error() {
UI.getNotificationManager().sendNotification("Download failed due to a connection error.", Color.red);
}
});
download.setRequestHeaders(server.getDownloadRequestHeaders());
this.download = download;
if (Options.useUnicodeMetadata()) // load glyphs
Fonts.loadGlyphs(Fonts.LARGE, getTitle());
Expand Down
7 changes: 7 additions & 0 deletions src/itdelatrisu/opsu/downloads/servers/DownloadServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import itdelatrisu.opsu.downloads.DownloadNode;

import java.io.IOException;
import java.util.Map;

/**
* Abstract class for beatmap download servers.
Expand Down Expand Up @@ -76,6 +77,12 @@ public String getPreviewURL(int beatmapSetID) {
return String.format(PREVIEW_URL, beatmapSetID);
}

/**
* Returns any HTTP request headers that should be set in the download request.
* @return the map of headers (key -> value), or null if none
*/
public Map<String, String> getDownloadRequestHeaders() { return null; }

@Override
public String toString() { return getName(); }
}
12 changes: 12 additions & 0 deletions src/itdelatrisu/opsu/downloads/servers/MengSkyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -46,6 +48,9 @@ public class MengSkyServer extends DownloadServer {
/** Formatted search URL: {@code query,page,unranked,approved,qualified} */
private static final String SEARCH_URL = "http://osu.mengsky.net/api/beatmapinfo?query=%s&page=%d&ranked=1&unrank=%d&approved=%d&qualified=%d";

/** Referer URL. */
private static final String REFERER_URL = "http://osu.mengsky.net/";

/** Maximum beatmaps displayed per page. */
private static final int PAGE_LIMIT = 20;

Expand Down Expand Up @@ -119,4 +124,11 @@ public DownloadNode[] resultList(String query, int page, boolean rankedOnly) thr

@Override
public int totalResults() { return totalResults; }

@Override
public Map<String, String> getDownloadRequestHeaders() {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Referer", REFERER_URL);
return headers;
}
}

0 comments on commit 06370bd

Please sign in to comment.