Updated BloodcatServer to use the new API. (fixes #139, 140)

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-09-30 00:28:21 -04:00
parent ab817172e4
commit 9c7dbce6fd

View File

@ -46,7 +46,10 @@ public class BloodcatServer extends DownloadServer {
private static final String DOWNLOAD_URL = "http://bloodcat.com/osu/s/%d"; private static final String DOWNLOAD_URL = "http://bloodcat.com/osu/s/%d";
/** Formatted search URL: {@code query,rankedOnly,page} */ /** Formatted search URL: {@code query,rankedOnly,page} */
private static final String SEARCH_URL = "http://bloodcat.com/osu/?q=%s&m=b&c=%s&g=&d=0&s=date&o=0&p=%d&mod=json"; private static final String SEARCH_URL = "http://bloodcat.com/osu/?q=%s&c=b&s=%s&m=0&p=%d&mod=json";//"?q=%s&m=b&c=%s&g=&d=0&s=date&o=0&p=%d&mod=json";
/** Maximum beatmaps displayed per page. */
private static final int PAGE_LIMIT = 40;
/** Total result count from the last query. */ /** Total result count from the last query. */
private int totalResults = -1; private int totalResults = -1;
@ -67,28 +70,32 @@ public class BloodcatServer extends DownloadServer {
DownloadNode[] nodes = null; DownloadNode[] nodes = null;
try { try {
// read JSON // read JSON
String search = String.format(SEARCH_URL, URLEncoder.encode(query, "UTF-8"), rankedOnly ? "0" : "", page); String search = String.format(SEARCH_URL, URLEncoder.encode(query, "UTF-8"), rankedOnly ? "1" : "", page);
JSONObject json = Utils.readJsonObjectFromUrl(new URL(search)); JSONArray arr = Utils.readJsonArrayFromUrl(new URL(search));
if (json == null) { if (arr == null) {
this.totalResults = -1; this.totalResults = -1;
return null; return null;
} }
// parse result list // parse result list
JSONArray arr = json.getJSONArray("results"); //JSONArray arr = json.getJSONArray("results");
nodes = new DownloadNode[arr.length()]; nodes = new DownloadNode[arr.length()];
for (int i = 0; i < nodes.length; i++) { for (int i = 0; i < nodes.length; i++) {
JSONObject item = arr.getJSONObject(i); JSONObject item = arr.getJSONObject(i);
nodes[i] = new DownloadNode( nodes[i] = new DownloadNode(
item.getInt("id"), formatDate(item.getString("date")), item.getInt("id"), formatDate(item.getString("synced")), //"date"
item.getString("title"), item.isNull("titleUnicode") ? null : item.getString("titleUnicode"), item.getString("title"), item.isNull("titleU") ? null : item.getString("titleU"), //"titleUnicode"
item.getString("artist"), item.isNull("artistUnicode") ? null : item.getString("artistUnicode"), item.getString("artist"), item.isNull("artistU") ? null : item.getString("artistU"), //"artistUnicode"
item.getString("creator") item.getString("creator")
); );
} }
// store total result count // store total result count
this.totalResults = json.getInt("resultCount"); //this.totalResults = arr.getInt("resultCount");
int resultCount = nodes.length + (page - 1) * PAGE_LIMIT;
if (nodes.length == PAGE_LIMIT)
resultCount++;
this.totalResults = resultCount;
} catch (MalformedURLException | UnsupportedEncodingException e) { } catch (MalformedURLException | UnsupportedEncodingException e) {
ErrorHandler.error(String.format("Problem loading result list for query '%s'.", query), e, true); ErrorHandler.error(String.format("Problem loading result list for query '%s'.", query), e, true);
} }
@ -103,19 +110,20 @@ public class BloodcatServer extends DownloadServer {
/** /**
* Returns a formatted date string from a raw date. * Returns a formatted date string from a raw date.
* @param s the raw date string (e.g. "2015-05-14T23:38:47+09:00") * @param s the raw date string (e.g. "2015-09-30 09:39:04.536")
* @return the formatted date, or the raw string if it could not be parsed * @return the formatted date, or the raw string if it could not be parsed
*/ */
private String formatDate(String s) { private String formatDate(String s) {
try { try {
// old format: "2015-05-14T23:38:47+09:00"
// make string parseable by SimpleDateFormat // make string parseable by SimpleDateFormat
int index = s.lastIndexOf(':'); // int index = s.lastIndexOf(':');
if (index == -1) // if (index == -1)
return s; // return s;
String str = new StringBuilder(s).deleteCharAt(index).toString(); // s = new StringBuilder(s).deleteCharAt(index).toString();
DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //"yyyy-MM-dd'T'HH:mm:ssZ"
Date d = f.parse(str); Date d = f.parse(s);
DateFormat fmt = new SimpleDateFormat("d MMM yyyy HH:mm:ss"); DateFormat fmt = new SimpleDateFormat("d MMM yyyy HH:mm:ss");
return fmt.format(d); return fmt.format(d);
} catch (StringIndexOutOfBoundsException | ParseException e) { } catch (StringIndexOutOfBoundsException | ParseException e) {