/* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /* blaconvert - Java program for converting units of measurement and currency using Google APIs by blatech Copyright 2011 Joe Robinson based on code by Jamie Walters based on code by Joe Robinson Version 1.4 CHANGELOG 1.0 - First version 1.1 - Allow units with more than one word eg "light year" - Added "same unit" error - Ensure standard imperial measurements are used for some units - google defaults to US 1.2 - Replace crazy non unicode spaces with commas 1.21 - Added quart to imperial measurements 1.3 - Added support for currency symbols 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 */ import java.net.URL; import java.net.URLConnection; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class blaconvert { public static void main(String[] args) throws IOException { String sourceUnit = ""; String targetUnit = ""; String amount = ""; String version = "1.4"; /* Build query from query or arguments, depending on whether arguments were given. */ if (args.length == 0) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] split = br.readLine().split(" "); int start = 0; int to = 0; int output = 0; if (split[0].startsWith("-")) { if (split[0].equals("-v") || split[0].equals("--version")) { System.out.println("v"+version); System.exit(0); } else if (split[0].charAt(1) == 'r') { //reserved for raw output option output = 1; start = 1; } } for(int i = 0; i < split.length; i++) { if (split[i].toLowerCase().equals("to") || split[i].toLowerCase().equals("in")) { to = i; } } amount = split[start]; for (int i = start+1; i < to-1; i++) { sourceUnit += split[i]+"%20"; } 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); } } else { if (args[0].equals("-v") || args[0].equals("--version")) { System.out.println("v"+version); System.exit(0); } amount = args[0]; sourceUnit = args[1]; targetUnit = args[2]; } String[] imperial = {"pint", "gallon", "gal", "cup", "teaspoon", "tablespoon", "fl oz", "fluid ounce", "quart"}; for(int i = 0; i < imperial.length; i++) { if (sourceUnit.startsWith(imperial[i])) { sourceUnit = "Imperial%20"+sourceUnit; } if (targetUnit.startsWith(imperial[i])) { targetUnit = "Imperial%20"+targetUnit; } } boolean currency = false; if(amount.contains("\u00A3") || amount.contains("\u0024") || amount.contains("\u20AC") || amount.contains("\u00A5") || amount.contains("\u0080")) { amount.replaceAll("\u00A3","%A3"); amount.replaceAll("\u0024","%24"); amount.replaceAll("\u20AC","%80"); amount.replaceAll("\u0080","%80"); amount.replaceAll("\u00A5","%A5"); 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); //System.out.println("http://www.google.com/ig/calculator?hl=en&q="+amount+sourceUnit+"=?"+targetUnit); URLConnection urlc = url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream())); String line; String[] lines; String tempString = ""; String lhs = ""; String rhs = ""; String error = ""; //Read every line while ((line = br.readLine()) != null) { // Splits the google output by the commas lines = line.split(","); for(int i=0; i