Now shortening the URL in /mvv using bit.ly!

This commit is contained in:
main() 2012-03-08 17:59:25 +01:00
parent 08b8f01c44
commit bdd5e8bd91
4 changed files with 112 additions and 15 deletions

View File

@ -9,10 +9,13 @@ package com.onarandombox.MultiverseCore.commands;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.event.MVVersionEvent;
import com.onarandombox.MultiverseCore.utils.webpaste.BitlyURLShortener;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteFailedException;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteService;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceFactory;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceType;
import com.onarandombox.MultiverseCore.utils.webpaste.URLShortener;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -25,6 +28,7 @@ import java.util.logging.Level;
* Dumps version info to the console.
*/
public class VersionCommand extends MultiverseCommand {
private static final URLShortener SHORTENER = new BitlyURLShortener();
public VersionCommand(MultiverseCore plugin) {
super(plugin);
@ -39,7 +43,7 @@ public class VersionCommand extends MultiverseCommand {
}
@Override
public void runCommand(CommandSender sender, List<String> args) {
public void runCommand(final CommandSender sender, final List<String> args) {
// Check if the command was sent from a Player.
if (sender instanceof Player) {
sender.sendMessage("Version info dumped to console. Please check your server logs.");
@ -69,25 +73,30 @@ public class VersionCommand extends MultiverseCommand {
this.plugin.getServer().getPluginManager().callEvent(versionEvent);
// log to console
String data = versionEvent.getVersionInfo();
final String data = versionEvent.getVersionInfo();
String[] lines = data.split("\n");
for (String line : lines) {
this.plugin.log(Level.INFO, line);
}
if (args.size() == 1) {
String pasteUrl = "";
if (args.get(0).equalsIgnoreCase("-p")) {
pasteUrl = postToService(PasteServiceType.PASTIE, true, data); // private post to pastie
} else if (args.get(0).equalsIgnoreCase("-b")) {
pasteUrl = postToService(PasteServiceType.PASTEBIN, true, data); // private post to pastie
} else {
return;
}
this.plugin.getServer().getScheduler().scheduleAsyncDelayedTask(this.plugin, new Runnable() {
@Override
public void run() {
if (args.size() == 1) {
String pasteUrl;
if (args.get(0).equalsIgnoreCase("-p")) {
pasteUrl = postToService(PasteServiceType.PASTIE, true, data); // private post to pastie
} else if (args.get(0).equalsIgnoreCase("-b")) {
pasteUrl = postToService(PasteServiceType.PASTEBIN, true, data); // private post to pastie
} else {
return;
}
sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteUrl);
this.plugin.log(Level.INFO, "Version info dumped here: " + pasteUrl);
}
sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteUrl);
plugin.log(Level.INFO, "Version info dumped here: " + pasteUrl);
}
}
});
}
/**
@ -100,7 +109,7 @@ public class VersionCommand extends MultiverseCommand {
private static String postToService(PasteServiceType type, boolean isPrivate, String pasteData) {
PasteService ps = PasteServiceFactory.getService(type, isPrivate);
try {
return ps.postData(ps.encodeData(pasteData), ps.getPostURL());
return SHORTENER.shorten(ps.postData(ps.encodeData(pasteData), ps.getPostURL()));
} catch (PasteFailedException e) {
System.out.print(e);
return "Error posting to service";

View File

@ -0,0 +1,34 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
import java.io.IOException;
/**
* An {@link URLShortener} using {@code bit.ly}.
*/
public class BitlyURLShortener extends HttpAPIClient implements URLShortener {
private static final String GENERIC_BITLY_REQUEST_FORMAT = "https://api-ssl.bitly.com/v3/shorten?format=txt&apiKey=%s&login=%s&longUrl=%s";
// I think it's no problem that these are public
private static final String USERNAME = "multiverse2";
private static final String API_KEY = "R_9dbff4862a3bc0c4218a7d78cc10d0e0";
public BitlyURLShortener() {
super(String.format(GENERIC_BITLY_REQUEST_FORMAT, API_KEY, USERNAME, "%s"));
}
/**
* {@inheritDoc}
*/
@Override
public String shorten(String longUrl) {
try {
String result = this.exec(longUrl);
if (!result.startsWith("http://j.mp/")) // ... then it's failed :/
throw new IOException(result);
return result;
} catch (IOException e) {
e.printStackTrace();
return longUrl; // sorry ...
}
}
}

View File

@ -0,0 +1,41 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
/**
* HTTP API-client.
*/
public abstract class HttpAPIClient {
/**
* The URL for this API-request.
*/
protected final String urlFormat;
public HttpAPIClient(String urlFormat) {
this.urlFormat = urlFormat;
}
/**
* Executes this API-Request.
* @param args Format-args.
* @return The result (as text).
* @throws IOException When the I/O-operation failed.
*/
protected final String exec(Object... args) throws IOException {
URLConnection conn = new URL(String.format(this.urlFormat, args)).openConnection();
conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while (!reader.ready()); // wait until reader is ready, may not be necessary, SUPPRESS CHECKSTYLE: EmptyStatement
StringBuilder ret = new StringBuilder();
while (reader.ready()) {
ret.append(reader.readLine()).append('\n');
}
reader.close();
return ret.toString();
}
}

View File

@ -0,0 +1,13 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
/**
* URL-Shortener.
*/
public interface URLShortener {
/**
* Shorten an URL.
* @param longUrl The long form.
* @return The shortened URL.
*/
String shorten(String longUrl);
}