/*
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.42
This code is now a terrible mess of hacks on top of hacks. I'm sorry.
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
1.41 - Allowed "bitcoin"(s) in translation string as well as BTC
1.42 - Added litecoin conversion
TODO
- Raw output option - result only with no spaces/commas/units
TESTING
Another test
*/
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.42";
/* 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;
}
String response = "";
BufferedReader br;
double dresult = 0.0;
URL url;
URLConnection urlc;
//Bitcoin conversion
if (sourceUnit.trim().toUpperCase().equals("BTC") || sourceUnit.trim().toUpperCase().contains("BITCOIN")
|| sourceUnit.trim().toUpperCase().equals("LTC") || sourceUnit.trim().toUpperCase().contains("LITECOIN")) {
if (targetUnit.trim().toUpperCase().equals("LTC") || targetUnit.trim().toUpperCase().contains("LITECOIN")) {
dresult = Double.parseDouble(amount);
url = new URL("http://www.lc8n.com/upload/ltc");
urlc = url.openConnection();
br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String ltcresponse = br.readLine();
double value = Double.parseDouble(ltcresponse);
dresult = (dresult*(1/value));
} else {
url = new URL("https://blockchain.info/tobtc?currency="+targetUnit.toUpperCase()+"&value=1");
try {
urlc = url.openConnection();
br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
response = br.readLine();
dresult = Double.parseDouble(response);
double value = Double.parseDouble(amount);
dresult = ((1/dresult)*value);
} 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);
}
if (sourceUnit.trim().toUpperCase().equals("LTC") || sourceUnit.trim().toUpperCase().contains("LITECOIN")) {
url = new URL("http://www.lc8n.com/upload/ltc");
urlc = url.openConnection();
br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String ltcresponse = br.readLine();
double value = Double.parseDouble(ltcresponse);
dresult = (dresult*value);
}
}
response = String.format("%.6f", dresult);
System.out.println(amount+" "+sourceUnit+" = "+response+" "+targetUnit);
System.exit(0);
} else if (targetUnit.trim().toUpperCase().equals("BTC") || targetUnit.trim().toUpperCase().contains("BITCOINS")
|| targetUnit.trim().toUpperCase().equals("LTC") || targetUnit.trim().toUpperCase().contains("LITECOIN")) {
if (sourceUnit.trim().toUpperCase().equals("LTC") || sourceUnit.trim().toUpperCase().contains("LITECOIN")) {
url = new URL("http://www.lc8n.com/upload/ltc");
urlc = url.openConnection();
br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String ltcresponse = br.readLine();
double value = Double.parseDouble(ltcresponse);
dresult = (dresult*value);
} else {
url = new URL("https://blockchain.info/tobtc?currency="+sourceUnit.toUpperCase()+"&value="+amount);
urlc = url.openConnection();
try {
br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
response = br.readLine();
dresult = Double.parseDouble(response);
} 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);
}
if (targetUnit.trim().toUpperCase().equals("LTC") || targetUnit.trim().toUpperCase().contains("LITECOIN")) {
url = new URL("http://www.lc8n.com/upload/ltc");
urlc = url.openConnection();
br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String ltcresponse = br.readLine();
double value = Double.parseDouble(ltcresponse);
dresult = (dresult*(1/value));
}
}
response = String.format("%.6f", dresult);
System.out.println(amount+" "+sourceUnit+" = "+response+" "+targetUnit);
System.exit(0);
}
//URL which provides XML response
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);
urlc = url.openConnection();
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