1
0

reorganize files and bring code inline with current master

Conflicts:
	lib/simple_config.py
This commit is contained in:
akshayaurora
2014-06-05 06:12:29 +05:30
committed by ThomasV
parent 9938316400
commit c121c1aa4e
67 changed files with 5360 additions and 2408 deletions

View File

@@ -6,13 +6,18 @@ This module is responsible for getting the conversion rates from different
bitcoin exchanges.
'''
import decimal
import json
from kivy.network.urlrequest import UrlRequest
from kivy.event import EventDispatcher
from kivy.properties import (OptionProperty, StringProperty, AliasProperty,
ListProperty)
from kivy.clock import Clock
import decimal
import json
from kivy.cache import Cache
# Register local cache
Cache.register('history_rate', timeout=220)
EXCHANGES = ["BitcoinAverage",
"BitcoinVenezuela",
@@ -25,27 +30,32 @@ EXCHANGES = ["BitcoinAverage",
"LocalBitcoins",
"Winkdex"]
HISTORY_EXCHNAGES = ['Coindesk',
'Winkdex',
'BitcoinVenezuela']
class Exchanger(EventDispatcher):
''' Provide exchanges rate between crypto and different national
currencies. See Module Documentation for details.
'''
symbols = {'ALL': 'Lek', 'AED': 'د.إ', 'AFN':'؋', 'ARS': '$', 'AMD': '֏',
'AWG': 'ƒ', 'ANG': 'ƒ', 'AOA': 'Kz', 'BDT': '', 'BHD': 'BD',
'BIF': 'FBu', 'BTC': 'BTC', 'BTN': 'Nu', 'CDF': 'FC', 'CHF': 'CHF',
'CLF': 'UF', 'CLP':'$', 'CVE': '$', 'DJF':'Fdj', 'DZD': 'دج',
'AUD': '$', 'AZN': 'ман', 'BSD': '$', 'BBD': '$', 'BYR': 'p', 'CRC': '',
'BZD': 'BZ$', 'BMD': '$', 'BOB': '$b', 'BAM': 'KM', 'BWP': 'P',
'BGN': 'лв', 'BRL': 'R$', 'BND': '$', 'KHR': '', 'CAD': '$',
'ERN': 'Nfk', 'ETB': 'Br', 'KYD': '$', 'USD': '$', 'CLP': '$',
'HRK': 'kn', 'CUP':'', 'CZK': '', 'DKK': 'kr', 'DOP': 'RD$',
'XCD': '$', 'EGP': '£', 'SVC': '$' , 'EEK': 'kr', 'EUR': '',
'FKP': '£', 'FJD': '$', 'GHC': '¢', 'GIP': '£', 'GTQ': 'Q', 'GBP': '£',
'GYD': '$', 'HNL': 'L', 'HKD': '$', 'HUF': 'Ft', 'ISK': 'kr',
'INR': '', 'IDR': 'Rp', 'IRR': '', 'IMP': '£', 'ILS': '', 'COP': '$',
'JMD': 'J$', 'JPY': '¥', 'JEP': '£', 'KZT': 'лв', 'KPW': '',
'KRW': '', 'KGS': 'лв', 'LAK': '', 'LVL': 'Ls', 'CNY': '¥'}
symbols = {'ALL': u'Lek', 'AED': u'د.إ', 'AFN':u'؋', 'ARS': u'$',
'AMD': u'֏', 'AWG': u'ƒ', 'ANG': u'ƒ', 'AOA': u'Kz', 'BDT': u'',
'BHD': u'BD', 'BIF': u'FBu', 'BTC': u'BTC', 'BTN': u'Nu', 'CDF': u'FC',
'CHF': u'CHF', 'CLF': u'UF', 'CLP':u'$', 'CVE': u'$', 'DJF':u'Fdj',
'DZD': u'دج', 'AUD': u'$', 'AZN': u'ман', 'BSD': u'$', 'BBD': u'$',
'BYR': u'p', 'CRC': u'', 'BZD': u'BZ$', 'BMD': u'$', 'BOB': u'$b',
'BAM': u'KM', 'BWP': u'P', 'BGN': 'uлв', 'BRL': u'R$', 'BND': u'$',
'KHR': u'', 'CAD': u'$', 'ERN': u'Nfk', 'ETB': u'Br', 'KYD': u'$',
'USD': u'$', 'CLP': u'$', 'HRK': u'kn', 'CUP': u'', 'CZK': u'',
'DKK': u'kr', 'DOP': u'RD$', 'XCD': u'$', 'EGP': u'£', 'SVC': u'$' ,
'EEK': u'kr', 'EUR': u'', u'FKP': u'£', 'FJD': u'$', 'GHC': u'¢',
'GIP': u'£', 'GTQ': u'Q', 'GBP': u'£', 'GYD': u'$', 'HNL': u'L',
'HKD': u'$', 'HUF': u'Ft', 'ISK': u'kr', 'INR': u'', 'IDR': u'Rp',
'IRR': u'', 'IMP': '£', 'ILS': '', 'COP': '$', 'JMD': u'J$',
'JPY': u'¥', 'JEP': u'£', 'KZT': u'лв', 'KPW': u'', 'KRW': u'',
'KGS': u'лв', 'LAK': u'', 'LVL': u'Ls', 'CNY': u'¥'}
_use_exchange = OptionProperty('Blockchain', options=EXCHANGES)
'''This is the exchange to be used for getting the currency exchange rates
@@ -56,23 +66,16 @@ class Exchanger(EventDispatcher):
'''
def _set_currency(self, value):
exchanger = self.exchanger
value = str(value)
if self.use_exchange == 'CoinDesk':
self._update_cd_currency(self.currency)
return
try:
self._currency = value
self.electrum_cinfig.set_key('currency', value, True)
except AttributeError:
self._currency = 'EUR'
self._currency = value
self.parent.electrum_config.set_key('currency', value, True)
def _get_currency(self):
try:
self._currency = self.electrum_config.get('currency', 'EUR')
except AttributeError:
pass
finally:
return self._currency
self._currency = self.parent.electrum_config.get('currency', 'EUR')
return self._currency
currency = AliasProperty(_get_currency, _set_currency, bind=('_currency',))
@@ -104,6 +107,7 @@ class Exchanger(EventDispatcher):
self.parent = parent
self.quote_currencies = None
self.exchanges = EXCHANGES
self.history_exchanges = HISTORY_EXCHNAGES
def exchange(self, btc_amount, quote_currency):
if self.quote_currencies is None:
@@ -115,10 +119,40 @@ class Exchanger(EventDispatcher):
return btc_amount * decimal.Decimal(quote_currencies[quote_currency])
def get_history_rate(self, item, btc_amt, mintime, maxtime):
def on_success(request, response):
response = json.loads(response)
try:
hrate = response['bpi'][mintime]
hrate = abs(btc_amt) * decimal.Decimal(hrate)
Cache.append('history_rate', uid, hrate)
except KeyError:
hrate = 'not found'
self.parent.set_history_rate(item, hrate)
# Check local cache before getting data from remote
exchange = 'coindesk'
uid = '{}:{}'.format(exchange, mintime)
hrate = Cache.get('history_rate', uid)
if hrate:
return hrate
req = UrlRequest(url='https://api.coindesk.com/v1/bpi/historical'
'/close.json?start={}&end={}'
.format(mintime, maxtime)
,on_success=on_success, timeout=15)
return None
def update_rate(self, dt):
''' This is called from :method:`start` every X seconds; to update the
rates for currencies for the currently selected exchange.
'''
if not self.parent.network or not self.parent.network.is_connected():
return
update_rates = {
"BitcoinAverage": self.update_ba,
"BitcoinVenezuela": self.update_bv,
@@ -268,7 +302,7 @@ class Exchanger(EventDispatcher):
for r in response:
quote_currencies[r] = _lookup_rate(response, r)
self.quote_currencies = quote_currencies
except KeyError:
except KeyError, TypeError:
pass
self.parent.set_currencies(quote_currencies)
@@ -329,9 +363,8 @@ class Exchanger(EventDispatcher):
timeout=5)
def start(self):
# check rates every few seconds
self.update_rate(0)
# check every few seconds
# check every 20 seconds
Clock.unschedule(self.update_rate)
Clock.schedule_interval(self.update_rate, 20)