mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-29 14:06:30 +01:00
Now shortening the URL in /mvv using bit.ly!
This commit is contained in:
parent
08b8f01c44
commit
bdd5e8bd91
@ -9,10 +9,13 @@ package com.onarandombox.MultiverseCore.commands;
|
|||||||
|
|
||||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||||
import com.onarandombox.MultiverseCore.event.MVVersionEvent;
|
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.PasteFailedException;
|
||||||
import com.onarandombox.MultiverseCore.utils.webpaste.PasteService;
|
import com.onarandombox.MultiverseCore.utils.webpaste.PasteService;
|
||||||
import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceFactory;
|
import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceFactory;
|
||||||
import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceType;
|
import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceType;
|
||||||
|
import com.onarandombox.MultiverseCore.utils.webpaste.URLShortener;
|
||||||
|
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -25,6 +28,7 @@ import java.util.logging.Level;
|
|||||||
* Dumps version info to the console.
|
* Dumps version info to the console.
|
||||||
*/
|
*/
|
||||||
public class VersionCommand extends MultiverseCommand {
|
public class VersionCommand extends MultiverseCommand {
|
||||||
|
private static final URLShortener SHORTENER = new BitlyURLShortener();
|
||||||
|
|
||||||
public VersionCommand(MultiverseCore plugin) {
|
public VersionCommand(MultiverseCore plugin) {
|
||||||
super(plugin);
|
super(plugin);
|
||||||
@ -39,7 +43,7 @@ public class VersionCommand extends MultiverseCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.
|
// Check if the command was sent from a Player.
|
||||||
if (sender instanceof Player) {
|
if (sender instanceof Player) {
|
||||||
sender.sendMessage("Version info dumped to console. Please check your server logs.");
|
sender.sendMessage("Version info dumped to console. Please check your server logs.");
|
||||||
@ -69,14 +73,17 @@ public class VersionCommand extends MultiverseCommand {
|
|||||||
this.plugin.getServer().getPluginManager().callEvent(versionEvent);
|
this.plugin.getServer().getPluginManager().callEvent(versionEvent);
|
||||||
|
|
||||||
// log to console
|
// log to console
|
||||||
String data = versionEvent.getVersionInfo();
|
final String data = versionEvent.getVersionInfo();
|
||||||
String[] lines = data.split("\n");
|
String[] lines = data.split("\n");
|
||||||
for (String line : lines) {
|
for (String line : lines) {
|
||||||
this.plugin.log(Level.INFO, line);
|
this.plugin.log(Level.INFO, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.plugin.getServer().getScheduler().scheduleAsyncDelayedTask(this.plugin, new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
if (args.size() == 1) {
|
if (args.size() == 1) {
|
||||||
String pasteUrl = "";
|
String pasteUrl;
|
||||||
if (args.get(0).equalsIgnoreCase("-p")) {
|
if (args.get(0).equalsIgnoreCase("-p")) {
|
||||||
pasteUrl = postToService(PasteServiceType.PASTIE, true, data); // private post to pastie
|
pasteUrl = postToService(PasteServiceType.PASTIE, true, data); // private post to pastie
|
||||||
} else if (args.get(0).equalsIgnoreCase("-b")) {
|
} else if (args.get(0).equalsIgnoreCase("-b")) {
|
||||||
@ -86,9 +93,11 @@ public class VersionCommand extends MultiverseCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteUrl);
|
sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteUrl);
|
||||||
this.plugin.log(Level.INFO, "Version info dumped here: " + pasteUrl);
|
plugin.log(Level.INFO, "Version info dumped here: " + pasteUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send the current contents of this.pasteBinBuffer to a web service.
|
* Send the current contents of this.pasteBinBuffer to a web service.
|
||||||
@ -100,7 +109,7 @@ public class VersionCommand extends MultiverseCommand {
|
|||||||
private static String postToService(PasteServiceType type, boolean isPrivate, String pasteData) {
|
private static String postToService(PasteServiceType type, boolean isPrivate, String pasteData) {
|
||||||
PasteService ps = PasteServiceFactory.getService(type, isPrivate);
|
PasteService ps = PasteServiceFactory.getService(type, isPrivate);
|
||||||
try {
|
try {
|
||||||
return ps.postData(ps.encodeData(pasteData), ps.getPostURL());
|
return SHORTENER.shorten(ps.postData(ps.encodeData(pasteData), ps.getPostURL()));
|
||||||
} catch (PasteFailedException e) {
|
} catch (PasteFailedException e) {
|
||||||
System.out.print(e);
|
System.out.print(e);
|
||||||
return "Error posting to service";
|
return "Error posting to service";
|
||||||
|
@ -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 ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user