Using JSON to gather the servers

This commit is contained in:
JKetelaar
2014-07-22 23:49:58 +02:00
parent 682490dd13
commit 1dea72621e
2 changed files with 22 additions and 34 deletions
@@ -11,6 +11,7 @@ public class Configuration {
public static final String SDN_SCRIPTS_JSON = "http://sdn.parabot.org/scripts.php?method=json&user=%s"; public static final String SDN_SCRIPTS_JSON = "http://sdn.parabot.org/scripts.php?method=json&user=%s";
public static final String GET_SDN_SCRIPT = "http://sdn.parabot.org/getscript.php?user=%s&pass=%s&scriptid=%d"; public static final String GET_SDN_SCRIPT = "http://sdn.parabot.org/getscript.php?user=%s&pass=%s&scriptid=%d";
public static final String GET_SERVER_PROVIDERS = "http://sdn.parabot.org/providers/index.php"; public static final String GET_SERVER_PROVIDERS = "http://sdn.parabot.org/providers/index.php";
public static final String GET_SERVER_PROVIDERS_JSON = "http://sdn.parabot.org/providers/index.php?method=json";
public static final String GET_SERVER_PROVIDER = "http://sdn.parabot.org/providers/provider.php?id="; public static final String GET_SERVER_PROVIDER = "http://sdn.parabot.org/providers/provider.php?id=";
public static final String GET_SERVER_PROVIDER_INFO = "http://sdn.parabot.org/providers/getInformation.php?id="; public static final String GET_SERVER_PROVIDER_INFO = "http://sdn.parabot.org/providers/getInformation.php?id=";
public static final String GET_BOT_VERSION = "http://bot.parabot.org/version.txt"; public static final String GET_BOT_VERSION = "http://bot.parabot.org/version.txt";
@@ -1,5 +1,7 @@
package org.parabot.core.parsers.servers; package org.parabot.core.parsers.servers;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.parabot.core.Configuration; import org.parabot.core.Configuration;
import org.parabot.core.desc.ServerDescription; import org.parabot.core.desc.ServerDescription;
import org.parabot.core.forum.AccountManager; import org.parabot.core.forum.AccountManager;
@@ -17,50 +19,35 @@ import java.net.URL;
*/ */
public class PublicServers extends ServerParser { public class PublicServers extends ServerParser {
private static AccountManager manager; private static AccountManager manager;
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() { public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
@Override @Override
public final void setManager(AccountManager manager) { public final void setManager(AccountManager manager) {
PublicServers.manager = manager; PublicServers.manager = manager;
} }
}; };
@Override @Override
public void execute() { public void execute() {
try { try {
BufferedReader br = WebUtil.getReader(new URL( BufferedReader br = WebUtil.getReader(new URL(
Configuration.GET_SERVER_PROVIDERS), manager.getAccount().getUsername(), manager.getAccount().getPassword()); Configuration.GET_SERVER_PROVIDERS_JSON), manager.getAccount().getUsername(), manager.getAccount().getPassword());
int count = 0;
String line; String line;
String name = null; JSONParser parser = new JSONParser();
String author = null;
double version = 0D;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
count++;
switch (count % 4) { JSONObject jsonObject = (JSONObject) parser.parse(line);
case 1: String name = String.valueOf(jsonObject.get("name"));
// server name String author = String.valueOf(jsonObject.get("author"));
name = line; double version = Double.parseDouble(String.valueOf(jsonObject.get("version")));
break;
case 2: ServerDescription desc = new ServerDescription(name,
// author author, version);
author = line; SERVER_CACHE.put(desc, new PublicServerExecuter(name, line));
break;
case 3:
// version
version = Double.parseDouble(line);
break;
case 0:
// serverID
ServerDescription desc = new ServerDescription(name,
author, version);
SERVER_CACHE.put(desc, new PublicServerExecuter(name, line));
}
} }
br.close(); br.close();