Last active
August 29, 2015 14:04
-
-
Save FarisR99/4c7ae5fe32d83f0fabc3 to your computer and use it in GitHub Desktop.
MCSG Bot - Plugin - CurrencyConverter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.skype.Chat; | |
import com.skype.User; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
import org.mcsg.bot.skype.Bot; | |
import org.mcsg.bot.skype.ChatManager; | |
import org.mcsg.bot.skype.McsgBotPlugin; | |
import org.mcsg.bot.skype.commands.SubCommand; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLConnection; | |
public class Currency implements McsgBotPlugin { | |
@Override | |
public String getName() { | |
return "CurrencyConverter"; | |
} | |
@Override | |
public void onEnable(Chat chat) throws Exception { | |
Bot.registerCommand(new CurrencyCommand(), this); | |
} | |
@Override | |
public void onDisable() throws Exception { | |
} | |
public static boolean isDouble(String aString) { | |
try { | |
Double.parseDouble(aString); | |
return true; | |
} catch (Exception ex) { | |
return false; | |
} | |
} | |
public static class CurrencyCommand implements SubCommand { | |
private static final String failedFormat = "Error: Could not convert %f from %s to %s due to %s"; | |
@Override | |
public void execute(Chat chat, User sender, String[] args) throws Exception { | |
if (args.length == 2 || args.length == 3) { | |
double money = 1D; | |
if (args.length == 3 && isDouble(args[2])) money = Double.parseDouble(args[2]); | |
String strFrom = args[0].toUpperCase(); | |
String strTo = args[1].toUpperCase(); | |
CurrencyResult conversionResult = CurrencyConverter.convertCurrency(strFrom, strTo, money); | |
if (!conversionResult.hasFailed()) { | |
String strResult = ""; | |
if (conversionResult.getInitial() % 1 == 0) | |
strResult = String.valueOf((int) conversionResult.getInitial()); | |
else strResult = String.valueOf(conversionResult.getInitial()); | |
ChatManager.chat(chat, "Converted " + strResult + conversionResult.getFrom() + " to " + conversionResult.getTo() + ": " + String.format("%.2f", conversionResult.getResult())); | |
if (money != 1D) | |
ChatManager.chat(chat, "Conversion rate: " + conversionResult.getRate() + " " + conversionResult.getTo() + "/" + conversionResult.getFrom()); | |
} else { | |
ConvertFailedReason failedReason = conversionResult.getError(); | |
if (failedReason == ConvertFailedReason.UNKNOWN) | |
ChatManager.chat(chat, String.format(failedFormat, money, strFrom, strTo, "an unknown reason.")); | |
else if (failedReason == ConvertFailedReason.INVALID_AMOUNT) | |
ChatManager.chat(chat, String.format(failedFormat, money, strFrom, strTo, "an invalid inputted amount.")); | |
else if (failedReason == ConvertFailedReason.INVALID_CURRENCY) | |
ChatManager.chat(chat, String.format(failedFormat, money, strFrom, strTo, "both/one of the inputted currency/currencies being invalid/not supported.")); | |
else | |
ChatManager.chat(chat, String.format(failedFormat, money, strFrom, strTo, "black magic.")); | |
} | |
} else { | |
ChatManager.chat(chat, "Usage: " + this.getUsage()); | |
} | |
} | |
@Override | |
public String getCommand() { | |
return "currency"; | |
} | |
@Override | |
public String[] getAliases() { | |
return new String[]{"cc"}; | |
} | |
@Override | |
public String getHelp() { | |
return "Convert currencies!"; | |
} | |
@Override | |
public String getUsage() { | |
return "currency <initialcurrency> <targetcurrency> [<money>]"; | |
} | |
} | |
public static class CurrencyConverter { | |
private static JSONParser jsonParser = null; | |
private static final String apiUrl = "http://rate-exchange.appspot.com/currency?from=%s&to=%s&q=%f"; | |
public static CurrencyResult convertCurrency(String cFrom, String cTo, double initial) { | |
initJsonParser(); | |
if (initial > 0) { | |
if (cFrom != null && cTo != null && cFrom.length() == 3 && cTo.length() == 3) { | |
cFrom = cFrom.toUpperCase(); | |
cTo = cTo.toUpperCase(); | |
try { | |
String conversionResult = httpRequest(cFrom, cTo, initial); | |
Object objParsedResult = jsonParser.parse(conversionResult); | |
if (objParsedResult instanceof JSONObject) { | |
JSONObject parsedResult = (JSONObject) objParsedResult; | |
if (parsedResult == null || parsedResult.containsKey("err") || parsedResult.containsKey("error")) { | |
return new CurrencyResult(ConvertFailedReason.INVALID_CURRENCY); | |
} else { | |
String from = parsedResult.containsKey("from") ? parsedResult.get("from").toString() : cFrom; | |
String to = parsedResult.containsKey("to") ? parsedResult.get("to").toString() : cTo; | |
double result = parsedResult.containsKey("v") ? Double.parseDouble(parsedResult.get("v").toString()) : 0D; | |
double rate = parsedResult.containsKey("rate") ? Double.parseDouble(parsedResult.get("rate").toString()) : result / initial; | |
return new CurrencyResult(from, to, initial, result, rate); | |
} | |
} | |
} catch (Exception ex) { | |
} | |
} else { | |
return new CurrencyResult(ConvertFailedReason.INVALID_CURRENCY); | |
} | |
} else { | |
return new CurrencyResult(ConvertFailedReason.INVALID_AMOUNT); | |
} | |
return new CurrencyResult(ConvertFailedReason.UNKNOWN); | |
} | |
private static String httpRequest(String from, String to, double initial) throws Exception { | |
URL apiURL = new URL(String.format(apiUrl, from, to, initial)); | |
URLConnection connection = apiURL.openConnection(); | |
connection.setDoOutput(true); | |
connection.setRequestProperty("Accept-Charset", "UTF-8"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
String response = ""; | |
BufferedReader responseStream = null; | |
HttpURLConnection httpURLConnection = (HttpURLConnection) connection; | |
if (httpURLConnection.getResponseCode() == 200) { | |
responseStream = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8")); | |
} else { | |
responseStream = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream(), "UTF-8")); | |
} | |
response = responseStream.readLine(); | |
responseStream.close(); | |
return response; | |
} | |
private static void initJsonParser() { | |
if (jsonParser == null) jsonParser = new JSONParser(); | |
} | |
} | |
public static class CurrencyResult { | |
private ConvertFailedReason reason = null; | |
private double initial = -1D, result = -1D, rate = -1D; | |
private String from = null, to = null; | |
public CurrencyResult(String from, String to, double initial, double result) { | |
this.from = from; | |
this.to = to; | |
this.initial = initial; | |
this.result = result; | |
} | |
public CurrencyResult(String from, String to, double initial, double result, double rate) { | |
this.from = from; | |
this.to = to; | |
this.initial = initial; | |
this.result = result; | |
this.rate = rate; | |
} | |
public CurrencyResult(ConvertFailedReason failedReason) { | |
this.reason = failedReason; | |
} | |
public ConvertFailedReason getError() { | |
return this.reason; | |
} | |
public String getFrom() { | |
return this.from; | |
} | |
public double getInitial() { | |
return this.initial; | |
} | |
public double getRate() { | |
return this.rate; | |
} | |
public double getResult() { | |
return this.result; | |
} | |
public String getTo() { | |
return this.to; | |
} | |
public boolean hasFailed() { | |
return this.reason != null; | |
} | |
} | |
public static enum ConvertFailedReason { | |
UNKNOWN, INVALID_AMOUNT, INVALID_CURRENCY; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "CurrencyConverter", | |
"main": "Currency" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment