From 4e4cecfa0dfff0ab89231af1dff079e92078e92f Mon Sep 17 00:00:00 2001 From: wjoe Date: Mon, 8 Apr 2013 14:40:32 +0100 Subject: Added bitcoin conversion --- blaconvert.java | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/blaconvert.java b/blaconvert.java index e2fd5a9..2741571 100644 --- a/blaconvert.java +++ b/blaconvert.java @@ -21,7 +21,7 @@ based on code by Jamie Walters based on code by Joe Robinson - Version 1.34 + Version 1.4 CHANGELOG 1.0 - First version @@ -34,6 +34,8 @@ 1.31 - Trying to add euro (probably won't work) 1.32 - Changed currency to unicode 1.33 - More euro fixing (maybe) + 1.34 - Fixed output of negative numbers + 1.4 - Added bitcoin conversion TODO - Raw output option - result only with no spaces/commas/units */ @@ -53,7 +55,7 @@ public class blaconvert { String sourceUnit = ""; String targetUnit = ""; String amount = ""; - String version = "1.34"; + String version = "1.4"; /* Build query from query or arguments, depending on whether arguments were given. */ @@ -87,14 +89,16 @@ public class blaconvert { } amount = split[start]; - for (int i = start+1; i < to; i++) + for (int i = start+1; i < to-1; i++) { sourceUnit += split[i]+"%20"; } - for (int i = to+1; i < split.length; i++) + sourceUnit += split[to-1]; + for (int i = to+1; i < split.length-1; i++) { targetUnit += split[i]+"%20"; } + targetUnit += split[split.length-1]; } catch (Exception e) { System.err.println("Error:" + e.getMessage()); System.exit(1); @@ -137,7 +141,46 @@ public class blaconvert { currency = true; } - + + //Bitcoin conversion + if (sourceUnit.trim().toUpperCase().equals("BTC")) { + URL url = new URL("https://blockchain.info/tobtc?currency="+targetUnit.toUpperCase()+"&value=1"); + try { + URLConnection urlc = url.openConnection(); + BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream())); + String response = br.readLine(); + double result = Double.parseDouble(response); + double value = Double.parseDouble(amount); + result = ((1/result)*value); + System.out.println(amount+" Bitcoins = "+result+" "+targetUnit); + System.exit(0); + } catch (Exception e) { + if(e.toString().contains("500")) { + System.out.println("500 Error: Probably unknown currency"); + } else if (e.toString().contains("404")) { + System.out.println("404 Error: Bitcoin converter unavailable"); + } + System.exit(1); + } + + } else if (targetUnit.trim().toUpperCase().equals("BTC")) { + URL url = new URL("https://blockchain.info/tobtc?currency="+sourceUnit.toUpperCase()+"&value="+amount); + URLConnection urlc = url.openConnection(); + try { + BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream())); + String response = br.readLine(); + System.out.println(amount+" "+sourceUnit+" = "+response+" Bitcoins"); + System.exit(0); + } catch (Exception e) { + if(e.toString().contains("500")) { + System.out.println("500 Error: Probably unknown currency"); + } else if (e.toString().contains("404")) { + System.out.println("404 Error: Bitcoin converter unavailable"); + } + System.exit(1); + } + + } //URL which provides XML response URL url = new URL("http://www.google.com/ig/calculator?hl=en&q="+amount+sourceUnit+"=?"+targetUnit); @@ -210,6 +253,8 @@ public class blaconvert { lhs += parts[i]+" "; } + + // Print results if(error.isEmpty()) { System.out.println(lhs+"= "+result); -- cgit v1.2.3