From 522ee4e632ac797121924312409c4c00bc622d28 Mon Sep 17 00:00:00 2001 From: b00lean Date: Mon, 6 Nov 2017 09:43:18 +0100 Subject: [PATCH] Removed YahooFinance --- .../extra/bitcoin/BitcoinExtension.java | 8 +- .../sources/yahoo/YahooFinanceRateSource.java | 198 ------------------ .../sources/poloniex/PoloniexRateSource.java | 11 +- 3 files changed, 9 insertions(+), 208 deletions(-) delete mode 100644 server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/bitcoin/sources/yahoo/YahooFinanceRateSource.java diff --git a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/bitcoin/BitcoinExtension.java b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/bitcoin/BitcoinExtension.java index 9d977d5..afc62c9 100644 --- a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/bitcoin/BitcoinExtension.java +++ b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/bitcoin/BitcoinExtension.java @@ -24,7 +24,6 @@ import com.generalbytes.batm.server.extensions.extra.bitcoin.paymentprocessors.b import com.generalbytes.batm.server.extensions.extra.bitcoin.paymentprocessors.coinofsale.CoinOfSalePP; import com.generalbytes.batm.server.extensions.extra.bitcoin.sources.FixPriceRateSource; import com.generalbytes.batm.server.extensions.extra.bitcoin.sources.bity.BityRateSource; -import com.generalbytes.batm.server.extensions.extra.bitcoin.sources.yahoo.YahooFinanceRateSource; import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.bitcoind.BATMBitcoindRPCWallet; import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.bitcore.BitcoreWallet; import com.generalbytes.batm.server.extensions.watchlist.IWatchList; @@ -153,12 +152,7 @@ public class BitcoinExtension implements IExtension{ StringTokenizer st = new StringTokenizer(sourceLogin,":"); String rsType = st.nextToken(); - if ("yahoo".equalsIgnoreCase(rsType)) { - if (st.hasMoreTokens()) { - return new YahooFinanceRateSource(st.nextToken().toUpperCase()); - } - return new YahooFinanceRateSource(ICurrencies.USD); - }else if ("btcfix".equalsIgnoreCase(rsType)) { + if ("btcfix".equalsIgnoreCase(rsType)) { BigDecimal rate = BigDecimal.ZERO; String preferredFiatCurrency = ICurrencies.USD; if (st.hasMoreTokens()) { diff --git a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/bitcoin/sources/yahoo/YahooFinanceRateSource.java b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/bitcoin/sources/yahoo/YahooFinanceRateSource.java deleted file mode 100644 index 67dbcaa..0000000 --- a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/bitcoin/sources/yahoo/YahooFinanceRateSource.java +++ /dev/null @@ -1,198 +0,0 @@ -/************************************************************************************* - * Copyright (C) 2014-2016 GENERAL BYTES s.r.o. All rights reserved. - * - * This software may be distributed and modified under the terms of the GNU - * General Public License version 2 (GPL2) as published by the Free Software - * Foundation and appearing in the file GPL2.TXT included in the packaging of - * this file. Please note that GPL2 Section 2[b] requires that all works based - * on this software must also be made publicly available under the terms of - * the GPL2 ("Copyleft"). - * - * Contact information - * ------------------- - * - * GENERAL BYTES s.r.o. - * Web : http://www.generalbytes.com - * - ************************************************************************************/ - -package com.generalbytes.batm.server.extensions.extra.bitcoin.sources.yahoo; - -import com.generalbytes.batm.server.extensions.ICurrencies; -import com.generalbytes.batm.server.extensions.IRateSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.math.BigDecimal; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.*; - - -public class YahooFinanceRateSource implements IRateSource{ - private static final String[] FIAT_CURRENCIES={"USD","EUR","CNY","CAD","RON","XAF","AUD","GBP","CZK","CHF","JPY", "PEN"}; - private static final Logger log = LoggerFactory.getLogger(YahooFinanceRateSource.class); - - - private static final HashMap rateAmounts = new HashMap(); - private static HashMap rateTimes = new HashMap(); - private static final long MAXIMUM_ALLOWED_TIME_OFFSET = 10 * 60 * 1000; //10 min - - - private String preferedFiatCurrency = ICurrencies.USD; - - public YahooFinanceRateSource(String preferedFiatCurrency) { - if (ICurrencies.EUR.equalsIgnoreCase(preferedFiatCurrency)) { - this.preferedFiatCurrency = ICurrencies.EUR; - }else if (ICurrencies.USD.equalsIgnoreCase(preferedFiatCurrency)) { - this.preferedFiatCurrency = ICurrencies.USD; - }else{ - this.preferedFiatCurrency = preferedFiatCurrency; - } - - } - - @Override - public BigDecimal getExchangeRateLast(String cryptoCurrency, String fiatCurrency) { - if (!ICurrencies.BTC.equalsIgnoreCase(cryptoCurrency)) { - return null; - } - String key = cryptoCurrency +"_" + fiatCurrency; - synchronized (rateAmounts) { - long now = System.currentTimeMillis(); - BigDecimal amount = rateAmounts.get(key); - if (amount == null) { - final Map rates = getExchangeRateLastSync(cryptoCurrency, fiatCurrency); - BigDecimal result = null; - if (rates != null) { - for (String toCurrency : rates.keySet()) { - String key2 = cryptoCurrency + "_" + toCurrency; - if (key.equalsIgnoreCase(key2)) { - result = rates.get(toCurrency); - } - rateAmounts.put(key2, rates.get(toCurrency)); - rateTimes.put(key2, now + MAXIMUM_ALLOWED_TIME_OFFSET); - } - } - log.debug("Called YahooFinance for rate: " + key + " = " + result); - return result; - }else { - Long expirationTime = rateTimes.get(key); - if (expirationTime > now) { - return rateAmounts.get(key); - }else{ - //do the job; - final Map rates = getExchangeRateLastSync(cryptoCurrency, fiatCurrency); - BigDecimal result = null; - if (rates != null) { - for (String toCurrency : rates.keySet()) { - String key2 = cryptoCurrency + "_" + toCurrency; - if (key.equalsIgnoreCase(key2)) { - result = rates.get(toCurrency); - } - rateAmounts.put(key2, rates.get(toCurrency)); - rateTimes.put(key2, now + MAXIMUM_ALLOWED_TIME_OFFSET); - } - } - log.debug("Called YahooFinance for rate: " + key + " = " + result); - return result; - - } - } - } - - } - - private Map getExchangeRateLastSync(String cryptoCurrency, String fiatCurrency) { - if (!ICurrencies.BTC.equalsIgnoreCase(cryptoCurrency)) { - return null; //unsupported currency - } - final Set fiatCurrencies = getFiatCurrencies(); - fiatCurrencies.add(fiatCurrency); - final Map exchangeRates = getExchangeRates(cryptoCurrency, fiatCurrencies); - return exchangeRates; - } - - - private static Map getExchangeRates(String fromCurrency, Set toCurrencies) { - try { - StringBuilder sb = new StringBuilder(); - for (String toCurrency : toCurrencies) { - sb.append(fromCurrency.toUpperCase()+toCurrency+"=X"); - sb.append("+"); - } - - Map results = new HashMap(); - final String symbols = sb.toString(); - String url = "https://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=" + symbols; - String result = getHTML(url); - if (result != null) { - result = result.replace("\"",""); - StringTokenizer lines = new StringTokenizer(result,"\n"); - while (lines.hasMoreElements()) { - String line = lines.nextToken(); - StringTokenizer st = new StringTokenizer(line,","); - String symbol = st.nextToken() ; //symbol - symbol = symbol.substring(fromCurrency.length(),symbol.length()-"=X".length()); - BigDecimal rate = new BigDecimal(st.nextToken()); - results.put(symbol,rate); - log.info("Obtained exchange rate " + fromCurrency +">" + symbol + " = " + rate ); - } - } - return results; - - }catch (Throwable t) { - t.printStackTrace(); - } - log.error("ERROR: Error obtaining exchange rate for " + fromCurrency + ">" + Arrays.toString(toCurrencies.toArray())); - return null; - } - - private static String getHTML(String urlToRead) { - URL url; - HttpURLConnection conn; - BufferedReader rd; - String line; - String result = ""; - try { - url = new URL(urlToRead); - conn = (HttpURLConnection) url.openConnection(); - conn.setRequestProperty("Accept-Charset", "UTF-8"); - conn.setRequestMethod("GET"); - rd = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); - while ((line = rd.readLine()) != null) { - result += line +"\n"; - } - rd.close(); - } catch (IOException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - return result; - } - - @Override - public Set getCryptoCurrencies() { - Set result = new HashSet(); - result.add(ICurrencies.BTC); - return result; - } - - @Override - public Set getFiatCurrencies() { - Set fiatCurrencies = new HashSet(); - fiatCurrencies.add(preferedFiatCurrency.toUpperCase()); - fiatCurrencies.addAll(Arrays.asList(FIAT_CURRENCIES)); - return fiatCurrencies; - } - - @Override - public String getPreferredFiatCurrency() { - return preferedFiatCurrency; - } - -} - diff --git a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/nxt/sources/poloniex/PoloniexRateSource.java b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/nxt/sources/poloniex/PoloniexRateSource.java index 728708f..c89abbf 100644 --- a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/nxt/sources/poloniex/PoloniexRateSource.java +++ b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/nxt/sources/poloniex/PoloniexRateSource.java @@ -19,7 +19,7 @@ package com.generalbytes.batm.server.extensions.extra.nxt.sources.poloniex; import com.generalbytes.batm.server.extensions.ICurrencies; import com.generalbytes.batm.server.extensions.IRateSource; -import com.generalbytes.batm.server.extensions.extra.bitcoin.sources.yahoo.YahooFinanceRateSource; +import com.generalbytes.batm.server.extensions.extra.bitcoin.exchanges.bitfinex.BitfinexExchange; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import si.mazi.rescu.RestProxyFactory; @@ -32,7 +32,7 @@ import java.util.Set; public class PoloniexRateSource implements IRateSource{ private static final Logger log = LoggerFactory.getLogger(PoloniexRateSource.class); - private YahooFinanceRateSource btcRs; + private BitfinexExchange btcRs; private String preferedFiatCurrency; private IPoloniexAPI api; @@ -45,7 +45,7 @@ public class PoloniexRateSource implements IRateSource{ preferedFiatCurrency = ICurrencies.USD; } this.preferedFiatCurrency = preferedFiatCurrency; - btcRs = new YahooFinanceRateSource(preferedFiatCurrency); + btcRs = new BitfinexExchange("***","***"); api = RestProxyFactory.createProxy(IPoloniexAPI.class, "https://poloniex.com"); } @@ -132,4 +132,9 @@ public class PoloniexRateSource implements IRateSource{ return null; } + + public static void main(String[] args) { + PoloniexRateSource rs = new PoloniexRateSource(ICurrencies.USD); + System.out.println("rs = " + rs.getExchangeRateLast(ICurrencies.NXT,ICurrencies.USD)); + } }